nginx
protocol_error
ai_generated
true
upstream sent invalid Content-Length: 0 with non-empty body while reading response header from upstream, client: 10.0.0.1, server: example.com
ID: nginx/upstream-sent-invalid-content-length-zero-body
74%Fix Rate
83%Confidence
1Evidence
2024-06-22First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| nginx 1.18.0 | active | — | — | — |
| nginx 1.20.1 | active | — | — | — |
| nginx 1.22.0 | active | — | — | — |
| nginx 1.24.0 | active | — | — | — |
Root Cause
Upstream sends a Content-Length of 0 but then includes a non-empty response body, causing nginx to detect a mismatch and abort.
generic中文
上游发送了 Content-Length 为 0 但随后包含了非空的响应主体,导致 nginx 检测到不匹配并终止。
Official Documentation
https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_hide_headerWorkarounds
-
85% success Fix the upstream application to correctly set Content-Length to the actual body size. For example, in a Go HTTP handler, ensure Content-Length matches the body: func handler(w http.ResponseWriter, r *http.Request) { body := []byte("Hello") w.Header().Set("Content-Length", strconv.Itoa(len(body))) w.Write(body) } Avoid hardcoding Content-Length to 0 when writing a body.
Fix the upstream application to correctly set Content-Length to the actual body size. For example, in a Go HTTP handler, ensure Content-Length matches the body: func handler(w http.ResponseWriter, r *http.Request) { body := []byte("Hello") w.Header().Set("Content-Length", strconv.Itoa(len(body))) w.Write(body) } Avoid hardcoding Content-Length to 0 when writing a body. -
72% success Use nginx's proxy_hide_header Content-Length; to remove the upstream's Content-Length header, forcing nginx to use chunked encoding or calculate length from the actual body. Example: location / { proxy_pass http://upstream; proxy_hide_header Content-Length; }
Use nginx's proxy_hide_header Content-Length; to remove the upstream's Content-Length header, forcing nginx to use chunked encoding or calculate length from the actual body. Example: location / { proxy_pass http://upstream; proxy_hide_header Content-Length; }
中文步骤
Fix the upstream application to correctly set Content-Length to the actual body size. For example, in a Go HTTP handler, ensure Content-Length matches the body: func handler(w http.ResponseWriter, r *http.Request) { body := []byte("Hello") w.Header().Set("Content-Length", strconv.Itoa(len(body))) w.Write(body) } Avoid hardcoding Content-Length to 0 when writing a body.Use nginx's proxy_hide_header Content-Length; to remove the upstream's Content-Length header, forcing nginx to use chunked encoding or calculate length from the actual body. Example: location / { proxy_pass http://upstream; proxy_hide_header Content-Length; }
Dead Ends
Common approaches that don't work:
-
Set proxy_request_buffering off;
95% fail
Request buffering affects client request body, not response body handling from upstream.
-
Increase proxy_buffer_size
93% fail
Buffer size does not affect the Content-Length validation logic; the error is about header-body mismatch.
-
Disable chunked transfer encoding with proxy_set_header Transfer-Encoding '';
88% fail
This modifies request headers to upstream, not the response; the error is in the response.