Fix "502 Bad Gateway" on Your Website
502 Bad Gatewaynginxupstream prematurely closedA 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 = 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
- Check the nginx error log — it names the real cause.
- Restart PHP-FPM (or your app backend) and confirm it's running.
- Look for a crashed backend (out of memory? see the OOM killer).
- Verify
fastcgi_passmatches 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.
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.
Go deeper on this
Guides and explainers that pair with this fix — from our guides and blog.
How to Speed Up a WordPress Site
Caching, images, PHP version and the hosting underneath.
ReadHow to Secure a WordPress Site
Updates, passwords, plugins and backups that actually matter.
ReadHow to Choose a Web Host
What to look for beyond the headline price.
ReadcPanel vs DirectAdmin vs Plesk
Which control panel does what, and what it costs.
ReadRelated articles
Fix "500 Internal Server Error" on Your Website
A 500 error on your site? The usual causes — .htaccess, file permissions, PHP — and how to fix them.
Read fix Web HostingHow to Point a Domain to Your Server (DNS A Record)
How to point a domain at your server with an A record — and why it doesn't work instantly.
Read fix Web HostingFix Email Going to Spam (SPF, DKIM & DMARC Explained)
Mail landing in spam? Set SPF, DKIM and DMARC correctly so your email reaches the inbox.
Read fix