# upstream sent invalid Content-Length: -1 while reading response header from upstream

- **ID:** `nginx/upstream-sent-invalid-content-length-negative`
- **Domain:** nginx
- **Category:** protocol_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

Upstream server returns a negative Content-Length value in the HTTP response, which violates HTTP specification.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| nginx 1.24.0 | active | — | — |
| nginx 1.25.3 | active | — | — |
| nginx 1.26.0 | active | — | — |

## Workarounds

1. **Use proxy_ignore_headers to ignore the Content-Length header from upstream:
  location / {
      proxy_pass http://upstream;
      proxy_ignore_headers Content-Length;
  }** (85% success)
   ```
   Use proxy_ignore_headers to ignore the Content-Length header from upstream:
  location / {
      proxy_pass http://upstream;
      proxy_ignore_headers Content-Length;
  }
   ```
2. **Fix the upstream application to emit a valid Content-Length (non-negative integer). For example, in a Python Flask app:
  from flask import Response
  @app.route('/data')
  def data():
      body = 'hello'
      return Response(body, headers={'Content-Length': str(len(body))})** (95% success)
   ```
   Fix the upstream application to emit a valid Content-Length (non-negative integer). For example, in a Python Flask app:
  from flask import Response
  @app.route('/data')
  def data():
      body = 'hello'
      return Response(body, headers={'Content-Length': str(len(body))})
   ```

## Dead Ends

- **Add proxy_buffering off;** — Buffering does not fix an invalid header value from upstream. (90% fail)
- **Increase proxy_buffer_size** — This addresses header size, not header validity. (100% fail)
- **Restart nginx** — Restarting does not change the upstream behavior. (100% fail)
