# 上游在读取响应头时发送了HTTP/2.0响应

- **ID:** `nginx/upstream-sent-http-2-0-response-while-reading-response-header-from-upstream`
- **领域:** nginx
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

上游服务器以HTTP/2（HTTP/2.0）响应，但nginx配置为使用HTTP/1.x进行代理，导致协议不匹配。

## 版本兼容性

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

## 解决方案

1. ```
   Configure nginx to proxy using HTTP/2 by setting proxy_http_version 2.0; (requires nginx compiled with HTTP/2 support and ngx_http_v2_module):

location / {
    proxy_http_version 2.0;
    proxy_pass https://upstream;  # Note: HTTPS required for HTTP/2
}
   ```
2. ```
   If the upstream is an HTTP/2 server, configure it to also support HTTP/1.1. For example, in a Node.js server using http2 module:

const http2 = require('http2');
const http = require('http');

const server = http2.createServer();
server.on('stream', (stream, headers) => {
    stream.respond({ ':status': 200 });
    stream.end('OK');
});
server.listen(8080);

Add an HTTP/1.1 listener on a different port if needed.
   ```
3. ```
   Use a load balancer like HAProxy that can convert HTTP/2 to HTTP/1.1:

frontend http-in
    bind *:80
    default_backend servers

backend servers
    server upstream1 10.0.0.1:8080 proto h2

HAProxy can terminate HTTP/2 and forward as HTTP/1.1 to nginx.
   ```

## 无效尝试

- **** — If the upstream only supports HTTP/2, forcing HTTP/1.1 will not change the upstream's response; the upstream will still send HTTP/2 responses. (90% 失败率)
- **** — Buffer size does not affect protocol version negotiation. (95% 失败率)
- **** — These headers are for WebSocket upgrades, not HTTP version negotiation. (85% 失败率)
