Skip to main content

HTTP · 6 common causes

502 Bad Gateway

Updated

A proxy in front of your application received an invalid or empty response from it.

The exact message

$ curl -i https://app.example.com/api/orders
HTTP/1.1 502 Bad Gateway
server: nginx

<html><head><title>502 Bad Gateway</title></head>
<body><center><h1>502 Bad Gateway</h1></center></body></html>

# nginx error.log
connect() failed (111: Connection refused) while connecting to upstream

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is 502 Bad Gateway.

How to fix it

Cause 1 of 6

The upstream application crashed or never started.

Read the application's logs, not the proxy's: docker compose logs <service>, journalctl -u <service>, or the platform's log view. An app that exits at boot — a missing environment variable, a failed database connection — produces a 502 on every single request, which is the pattern to look for.

Cause 2 of 6

The proxy points at the wrong port or address.

Match the proxy_pass target, or the load balancer's target group, to the port the application actually listens on. An app bound to 127.0.0.1 is unreachable from another container or host — bind 0.0.0.0 when the proxy is elsewhere.

Cause 3 of 6

The upstream closes the connection mid-response.

nginx logs 'upstream prematurely closed connection'. That usually means a worker died during the request — an out-of-memory kill or an unhandled exception. The application log will show the crash; the proxy only saw the socket close.

Cause 4 of 6

Health checks are failing so no healthy targets remain.

Request the health path directly from inside the network and confirm it returns 200 within the configured timeout. A health endpoint that touches the database will fail the whole service the moment the database is slow, so keep it cheap.

Cause 5 of 6

A deploy is in progress and every instance is restarting at once.

Roll deploys instead of replacing all instances simultaneously, and give the platform a readiness check so traffic only arrives once an instance can serve it.

Cause 6 of 6

Response headers exceed the proxy's buffer.

A large cookie or a long JWT can overflow nginx's default buffer, producing intermittent 502s for signed-in users only. Raising proxy_buffer_size and proxy_buffers fixes it; shrinking what you put in cookies fixes it better.

Stopping it coming back

Give the application a cheap readiness endpoint, alert on upstream connection errors rather than on the 502 count alone, and make sure a failed boot is loud in your logs.

502 Bad Gateway — common questions

What is the difference between 502 and 504?
502 means the upstream answered with something invalid, or the connection failed outright. 504 means the upstream did not answer within the proxy's timeout. One points at a crash or misconfiguration, the other at slowness.
The 502 is intermittent. Where do I look?
At restarts and resource limits. A worker being OOM-killed, a process manager recycling workers, or a connection pool exhausted at peak all produce sporadic 502s. Correlate the timestamps with deploys and memory graphs before changing proxy settings.
Can a 502 be my code's fault if the proxy generated it?
Yes — that is the usual case. The proxy only reports what it saw. Unless the proxy config is wrong, the fault is upstream, so start with the application logs every time.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor