# 上游发送无效头部

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

## 根因

上游服务器返回了格式错误或无效的HTTP头部，nginx无法解析，通常由后端应用崩溃或配置错误引起。

## 版本兼容性

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

## 解决方案

1. ```
   Check upstream application logs for errors (e.g., PHP-FPM logs, uWSGI logs). Fix the application code that sends malformed headers.
   ```
2. ```
   Add a custom error page or proxy_intercept_errors on; for example:
server {
    proxy_intercept_errors on;
    error_page 502 /502.html;
}
This hides the error from clients but does not fix the root cause.
   ```
3. ```
   Configure nginx to ignore invalid headers by setting 'proxy_ignore_headers X-Accel-Redirect;' if the invalid header is non-critical, but prefer fixing upstream.
   ```

## 无效尝试

- **** — The error is not about buffer size but about header validity; increasing buffers masks the issue but backend still sends invalid data. (65% 失败率)
- **** — Restarting nginx does not fix the upstream application that generates invalid headers. (80% 失败率)
- **** — Disabling buffering may change behavior but does not correct invalid headers from upstream. (70% 失败率)
