nginx protocol_error ai_generated partial

upstream sent invalid status line while reading response header from upstream: "HTTP/1.1 200 OK\r\n" (extra characters)

ID: nginx/upstream-sent-invalid-status-line-while-reading-response-header

Also available as: JSON · Markdown
85%Fix Rate
82%Confidence
1Evidence
2025-08-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
nginx 1.24.0 active
nginx 1.25.3 active
nginx 1.26.0 active

Root Cause

Upstream server sends a response with malformed status line, such as extra spaces, invalid characters, or non-standard HTTP version.

generic

Official Documentation

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

Workarounds

  1. 30% success Use a custom nginx module or Lua to sanitize upstream response. Example with lua-nginx-module: location / { rewrite_by_lua_block { -- This is a placeholder; actual sanitization would require body_filter_by_lua } body_filter_by_lua ' -- If you can modify headers, but status line is not easily modifiable in Lua -- Better to fix upstream '; proxy_pass http://upstream; }
    Use a custom nginx module or Lua to sanitize upstream response. Example with lua-nginx-module:
      location / {
          rewrite_by_lua_block {
              -- This is a placeholder; actual sanitization would require body_filter_by_lua
          }
          body_filter_by_lua '
              -- If you can modify headers, but status line is not easily modifiable in Lua
              -- Better to fix upstream
          ';
          proxy_pass http://upstream;
      }
  2. 95% success Fix the upstream application to emit a valid HTTP status line. For example, in a Python server: # Ensure the response line is exactly "HTTP/1.1 200 OK\r\n" response = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello" # Avoid extra characters or spaces after the status line
    Fix the upstream application to emit a valid HTTP status line. For example, in a Python server:
      # Ensure the response line is exactly "HTTP/1.1 200 OK\r\n"
      response = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello"
      # Avoid extra characters or spaces after the status line

中文步骤

  1. Use a custom nginx module or Lua to sanitize upstream response. Example with lua-nginx-module:
      location / {
          rewrite_by_lua_block {
              -- This is a placeholder; actual sanitization would require body_filter_by_lua
          }
          body_filter_by_lua '
              -- If you can modify headers, but status line is not easily modifiable in Lua
              -- Better to fix upstream
          ';
          proxy_pass http://upstream;
      }
  2. Fix the upstream application to emit a valid HTTP status line. For example, in a Python server:
      # Ensure the response line is exactly "HTTP/1.1 200 OK\r\n"
      response = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello"
      # Avoid extra characters or spaces after the status line

Dead Ends

Common approaches that don't work:

  1. Add proxy_set_header Host $host; 100% fail

    This sets request headers, not response validation.

  2. Increase proxy_read_timeout 100% fail

    Timeout does not fix malformed response line.

  3. Restart nginx 100% fail

    Restarting does not change upstream behavior.