HTTP

502 Bad Gateway

A proxy/load-balancer got an invalid (or no) response from the upstream app server.

What it looks like

502 Bad Gateway
nginx

[error] connect() failed (111: Connection refused) while connecting to upstream

What does "502 Bad Gateway" mean?

Returned by a reverse proxy, load balancer or CDN — not by your app — when it could not get a valid response from the app server behind it. The proxy’s own error log usually names the upstream address it failed to reach.

What causes "502 Bad Gateway"?

  • The app/backend crashed or isn't running.
  • Wrong upstream host/port in the proxy.
  • The app took too long and returned garbage.

How do I confirm the cause?

  1. Read the proxy error log: “connection refused” means the app is not listening, “upstream prematurely closed” means it crashed mid-request.
  2. Check the app process is running and listening on the host and port the proxy is configured to use.
  3. If it happens only on some requests, look for the app crashing or running out of memory on those specific requests.

How do I fix "502 Bad Gateway"?

  • Check the upstream app is running and healthy.
  • Verify the proxy's upstream address/port.
  • Look at both proxy and app logs.

How do I stop it happening again?

  • Run the app under a supervisor that restarts it, with health checks the proxy can use.
  • Keep the proxy’s upstream address in the same config as the app’s listen address.
Got a different error? Paste it into the Explain This Error tool → Identifies the family and the fix — runs locally, nothing uploaded.

Related errors