HTTP

HTTP 500 Internal Server Error vs 502 Bad Gateway

Both 500 and 502 indicate server-side failures, but they originate from different layers. A 500 means the origin server itself crashed or encountered an unhandled error, while a 502 means an intermediary (reverse proxy, load balancer) received an invalid response from the upstream server.

説明

The server encountered an unexpected condition that prevented it from fulfilling the request. A generic catch-all for server-side errors.

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

When an unhandled exception occurs, a database connection fails, or server code has a bug.

修正方法

Check server logs for the stack trace. Common causes: unhandled exceptions, database errors, misconfigurations.

説明

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.

主な違い

1.

500 originates from the application server — an unhandled exception, a bug, or a misconfiguration.

2.

502 originates from a proxy or gateway — Nginx, a load balancer, or a CDN could not get a valid response from upstream.

3.

500 usually requires checking application logs for stack traces.

4.

502 usually requires checking whether the upstream server (e.g., Gunicorn) is running and reachable.

5.

A 502 often means the upstream server crashed, returned garbage, or closed the connection unexpectedly.

使い分け方

Return 500 when your application code encounters an unrecoverable error such as an unhandled exception or database failure. A 502 is typically generated by a reverse proxy (Nginx, HAProxy) when the upstream application server is unreachable or returns a malformed response. Developers should check the proxy error logs for 502 and the app logs for 500.

詳細を見る