Black Friday: 60% off yearly plans with code BF2026, until November 30. See the deal

Troubleshooting

502 Bad Gateway: what it means and how to fix it

A 502 Bad Gateway means a server acting as a gateway or proxy, such as nginx, a load balancer or a CDN, received an invalid response from the server behind it. The fault is almost always upstream of the proxy: a crashed or unreachable application server.

The short answer
  • The proxy is working. The server behind it sent nothing usable, or nothing at all.
  • Refresh once, then check whether the application process is actually running.
  • Visitors cannot fix a 502. Only the site owner or host can.
Try the website status checkerFree, live, no signup

How is 502 different from 503 and 504?

Gateway-related 5xx status codes
CodeMeaningTypical cause
502 Bad GatewayThe gateway received an invalid response from upstreamUpstream crashed, refused the connection or sent a malformed reply
503 Service UnavailableThe server is temporarily unable to handle the requestOverload or planned maintenance, often with a Retry-After header
504 Gateway TimeoutThe gateway did not get a response from upstream in timeSlow upstream: a long query, a hung process or a timeout set too low

Definitions follow RFC 9110, section 15.6.

What usually causes a 502?

Work from the application outward. The cause is nearly always one of these.

Application process is down

PHP-FPM, Node, Gunicorn or another app server crashed, was killed for memory or never restarted after a deploy.

Wrong upstream address

The proxy points at a socket or port nothing listens on, often after a config change or version upgrade.

Upstream closes the connection

The app server hits a worker limit or request limit and drops the connection mid-response.

Upstream hostname fails to resolve

A proxy that resolves the upstream name once at startup keeps using a stale or missing address.

TLS mismatch to the origin

The proxy speaks HTTPS to an origin that only serves HTTP, or the origin certificate is invalid.

CDN cannot reach the origin

A firewall or rate limit blocks the CDN's IP ranges, so every request through the CDN fails.

How do you fix a 502, in order?

  1. 01

    Reload once and test the origin directly

    Reload to rule out a blip. Then request the origin server by IP or its internal URL, bypassing the CDN or load balancer. If the origin works directly, the fault is in the proxy layer.

  2. 02

    Check the application process

    Confirm the app server is running, for example with systemctl status php8.3-fpm or your process manager. Restart it and watch whether it stays up.

  3. 03

    Read the proxy error log

    The log names the failure. A refused connection, a reset connection and a premature close each point to a different cause (see the log lines below).

  4. 04

    Match the upstream address to reality

    Compare the proxy_pass or fastcgi_pass target with the socket path or port the app server really listens on.

  5. 05

    Look at resources and limits

    Out-of-memory kills, exhausted worker pools and low request limits all make the app server drop connections under load.

  6. 06

    Check what changed

    A deploy, a package upgrade, a firewall rule or a CDN setting that changed shortly before the first 502 is the likeliest cause.

Which nginx error line means what?

nginx error.log: common upstream messages
connect() failed (111: Connection refused) while connecting to upstream
  -> nothing is listening at the upstream address: app server down or wrong port

upstream prematurely closed connection while reading response header from upstream
  -> the app server accepted the request, then died or closed the connection

recv() failed (104: Connection reset by peer) while reading response header from upstream
  -> the upstream reset the connection, often a crash or a limit being hit

connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory)
  -> the socket path is wrong or the service is not running

Upstream behaviour is configured with the directives in the nginx proxy module documentation.

A 502 only some of the time

Intermittent 502s usually mean the app server is crashing and restarting, or hitting a worker or memory limit under load. Check the application log and system memory at the time of the errors, and confirm the problem from more than one location, because one failing request tells you little. Aepto can check a site from several regions and alert you when it starts failing.

Common questions

Is a 502 Bad Gateway my fault as a visitor?

No. A 502 is a server-side error, so nothing on your device or connection causes it. Reload once, try again in a few minutes, and contact the site owner if it persists. Clearing cache rarely helps, because the failing request never reaches the page content.

Can a 502 error fix itself?

Yes, when it comes from a brief upstream restart or a deploy in progress. A 502 that lasts more than a few minutes points to an application server that is down or misconfigured and needs someone to fix it.

Does a 502 hurt SEO?

A short 502 does little harm, because Google retries later. Google has said 5xx errors that persist can cause pages to drop out of the index over time, so repeated or lasting 502s are worth fixing and monitoring.

What is the difference between a 502 and a 520 from Cloudflare?

A 502 means a gateway received an invalid upstream response. Cloudflare reports its own 52x codes, such as 520 for an unknown or empty origin response, so a Cloudflare error code points to the origin as well but gives more detail about how it failed.

How do I find out about a 502 before customers do?

Monitor the site with an uptime check that alerts you on any 5xx response, ideally from more than one region and at a short interval, so a failing origin is caught within minutes instead of through support tickets.

Know about a 502 before your customers do

Aepto checks your sites from up to six regions, as often as every minute on paid plans, and alerts you when one starts failing.