nginx protocol_error ai_generated partial

upstream sent invalid content-length: 0 while reading response header from upstream

ID: nginx/upstream-sent-invalid-content-length-zero

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
1Evidence
2024-05-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
nginx 1.24.0 active
nginx 1.22.1 active
nginx 1.20.2 active
nginx 1.18.0 active

Root Cause

The upstream server sent a Content-Length header with value 0 but included a response body, violating HTTP/1.1 specifications and causing nginx to reject the response.

generic

中文

上游服务器发送了值为 0 的 Content-Length 头部,但包含了响应体,违反了 HTTP/1.1 规范,导致 nginx 拒绝响应。

Official Documentation

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

Workarounds

  1. 85% success Add 'proxy_ignore_headers Content-Length;' in the location block to make nginx ignore the Content-Length header from upstream, allowing the response to be processed based on chunked encoding or connection close.
    Add 'proxy_ignore_headers Content-Length;' in the location block to make nginx ignore the Content-Length header from upstream, allowing the response to be processed based on chunked encoding or connection close.
  2. 90% success Fix the upstream application to not send Content-Length: 0 when there is a body; use chunked transfer encoding instead by setting 'Transfer-Encoding: chunked' and omitting Content-Length.
    Fix the upstream application to not send Content-Length: 0 when there is a body; use chunked transfer encoding instead by setting 'Transfer-Encoding: chunked' and omitting Content-Length.
  3. 75% success If the upstream is a legacy server that cannot be changed, use a proxy module like 'ngx_http_lua_module' to strip the Content-Length header: 'header_filter_by_lua_block { if ngx.header["Content-Length"] == 0 then ngx.header["Content-Length"] = nil end }'
    If the upstream is a legacy server that cannot be changed, use a proxy module like 'ngx_http_lua_module' to strip the Content-Length header: 'header_filter_by_lua_block { if ngx.header["Content-Length"] == 0 then ngx.header["Content-Length"] = nil end }'

中文步骤

  1. Add 'proxy_ignore_headers Content-Length;' in the location block to make nginx ignore the Content-Length header from upstream, allowing the response to be processed based on chunked encoding or connection close.
  2. Fix the upstream application to not send Content-Length: 0 when there is a body; use chunked transfer encoding instead by setting 'Transfer-Encoding: chunked' and omitting Content-Length.
  3. If the upstream is a legacy server that cannot be changed, use a proxy module like 'ngx_http_lua_module' to strip the Content-Length header: 'header_filter_by_lua_block { if ngx.header["Content-Length"] == 0 then ngx.header["Content-Length"] = nil end }'

Dead Ends

Common approaches that don't work:

  1. 85% fail

    The error is about header semantic validity, not buffer size.

  2. 90% fail

    proxy_set_header modifies request headers, not response headers.

  3. 95% fail

    The error occurs during header parsing, which is independent of response buffering.