上游在发送 Content-Length 头的同时发送了分块传输编码,客户端:10.0.0.1,服务器:example.com
upstream sent chunked transfer encoding while sending Content-Length header, client: 10.0.0.1, server: example.com
ID: nginx/upstream-sent-http-1-1-chunked-with-content-length
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| nginx 1.20.2 | active | — | — | — |
| nginx 1.22.1 | active | — | — | — |
| nginx 1.24.0 | active | — | — | — |
| nginx 1.26.0 | active | — | — | — |
根因分析
上游服务器发送了冲突的 HTTP 头:同时包含 Content-Length 和 Transfer-Encoding: chunked,违反了 HTTP/1.1 规范。
English
Upstream server sends conflicting HTTP headers: both Content-Length and Transfer-Encoding: chunked, violating HTTP/1.1 spec.
官方文档
https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_hide_header解决方案
-
Fix the upstream application to not send both Content-Length and Transfer-Encoding headers. For example, in a Python Flask app, remove the Content-Length header when using chunked encoding: @app.after_request def remove_conflicting_headers(response): if response.headers.get('Transfer-Encoding') == 'chunked': response.headers.pop('Content-Length', None) return response -
Use nginx's proxy_hide_header directive to remove the conflicting Content-Length header from upstream responses: proxy_hide_header Content-Length; This forces nginx to rely on chunked encoding alone.
无效尝试
常见但无效的做法:
-
Set proxy_http_version 1.0;
70% 失败
HTTP/1.0 does not support chunked encoding, but the upstream may still send conflicting headers; this can cause other issues like connection close.
-
Disable chunked transfer with proxy_set_header Transfer-Encoding '';
90% 失败
This removes the header from the request to upstream, not the response; the error is about the response from upstream.
-
Increase proxy_buffer_size
95% 失败
Buffer size does not affect header parsing or protocol compliance; the error is a protocol violation, not a buffer overflow.