api
network_error
ai_generated
partial
502 坏网关:上游超时
502 Bad Gateway: Upstream timeout
ID: api/http-502-bad-gateway-upstream-timeout
80%修复率
85%置信度
1证据数
2024-01-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Nginx 1.24 | active | — | — | — |
| AWS ALB | active | — | — | — |
| HAProxy 2.6 | active | — | — | — |
| Kong 3.4 | active | — | — | — |
| Istio 1.18 | active | — | — | — |
根因分析
代理或网关服务器(例如 Nginx、AWS ALB)未及时收到上游服务器的响应,导致超时。
English
The proxy or gateway server (e.g., Nginx, AWS ALB) did not receive a timely response from the upstream server, causing a timeout.
官方文档
https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout解决方案
-
Increase the proxy timeout value. For Nginx, add 'proxy_read_timeout 120s;' in the location block. For AWS ALB, increase the idle timeout setting in the target group to 120 seconds.
-
Optimize the upstream server's response time by adding database indexes, caching, or scaling horizontally. Use a CDN or caching layer for static responses.
-
Implement a retry mechanism with exponential backoff. Example in Python: for i in range(3): try: response = requests.get(url, timeout=30); break except requests.exceptions.Timeout: time.sleep(2**i)
无效尝试
常见但无效的做法:
-
70% 失败
The timeout is on the proxy side, not the client. The proxy's timeout configuration must be adjusted.
-
60% 失败
The upstream server may be slow due to load or inefficient queries, not a crash.
-
90% 失败
The upstream server is still under load; immediate retries will likely timeout again.