nginx 502 vs 504
502 Bad Gateway: The server was acting as a gateway or proxy and received an invalid response from the upstream server.
504: he server was acting as a gateway or proxy and did not receive a timely response from the upstream server.
Conclusion
504 是 nginx 没有及时从上游服务获取响应,超时了:
- 上游服务响应慢,读取 response / 发送 request 超时(
upstream timed out (110: Operation timed out) **while** reading response header from upstream
)- 某些请求处理就是慢。此时就应该调大 proxy_read_timeout (默认 60s)
- 上游服务压力太大,响应变慢。此时可以增加上游服务的响应能力,也可以适当提升 proxy_send_timeout, proxy_read_timeout
- 连接上游服务超时。可能是上游服务已经断了,但由于 keepalive,nginx 依然保有 tcp 连接,但实际操作时,却连不上,就超时了。
502 是一般是上游服务器故障导致的。比如停机,进程被杀死,上游服务 reset 了连接,进程僵死等各种原因。在 nginx 的日志中我们能够发现 502 错误的具体原因,分别为:104: Connection reset by peer
,113: Host is unreachable
,111: Connection refused