502 Bad Gateway: the server behind the proxy failed

A proxy or CDN got no valid answer from the origin server.

Is a website down for everyone or just you?

What 502 Bad Gateway means

Many sites have two layers: a front server (a proxy like Nginx, a load balancer or a CDN like Cloudflare) and the application behind it. 502 Bad Gateway means the front server is fine, but the answer it got from the server behind was invalid or the connection failed. The problem is on the website's side, not yours.

Related errors

  • 503 Service Unavailable: the server is overloaded or in maintenance.
  • 504 Gateway Timeout: the server behind took too long to answer.
  • Cloudflare 520–526: the origin server failed in a specific way (521 = down, 522 = timeout, 525/526 = SSL problem between Cloudflare and the origin).

Common causes (site owners)

  • The application crashed or is restarting: PHP-FPM, Node, Gunicorn or a container that is not running.
  • The proxy points to the wrong port or socket after a change.
  • The app runs out of memory or workers under load.
  • A firewall blocks the CDN's IP addresses from reaching the origin.
  • The response headers are too big for the proxy buffer.

How to fix it

  1. Check whether the site is down for everyone with the tool above.
  2. Look at the proxy's error log (for example /var/log/nginx/error.log): it says why the upstream failed ("connection refused", "upstream prematurely closed").
  3. Restart the application service and check its own logs for the crash.
  4. If you use Cloudflare, check that the origin answers directly and that its certificate is valid.

Visitors can only wait and reload in a minute: the fix is on the server.

Related errors

Last updated: