nginx protocol_error ai_generated true

上游发送了无效的 transfer-encoding

upstream sent invalid transfer-encoding while reading response body from upstream

ID: nginx/upstream-sent-invalid-transfer-encoding

其他格式: JSON · Markdown 中文 · English
82%修复率
84%置信度
1证据数
2024-04-22首次发现

版本兼容性

版本状态引入弃用备注
nginx-1.24.0 active
nginx-1.25.3 active
nginx-1.26.0 active

根因分析

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

English

The upstream server returns a response with a Transfer-Encoding header that is malformed or conflicting with Content-Length, causing nginx to reject the response.

generic

官方文档

https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

解决方案

  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;
    }

无效尝试

常见但无效的做法:

  1. 60% 失败

    HTTP/1.0 cannot use chunked encoding; the upstream must send Content-Length instead.

  2. 90% 失败

    This only affects connection persistence, not the encoding of the response body.

  3. 95% 失败

    Buffer sizes affect data handling, not header validation.