# 上游服务器发送了无效的 Host 头部

- **ID:** `nginx/invalid-host-header-upstream`
- **领域:** nginx
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

上游服务器返回了无效或格式错误的 Host 头部（例如空值、非 ASCII 字符或过长），导致 nginx 在处理响应时拒绝。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| nginx 1.24.0 | active | — | — |
| nginx 1.22.1 | active | — | — |
| nginx 1.20.2 | active | — | — |

## 解决方案

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');`
   ```
2. ```
   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.
   ```

## 无效尝试

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