Skip to content
Webmaster Tools

502 Bad Gateway

What HTTP 502 Bad Gateway means, why nginx, Apache and load balancers return it when the upstream app or PHP-FPM fails, and how to read the error log to fix it.

All status codes

What 502 means

A server acting as a gateway or proxy tried to pass the request on to the server behind it (the upstream) and got an invalid response, or none at all. 502 is defined in RFC 9110 §15.6.3. The proxy itself is working; the application behind it isn't answering properly.

HTTP/1.1 502 Bad Gateway
Server: nginx
Content-Type: text/html

<html><head><title>502 Bad Gateway</title></head>...

The Server header tells you which layer generated the error: nginx, cloudflare, awselb/2.0, and so on. That's the layer whose logs to read first.

Common causes

  • The application isn't running: it crashed, is restarting, or never started after a deploy.
  • Wrong upstream address: proxy_pass or fastcgi_pass points at the wrong port, host or socket path.
  • PHP-FPM is down or saturated: all pm.max_children workers are busy, or the socket file doesn't exist or has the wrong owner.
  • The upstream closed the connection mid-response, often a worker killed for using too much memory.
  • Response headers too large for the proxy's buffers (big cookies, long redirect URLs).
  • Containers: the app listens on 127.0.0.1 inside its container, so the proxy can't reach it; it must bind 0.0.0.0.

How to fix it

nginx. The error log says exactly which case you're in:

  • connect() failed (111: Connection refused) while connecting to upstream: nothing listens on that address. Start the app or fix proxy_pass.
  • connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory): wrong socket path or PHP-FPM is stopped. (13: Permission denied) means nginx's user can't open the socket; set listen.owner / listen.group in the FPM pool.
  • upstream prematurely closed connection while reading response header from upstream: the app crashed or was killed mid-request. Check its own log and dmesg for the OOM killer.
  • upstream sent too big header: raise proxy_buffer_size and proxy_buffers (or fastcgi_buffer_size / fastcgi_buffers).

For PHP-FPM under load, look for server reached pm.max_children setting in the FPM log and raise the limit only if memory allows.

Apache with mod_proxy. Look for AH01102: error reading status line from remote server or AH00957: ... attempt to connect ... failed in the error log; the same causes apply.

Load balancers and Kubernetes. A 502 often means no healthy target: check health checks, readiness probes and that the target port matches the container port. An application keep-alive timeout shorter than the load balancer's idle timeout causes intermittent 502s; make the app's longer.

502 and SEO

A 502 is a server error to Google. Brief ones are harmless, but persistent 5xx responses make Googlebot crawl the site more slowly, and URLs that keep failing are eventually dropped from the index. Fix crash loops quickly, and serve 503 during planned work instead.

Check what a URL returns with the Redirect Checker, or send a request straight to the backend with the HTTP Request Builder.