nginx protocol_error ai_generated true

upstream sent invalid Content-Length: -1 while reading response header from upstream

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

Also available as: JSON · Markdown
90%Fix Rate
85%Confidence
1Evidence
2025-02-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
nginx 1.24.0 active
nginx 1.25.3 active
nginx 1.26.0 active

Root Cause

Upstream server returns a negative Content-Length value in the HTTP response, which violates HTTP specification.

generic

Official Documentation

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

Workarounds

  1. 85% success Use proxy_ignore_headers to ignore the Content-Length header from upstream: location / { proxy_pass http://upstream; proxy_ignore_headers Content-Length; }
    Use proxy_ignore_headers to ignore the Content-Length header from upstream:
      location / {
          proxy_pass http://upstream;
          proxy_ignore_headers Content-Length;
      }
  2. 95% success Fix the upstream application to emit a valid Content-Length (non-negative integer). For example, in a Python Flask app: from flask import Response @app.route('/data') def data(): body = 'hello' return Response(body, headers={'Content-Length': str(len(body))})
    Fix the upstream application to emit a valid Content-Length (non-negative integer). For example, in a Python Flask app:
      from flask import Response
      @app.route('/data')
      def data():
          body = 'hello'
          return Response(body, headers={'Content-Length': str(len(body))})

中文步骤

  1. Use proxy_ignore_headers to ignore the Content-Length header from upstream:
      location / {
          proxy_pass http://upstream;
          proxy_ignore_headers Content-Length;
      }
  2. Fix the upstream application to emit a valid Content-Length (non-negative integer). For example, in a Python Flask app:
      from flask import Response
      @app.route('/data')
      def data():
          body = 'hello'
          return Response(body, headers={'Content-Length': str(len(body))})

Dead Ends

Common approaches that don't work:

  1. Add proxy_buffering off; 90% fail

    Buffering does not fix an invalid header value from upstream.

  2. Increase proxy_buffer_size 100% fail

    This addresses header size, not header validity.

  3. Restart nginx 100% fail

    Restarting does not change the upstream behavior.