nginx protocol_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
88%Fix Rate
82%Confidence
1Evidence
2024-06-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
nginx/1.24.0 active
nginx/1.22.1 active
nginx/1.26.0 active

Root Cause

Upstream server sent a malformed chunked response (e.g., missing CRLF, invalid chunk size) that violates HTTP/1.1 chunked transfer encoding rules.

generic

中文

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

Official Documentation

https://nginx.org/en/docs/http/ngx_http_upstream_module.html

Workarounds

  1. 90% success Fix the upstream application to generate proper chunked encoding (e.g., in Go: ensure http.ResponseWriter uses default chunking correctly, avoid manual chunked writes).
    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. 85% success 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)).
    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. 75% success Use proxy_set_header Connection ''; to force HTTP/1.0 and avoid chunked encoding, then ensure upstream responds with Content-Length.
    Use proxy_set_header Connection ''; to force HTTP/1.0 and avoid chunked encoding, then ensure upstream responds with Content-Length.

中文步骤

  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.

Dead Ends

Common approaches that don't work:

  1. 75% fail

    Timeout does not fix malformed data; the error is structural, not timing-related.

  2. 65% fail

    If upstream expects HTTP/1.1, this may break communication; also, nginx still parses chunked if upstream sends it.

  3. 90% fail

    This directive does not exist in nginx; it is a common misconception.