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_passorfastcgi_passpoints at the wrong port, host or socket path. - PHP-FPM is down or saturated: all
pm.max_childrenworkers 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.1inside its container, so the proxy can't reach it; it must bind0.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 fixproxy_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; setlisten.owner/listen.groupin 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 anddmesgfor the OOM killer.upstream sent too big header: raiseproxy_buffer_sizeandproxy_buffers(orfastcgi_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.
Related codes
- 504 Gateway Timeout: the upstream answered too slowly rather than badly.
- 503 Service Unavailable: deliberately unavailable, with
Retry-After. - 500 Internal Server Error: the application itself reported the failure.
- 520 and 521: Cloudflare's codes for a broken or refused origin.
Check what a URL returns with the Redirect Checker, or send a request straight to the backend with the HTTP Request Builder.