# 上游发送了无效的 Content-Length 头

- **ID:** `nginx/upstream-sent-invalid-content-length`
- **领域:** nginx
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

上游服务器返回了包含负数或非数字值的 Content-Length 头，违反了 HTTP 协议。

## 版本兼容性

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

## 解决方案

1. ```
   Fix the upstream application to return a valid Content-Length header (e.g., in Python Flask: remove or set correct Content-Length via response.headers['Content-Length'] = str(len(data)))
   ```
2. ```
   Add proxy_ignore_headers Content-Length; in nginx config to ignore upstream's Content-Length if it is invalid (use with caution). Example: location / { proxy_pass http://backend; proxy_ignore_headers Content-Length; }
   ```
3. ```
   Update upstream server code to not send Content-Length for chunked responses (e.g., in Node.js: res.removeHeader('Content-Length') before piping).
   ```

## 无效尝试

- **** — Buffer size does not affect header validity; the error is about malformed header value, not size. (70% 失败率)
- **** — Even without buffering, nginx parses response headers; the invalid Content-Length is still detected. (80% 失败率)
- **** — This controls request body size, not response header validation. (90% 失败率)
