nginx 502 vs 504

nginx 502 和 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
  • 连接上游服务超时。可能是上游服务已经断了,但由于 keepalive,nginx 依然保有 tcp 连接,但实际操作时,却连不上,就超时了。

502 是一般是上游服务器故障导致的。比如停机,进程被杀死,上游服务 reset 了连接,进程僵死等各种原因。在 nginx 的日志中我们能够发现 502 错误的具体原因,分别为:104: Connection reset by peer113: Host is unreachable111: Connection refused