# 上游发送了无效的 chunked 传输编码

- **ID:** `nginx/upstream-sent-invalid-chunked-transfer-encoding`
- **领域:** nginx
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

上游服务器发送了格式错误的 chunked 响应（例如缺少 CRLF、无效的块大小），违反了 HTTP/1.1 的分块传输编码规则。

## 版本兼容性

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

## 解决方案

1. ```
   Fix the upstream application to generate proper chunked encoding (e.g., in Go: ensure http.ResponseWriter uses default chunking correctly, avoid manual chunked writes).
   ```
2. ```
   Disable chunked transfer encoding in upstream by setting Content-Length header in the response (e.g., in Node.js: res.setHeader('Content-Length', Buffer.byteLength(body)); res.end(body)).
   ```
3. ```
   Use proxy_set_header Connection ''; to force HTTP/1.0 and avoid chunked encoding, then ensure upstream responds with Content-Length.
   ```

## 无效尝试

- **** — Timeout does not fix malformed data; the error is structural, not timing-related. (75% 失败率)
- **** — If upstream expects HTTP/1.1, this may break communication; also, nginx still parses chunked if upstream sends it. (65% 失败率)
- **** — This directive does not exist in nginx; it is a common misconception. (90% 失败率)
