nginx protocol_error ai_generated true

上游发送了无效的 Content-Length 头

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

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

其他格式: JSON · Markdown 中文 · English
90%修复率
85%置信度
1证据数
2024-03-15首次发现

版本兼容性

版本状态引入弃用备注
nginx/1.24.0 active
nginx/1.22.1 active
nginx/1.26.0 active

根因分析

上游服务器返回了包含负数或非数字值的 Content-Length 头,违反了 HTTP 协议。

English

Upstream server returned a Content-Length header with a negative or non-numeric value, violating HTTP protocol.

generic

官方文档

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

解决方案

  1. Fix the upstream application to return a valid Content-Length header (e.g., in Python Flask: remove or set correct Content-Length via response.headers['Content-Length'] = str(len(data)))
  2. Add proxy_ignore_headers Content-Length; in nginx config to ignore upstream's Content-Length if it is invalid (use with caution). Example: location / { proxy_pass http://backend; proxy_ignore_headers Content-Length; }
  3. Update upstream server code to not send Content-Length for chunked responses (e.g., in Node.js: res.removeHeader('Content-Length') before piping).

无效尝试

常见但无效的做法:

  1. 70% 失败

    Buffer size does not affect header validity; the error is about malformed header value, not size.

  2. 80% 失败

    Even without buffering, nginx parses response headers; the invalid Content-Length is still detected.

  3. 90% 失败

    This controls request body size, not response header validation.