# 上游发送了无效的 transfer-encoding

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

## 根因

上游服务器返回的响应中包含格式错误或与 Content-Length 冲突的 Transfer-Encoding 头部，导致 nginx 拒绝该响应。

## 版本兼容性

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

## 解决方案

1. ```
   Fix the upstream application to send a valid Transfer-Encoding header. For example, in a Node.js Express app, ensure you use res.end() properly to avoid conflicting headers:
app.get('/data', (req, res) => {
    res.set('Transfer-Encoding', 'chunked');
    res.write('Hello');
    res.end();
});
# Or use a library like compression for proper encoding.
   ```
2. ```
   If the upstream is a legacy server, force nginx to use HTTP/1.1 and disable buffering to work around the issue:
location / {
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_buffering off;
    proxy_pass http://upstream;
}
   ```

## 无效尝试

- **** — HTTP/1.0 cannot use chunked encoding; the upstream must send Content-Length instead. (60% 失败率)
- **** — This only affects connection persistence, not the encoding of the response body. (90% 失败率)
- **** — Buffer sizes affect data handling, not header validation. (95% 失败率)
