502 Bad Gateway

Updated 11 October 2026

Short answer

Most websites have two layers: a front server (Nginx, a load balancer, or Cloudflare) that receives visitors, and your app behind it. A 502 means the front server reached out to your app and got back nothing usable. Almost always your app has crashed, stopped, or isn't listening where the front server expects. Restart the app and read its logs to see why it stopped.

What's the problem

Visitors see "502 Bad Gateway", sometimes with "nginx" or "Cloudflare" written under it. It often appears suddenly after a deploy, or comes and goes under load.

Why it happens

  • The app process has crashed or stopped. Out of memory, an unhandled error at startup, or a server restart where the app didn't come back.
  • The app is listening on a different port than the front server is sending traffic to, often after a config change.
  • The app is still starting up after a deploy, and the front server gave up waiting.
  • The app is overloaded and dropping connections.
  • A firewall between the two layers is blocking the connection.

How to fix it

  1. Check the app is running. On a VPS: systemctl status yourapp or pm2 list. On a platform like Heroku, Render or Railway: check its dashboard for crashed processes.
  2. Read the app's own logs, not just the front server's. The crash reason is there.
  3. Check the port. The port your app listens on must match the one in your Nginx proxy_pass or load balancer settings.
  4. Restart the app to get the site back, then fix the cause so it doesn't happen again.
  5. If it started after a deploy, roll back and look at what changed.
  6. If it only happens under load, the app is running out of memory or workers. See server overloaded.

What Preventionlabs checks

One click runs the full report on your domain. For this problem it shows whether your site loads, how fast, and where its redirects go; up to 50 pages from your sitemap, listing any that fail or are slow, plus broken links on them. With Cloudflare connected, it also shows which pages returned server errors (5xx) to visitors, and when; which requests the firewall blocked, by Cloudflare's own rule names; recent deploys and account changes, with their times.

Run your free report

Your first report is free, no card needed. Then A$50 a month, up to 10 domains. Read-only: it never changes anything.

Sources

  1. MDN Web Docs: 502 Bad GatewayreferenceThe HTTP 502 Bad Gateway server error response status code indicates that a server was acting as a gateway or proxy and that it received an invalid response from the upstream server.
← All of Error codes