# 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`
- **Domain:** nginx
- **Category:** protocol_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

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

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| nginx 1.24.0 | active | — | — |
| nginx 1.25.3 | active | — | — |
| nginx 1.26.0 | active | — | — |

## Workarounds

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;
  }** (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;
  }
   ```
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** (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
   ```

## Dead Ends

- **Add proxy_set_header Host $host;** — This sets request headers, not response validation. (100% fail)
- **Increase proxy_read_timeout** — Timeout does not fix malformed response line. (100% fail)
- **Restart nginx** — Restarting does not change upstream behavior. (100% fail)
