# 上游在发送 Content-Length 头的同时发送了分块传输编码，客户端：10.0.0.1，服务器：example.com

- **ID:** `nginx/upstream-sent-http-1-1-chunked-with-content-length`
- **领域:** nginx
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

上游服务器发送了冲突的 HTTP 头：同时包含 Content-Length 和 Transfer-Encoding: chunked，违反了 HTTP/1.1 规范。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| nginx 1.20.2 | active | — | — |
| nginx 1.22.1 | active | — | — |
| nginx 1.24.0 | active | — | — |
| nginx 1.26.0 | active | — | — |

## 解决方案

1. ```
   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
   ```
2. ```
   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;** — HTTP/1.0 does not support chunked encoding, but the upstream may still send conflicting headers; this can cause other issues like connection close. (70% 失败率)
- **Disable chunked transfer with proxy_set_header Transfer-Encoding '';** — This removes the header from the request to upstream, not the response; the error is about the response from upstream. (90% 失败率)
- **Increase proxy_buffer_size** — Buffer size does not affect header parsing or protocol compliance; the error is a protocol violation, not a buffer overflow. (95% 失败率)
