HTTP

HTTP 502 Bad Gateway vs 504 Gateway Timeout

Both 502 and 504 are gateway errors generated by an intermediary proxy, but they describe different failures. A 502 means the proxy received an invalid response from upstream, while a 504 means the proxy received no response at all within its timeout window.

説明

The server, acting as a gateway or proxy, received an invalid response from the upstream server.

このコードが表示される場合

When Nginx/Apache can't reach the application server (e.g., Gunicorn is down, upstream timeout).

修正方法

Check if the upstream server is running. Verify proxy configuration. Check for upstream timeouts.

説明

The server, acting as a gateway or proxy, did not receive a timely response from the upstream server.

このコードが表示される場合

When an upstream server is too slow to respond within the proxy's timeout window.

修正方法

Increase proxy timeout settings. Optimize upstream server performance. Check for long-running queries.

主な違い

1.

502 means the upstream responded but the response was invalid — a malformed header, a connection reset, or garbage data.

2.

504 means the upstream did not respond in time — the proxy's timeout expired before any response arrived.

3.

502 often indicates the upstream server crashed mid-response; 504 indicates it is too slow or unreachable.

4.

For 502, check if the upstream process is running; for 504, check for slow queries or network latency.

5.

Increasing the proxy timeout may fix a 504 but will never fix a 502.

使い分け方

A 502 is generated automatically by the proxy when the upstream server returns a bad response — you do not return it manually. A 504 is generated when the upstream is too slow. To fix 502, ensure the upstream process is running and healthy. To fix 504, optimize slow endpoints, increase proxy timeouts, or move long-running work to background jobs.

詳細を見る