api network_error ai_generated partial

502 坏网关:上游超时

502 Bad Gateway: Upstream timeout

ID: api/http-502-bad-gateway-upstream-timeout

其他格式: JSON · Markdown 中文 · English
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.

generic

官方文档

https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout

解决方案

  1. 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.
  2. Optimize the upstream server's response time by adding database indexes, caching, or scaling horizontally. Use a CDN or caching layer for static responses.
  3. 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)

无效尝试

常见但无效的做法:

  1. 70% 失败

    The timeout is on the proxy side, not the client. The proxy's timeout configuration must be adjusted.

  2. 60% 失败

    The upstream server may be slow due to load or inefficient queries, not a crash.

  3. 90% 失败

    The upstream server is still under load; immediate retries will likely timeout again.