nginx protocol_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
82%Fix Rate
84%Confidence
1Evidence
2024-04-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
nginx-1.24.0 active
nginx-1.25.3 active
nginx-1.26.0 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 85% success 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.
    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. 80% success 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; }
    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. 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;
    }

Dead Ends

Common approaches that don't work:

  1. 60% fail

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

  2. 90% fail

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

  3. 95% fail

    Buffer sizes affect data handling, not header validation.