DevTool Desk

5xx Server error

HTTP 502 Bad Gateway

A gateway or proxy received an invalid response from the upstream server.

A 502 Bad Gateway means a proxy, load balancer, or CDN got an invalid response from the server behind it. The gateway is working, but the upstream app it talks to isn't responding properly.

Common causes of a 502 error

  • The backend application crashed, was restarted, or isn't running.
  • The proxy is pointed at the wrong host or port, or a firewall blocks the connection.
  • The backend closed the connection early or sent a malformed response.
  • The backend ran out of workers or memory under load (for example all PHP-FPM or Node workers busy).
  • A DNS or SSL problem between the proxy and the upstream.

How to fix a 502 error

  1. Check that the backend process is running and listening on the port the proxy expects.
  2. Read the proxy's error log (for nginx, error.log) next to the app's own logs.
  3. Verify the upstream host, port and protocol in the proxy config.
  4. Increase worker counts or memory if it happens under load, and restart the app.
  5. If you use a CDN, test the origin directly to see whether the problem is the origin or the CDN.

Frequently asked questions

What's the difference between 502 and 504?

502 means the upstream sent back an invalid response, or the connection failed. 504 means the upstream didn't respond in time. A crashed app usually causes 502; a slow one causes 504.

Why do I get 502 right after a deploy?

The new app version is probably still starting, or crashed on startup, so the proxy has nothing healthy to talk to. Check the app's startup logs and add a health check.

Other common status codes

See 502 in the full searchable list, or use the HTTP Header Checker to see the status code any URL returns.