Skip to content
Web Hosting

Fix "502 Bad Gateway" on Your Website

502 Bad Gatewaynginxupstream prematurely closed
8 min read Updated 12 June 2026 ESAGAMES Team

A 502 means your web server (nginx) reached the backend (PHP-FPM, an app server) but got an invalid or no response. The web server itself is fine — the thing behind it crashed, timed out, or is misconfigured. Here is how to fix it.

Cause 1: PHP-FPM is down

The most common cause. If the PHP-FPM service crashed or isn't running, nginx has nothing to talk to. Restart it and check the logs:

systemctl status php-fpm     # or php8.x-fpm
systemctl restart php-fpm
tail -50 /var/log/nginx/error.log

Cause 2: the backend timed out

A slow script (or a long DB query) that exceeds the proxy timeout returns a 502. The nginx error log shows "upstream timed out" or "prematurely closed". Fix the slow request, or raise the timeout if the work is legitimately long.

Cause 3: wrong socket / port

If nginx points to a PHP-FPM socket or port that doesn't match FPM's config, every request 502s. Make sure fastcgi_pass matches FPM's listen directive.

502 vs 504

502 = the backend gave a bad/no response (often crashed). 504 = the backend took too long (a timeout). They point at different fixes — see our 504 guide.

The clean checklist

  1. Check the nginx error log — it names the real cause.
  2. Restart PHP-FPM (or your app backend) and confirm it's running.
  3. Look for a crashed backend (out of memory? see the OOM killer).
  4. Verify fastcgi_pass matches FPM's listen socket/port.
502 = the backend failed. Restart PHP-FPM, read the nginx error log, and fix the crashed or mismatched backend.

Cause 4: the backend ran out of memory

If PHP-FPM workers (or your app process) get killed by the Linux out-of-memory killer, nginx immediately sees a dead connection and returns 502. Check dmesg/syslog for OOM kill messages — see the OOM killer guide. Fixing memory limits or the underlying leak stops the crash.

Cause 5: a bad deploy or fatal PHP error

A code deploy with a syntax error or a fatal error on every request crashes PHP-FPM workers repeatedly. Check the PHP-FPM error log — not just nginx's — for the actual fatal error, and roll back or fix the offending code.

How to prevent 502s

  • Monitor PHP-FPM and auto-restart it on failure (a supervisor or systemd Restart=on-failure).
  • Set realistic pm.max_children for your RAM so workers don't get OOM-killed.
  • Test deploys on staging before production.
  • Keep an eye on the error log so a bad release is caught in minutes, not hours.

Related errors

A 502 (bad/no response) is not the same as a 504 Gateway Timeout (too slow) or a 500 Internal Server Error (the app itself threw an error and still responded). If the database is the actual culprit, see MySQL/MariaDB won't start.

Web hosting that stays up

Our managed web hosting runs and monitors PHP-FPM for you on NVMe, with DDoS protection included.

See web hosting
FAQ

Frequently asked questions

What does 502 Bad Gateway mean?

Nginx reached the backend (PHP-FPM or an app server) but got an invalid or empty response — usually because the backend crashed, timed out, or is misconfigured. The web server itself is fine.

How do I fix a 502 error?

Check and restart PHP-FPM (or your app backend), read the nginx error log for the real reason, make sure the backend isn't out of memory, and confirm nginx's fastcgi_pass matches FPM's listen socket.

What's the difference between 502 and 504?

502 means the backend returned a bad or no response (often crashed); 504 means it took too long and timed out. They have different fixes — a crash vs a slow request.

Can a database crash cause a 502 error?

Indirectly, yes — if PHP-FPM workers hang or crash while waiting on a dead database connection, nginx sees no valid response and returns 502. Check that MySQL/MariaDB is running — see MySQL/MariaDB won't start.

Why do I get 502 only right after deploying new code?

A fatal PHP error in the new code (a syntax error, a missing file, an unhandled exception) can crash every PHP-FPM worker that touches it. Check the PHP-FPM error log and roll back if needed.

Does restarting the server fix a 502?

It can temporarily, by restarting PHP-FPM along with everything else, but if the underlying cause — a crash-inducing bug, low memory, a wrong socket path — is still there, the 502 will return. Fix the root cause, not just the symptom.

Knowledge base

Related articles

Skip the troubleshooting

Managed, protected hosting in Frankfurt — we handle the Linux, the network and the DDoS so you don't have to.

Payments Secure checkout with cards, banking apps and digital wallets.

Choose the payment flow that fits your stack and region without leaving the platform.

Pay by Zen Visa Mastercard Paysafecard PaysafeCash Skrill Trustly Bancontact UnionPay iDeal WebMoney