# 502 错误网关：上游连接被对端重置

- **ID:** `api/http-502-bad-gateway-upstream-connection-reset`
- **领域:** api
- **类别:** network_error
- **错误码:** `502`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

上游服务器（例如应用程序后端）在代理（例如 NGINX、ALB）尝试转发请求时突然关闭了 TCP 连接，通常是由于崩溃、超时或防火墙规则。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| NGINX 1.24+ | active | — | — |
| AWS ALB 2023+ | active | — | — |
| HAProxy 2.8+ | active | — | — |
| Gunicorn 21+ | active | — | — |
| uWSGI 2.0+ | active | — | — |

## 解决方案

1. ```
   Check upstream server logs for application crashes or resource exhaustion. For example, in a Python Gunicorn setup, look for 'Worker failed to boot' or 'Out of memory'. Increase the number of workers or memory limits. Example command to add more workers:
gunicorn --workers=4 --timeout=120 myapp:app
   ```
2. ```
   Verify firewall rules between proxy and upstream. On Linux, use iptables to check for dropped packets:
iptables -L -n -v | grep DROP
If rules are blocking, add an allow rule for the proxy IP on the upstream's port (e.g., port 8080).
   ```

## 无效尝试

- **** — The reset is immediate, not due to a slow response. (80% 失败率)
- **** — The upstream server is the root cause. (90% 失败率)
