nginx
protocol_error
ai_generated
true
上游发送了无效的 Content-Length:0 但主体非空,从上游读取响应头时,客户端:10.0.0.1,服务器:example.com
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%修复率
83%置信度
1证据数
2024-06-22首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| nginx 1.18.0 | active | — | — | — |
| nginx 1.20.1 | active | — | — | — |
| nginx 1.22.0 | active | — | — | — |
| nginx 1.24.0 | active | — | — | — |
根因分析
上游发送了 Content-Length 为 0 但随后包含了非空的响应主体,导致 nginx 检测到不匹配并终止。
English
Upstream sends a Content-Length of 0 but then includes a non-empty response body, causing nginx to detect a mismatch and abort.
官方文档
https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_hide_header解决方案
-
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; }
无效尝试
常见但无效的做法:
-
Set proxy_request_buffering off;
95% 失败
Request buffering affects client request body, not response body handling from upstream.
-
Increase proxy_buffer_size
93% 失败
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% 失败
This modifies request headers to upstream, not the response; the error is in the response.