HTTP status code lookup

502 Bad Gateway: What It Means and How to Fix It

A 502 Bad Gateway error comes from a server that sits in front of your application, such as nginx, a load balancer or a CDN like Cloudflare. It forwarded the request to the server behind it (the upstream) and got back no response or an invalid one.

The proxy itself is working; the problem is almost always the application behind it, or the connection between the two.

502

Bad Gateway

A server acting as a gateway or proxy got an invalid response from the server behind it.

Look up any other status code →

Common causes

  • The application crashed, is restarting, or was never started.
  • The proxy points at the wrong host or port, for example proxy_pass http://127.0.0.1:3000 while the app listens on 8080, or on a different network interface inside a container.
  • The application closed the connection mid-response, often because it ran out of memory and was killed.
  • Response headers are larger than the proxy's buffer, which happens with very large cookies.
  • A firewall or security group blocks traffic from the proxy to the application.

Fixing a 502 behind nginx

Start with the nginx error log, which states the exact reason:

sudo tail -n 50 /var/log/nginx/error.log

# connect() failed (111: Connection refused) while connecting to upstream
#   → nothing listens on the upstream address: start the app or fix proxy_pass
# upstream prematurely closed connection while reading response header
#   → the app crashed or closed the connection: check its logs
# upstream sent too big header while reading response header
#   → raise proxy_buffer_size and proxy_buffers

Fixing a 502 on Cloudflare, AWS and other hosts

  • Cloudflare: if the error page shows Cloudflare's branding, Cloudflare couldn't get a valid response from your origin server. Check that the origin is up and reachable on the expected port. Cloudflare's own 520 to 527 codes narrow the cause further.
  • AWS Application Load Balancer: the target closed the connection or sent a malformed response. Check the target group health checks and make sure the app's keep-alive timeout is longer than the load balancer's idle timeout.
  • Docker and Kubernetes: the app must listen on 0.0.0.0, not 127.0.0.1, or it can't be reached from outside its container.

If you're visiting a site that shows 502

The problem is on the website's side. Reload after a minute, since many 502s happen during a deploy or restart. If it continues, the site's owners need to fix their server.

Frequently asked questions

What is the difference between 502 and 504?

Both come from a proxy. 502 means the upstream sent a bad response or refused the connection; 504 means it didn't answer in time.

What does "The proxy server received an invalid response from an upstream server" mean?

It's Apache's wording for a 502 when it runs as a reverse proxy with mod_proxy. The application behind it crashed, closed the connection or sent something that isn't valid HTTP. Look in Apache's error log for the proxy: line with the reason, then in the application's own logs.

What is the difference between 500 and 502?

A 500 comes from the application itself, which hit an error. A 502 comes from a proxy in front of the application, which couldn't get a usable response from it.

Can a 502 be cached?

Browsers don't normally cache a 502. CDNs may cache errors briefly if configured to, so purge the cache after fixing the origin if the error persists.

More http status code lookup guides

Open the full http status code lookup →

From the blog