nginx protocol_error ai_generated true

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

upstream sent invalid host header while connecting to upstream

ID: nginx/invalid-host-header-upstream

其他格式: JSON · Markdown 中文 · English
85%修复率
82%置信度
1证据数
2024-03-10首次发现

版本兼容性

版本状态引入弃用备注
nginx 1.24.0 active — — —
nginx 1.22.1 active — — —
nginx 1.20.2 active — — —

根因分析

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

English

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.

generic

官方文档

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

解决方案

  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.

无效尝试

常见但无效的做法:

  1. 80% 失败

    This controls the request header sent to upstream, not the response header from upstream.

  2. 60% 失败

    The issue is often in the upstream application logic that generates the response header.

  3. 90% 失败

    proxy_ignore_headers does not apply to the Host header; it applies to other headers like Cache-Control.