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.

Description

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

When You See It

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

How to Fix

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

Description

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

When You See It

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

How to Fix

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

Key Differences

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.

When to Use Which

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.

Learn More