nginx
protocol_error
ai_generated
true
upstream sent invalid Content-Length: -1 while reading response header from upstream
ID: nginx/upstream-sent-invalid-content-length-negative
90%Fix Rate
85%Confidence
1Evidence
2025-02-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| nginx 1.24.0 | active | — | — | — |
| nginx 1.25.3 | active | — | — | — |
| nginx 1.26.0 | active | — | — | — |
Root Cause
Upstream server returns a negative Content-Length value in the HTTP response, which violates HTTP specification.
genericOfficial Documentation
https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ignore_headersWorkarounds
-
85% success Use proxy_ignore_headers to ignore the Content-Length header from upstream: location / { proxy_pass http://upstream; proxy_ignore_headers Content-Length; }
Use proxy_ignore_headers to ignore the Content-Length header from upstream: location / { proxy_pass http://upstream; proxy_ignore_headers Content-Length; } -
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))})
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))})
中文步骤
Use proxy_ignore_headers to ignore the Content-Length header from upstream: location / { proxy_pass http://upstream; proxy_ignore_headers Content-Length; }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
Common approaches that don't work:
-
Add proxy_buffering off;
90% fail
Buffering does not fix an invalid header value from upstream.
-
Increase proxy_buffer_size
100% fail
This addresses header size, not header validity.
-
Restart nginx
100% fail
Restarting does not change the upstream behavior.