# 在从上游读取响应头时，上游连接被对端重置

- **ID:** `nginx/upstream-connection-reset-by-peer`
- **领域:** nginx
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

上游服务器在发送完整响应之前强制关闭TCP连接（RST包），通常由应用程序崩溃或超时引起。

## 版本兼容性

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

## 解决方案

1. ```
   检查上游应用程序日志中的崩溃或超时；修复应用程序以优雅处理请求
   ```
2. ```
   使用proxy_next_upstream添加重试逻辑：proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
   ```
3. ```
   增加上游keepalive设置以减少连接变更：keepalive 32;
   ```

## 无效尝试

- **** — Connection timeout only affects initial handshake, not response reading; RST occurs after connection is established. (80% 失败率)
- **** — HTTP version change may affect keepalive but doesn't prevent RST from upstream. (70% 失败率)
- **** — This disables keepalive but upstream may still send RST if it crashes. (65% 失败率)
