# 上游服务器在读取响应头时发送了无效的内容长度：0

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

## 根因

上游服务器发送了值为 0 的 Content-Length 头部，但包含了响应体，违反了 HTTP/1.1 规范，导致 nginx 拒绝响应。

## 版本兼容性

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

## 解决方案

1. ```
   Add 'proxy_ignore_headers Content-Length;' in the location block to make nginx ignore the Content-Length header from upstream, allowing the response to be processed based on chunked encoding or connection close.
   ```
2. ```
   Fix the upstream application to not send Content-Length: 0 when there is a body; use chunked transfer encoding instead by setting 'Transfer-Encoding: chunked' and omitting Content-Length.
   ```
3. ```
   If the upstream is a legacy server that cannot be changed, use a proxy module like 'ngx_http_lua_module' to strip the Content-Length header: 'header_filter_by_lua_block { if ngx.header["Content-Length"] == 0 then ngx.header["Content-Length"] = nil end }'
   ```

## 无效尝试

- **** — The error is about header semantic validity, not buffer size. (85% 失败率)
- **** — proxy_set_header modifies request headers, not response headers. (90% 失败率)
- **** — The error occurs during header parsing, which is independent of response buffering. (95% 失败率)
