# upstream sent invalid host header while connecting to upstream

- **ID:** `nginx/invalid-host-header-upstream`
- **Domain:** nginx
- **Category:** protocol_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

The upstream server responded with an invalid or malformed Host header (e.g., empty, non-ASCII, or too long), causing nginx to reject it during response processing.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| nginx 1.24.0 | active | — | — |
| nginx 1.22.1 | active | — | — |
| nginx 1.20.2 | active | — | — |

## Workarounds

1. **Fix the upstream application to return a valid Host header (e.g., ensure it's a non-empty ASCII string). For example, in a Node.js app: `res.setHeader('Host', req.headers.host || 'example.com');`** (90% success)
   ```
   Fix the upstream application to return a valid Host header (e.g., ensure it's a non-empty ASCII string). For example, in a Node.js app: `res.setHeader('Host', req.headers.host || 'example.com');`
   ```
2. **Use proxy_pass with a specific IP and set the Host header manually in nginx: `proxy_set_header Host $proxy_host;`** (70% success)
   ```
   Use proxy_pass with a specific IP and set the Host header manually in nginx: `proxy_set_header Host $proxy_host;`
   ```
3. **Inspect upstream logs to see the exact Host header being sent and sanitize it in the upstream application code.** (80% success)
   ```
   Inspect upstream logs to see the exact Host header being sent and sanitize it in the upstream application code.
   ```

## Dead Ends

- **** — This controls the request header sent to upstream, not the response header from upstream. (80% fail)
- **** — The issue is often in the upstream application logic that generates the response header. (60% fail)
- **** — proxy_ignore_headers does not apply to the Host header; it applies to other headers like Cache-Control. (90% fail)
