How to Fix 502 Bad Gateway Error
What the 502 Bad Gateway error means
HTTP status 502 means "bad gateway." It happens when one server acts as a gateway — a reverse proxy, a load balancer, or a CDN like Cloudflare — and the server behind it (your origin server, PHP-FPM, or an application server) responds with something invalid, or does not respond at all. Picture a receptionist who calls the back office and gets a dead line: the receptionist can only shrug at you.
That chain is why 502s feel random. The page was fine a minute ago, and it may be fine again in five minutes — a crashed PHP process, a server reboot, or a brief network blip between Cloudflare and your host is enough. Visitors can usually just wait it out; owners need to find which link in the chain broke.
Common causes
- Crashed or overloaded PHP-FPM / application server: the backend process that builds pages died or is too busy to answer the gateway in time.
- Server reboot or restart in progress: during updates or restarts the backend is briefly unreachable, and the gateway reports 502.
- Firewall blocking the gateway: a firewall rule blocks the proxy's requests to the backend, so they never get answered.
- DNS or network issues between servers: the gateway cannot resolve or reach the origin server's address.
- Cloudflare or CDN misconfiguration: wrong origin IP, an expired SSL setting, or a paused service breaks the gateway-to-origin path.
How to fix the 502 Bad Gateway error — step by step
Steps 1 and 2 are for visitors. Steps 3 through 5 are for site owners.
- Refresh and wait a few minutes. Press Ctrl + R (or Cmd + R on a Mac) — many 502s last only seconds while a server process restarts. If it persists, wait 3 to 5 minutes and try again; do not hammer refresh, since rapid retries add load to the server that is already struggling. Open the site in an incognito window (Ctrl + Shift + N) to rule out a stale cached copy of the error page on your side.
- Clear your browser cache. A cached 502 page can keep showing the error after the site has recovered. Press Ctrl + Shift + Delete, set the time range to All time, check Cached images and files, and click Clear data. Then close and reopen the tab and load the site fresh. Also try a different browser or your phone's mobile data — if the site works there, the problem is local caching or your network, not the site.
- Site owner: restart PHP-FPM and the web server. On a VPS or dedicated server, connect via SSH and restart the backend: sudo systemctl restart php8.2-fpm (match your PHP version — check with php -v) followed by sudo systemctl restart nginx or sudo systemctl restart apache2. Then check that the services are actually running with sudo systemctl status php8.2-fpm. On shared hosting you cannot restart services yourself — use the control panel's PHP or service manager if one exists, or move to step 5 and ask your host.
- Site owner: check the firewall and server load. A firewall can silently drop the gateway's requests. Review your firewall rules (ufw status, or your host's firewall panel) and make sure the proxy or CDN IP ranges are allowed through to your web ports. Check server load with top or htop — if CPU or memory is pegged at 100%, find the runaway process before restarting anything, or the 502 will return within minutes. Also verify there is free disk space with df -h; a full disk stops PHP-FPM from starting.
- Site owner: check Cloudflare and host status. If you use Cloudflare, open its status page and your Cloudflare dashboard: confirm the DNS A record points at the correct origin IP, and try pausing Cloudflare (Overview, then Advanced Actions, then Pause Cloudflare) to test whether the origin works directly. Check your hosting provider's status page for ongoing incidents. If everything looks right on your end, open a support ticket with the exact time the 502s started and a screenshot — the fault may be in the host's network between their edge and your server.
Did this fix it? Quick checklist
- The site loads without the 502 error
- It works in an incognito window and on another network
- PHP-FPM and the web server are running
- No firewall rule blocks the gateway or CDN
If 502s come and go at the same time every day, suspect a scheduled task: backups, cron jobs, or traffic spikes overwhelming the backend. Stagger heavy cron jobs away from peak hours and consider a higher PHP-FPM process limit (pm.max_children) if the pool is constantly exhausted. Persistent 502s right after a deploy usually mean the new code crashes on boot — roll back and check the application logs.
Frequently asked questions
What does a 502 Bad Gateway error mean?
It means a gateway server (like a proxy, load balancer, or Cloudflare) received an invalid response — or no response — from the upstream server that was supposed to provide the page. The chain between servers broke somewhere.
Is a 502 error my fault as a visitor?
No. A 502 is a server-side problem between the site's servers. Refreshing, clearing your cache, and waiting a few minutes are the only useful visitor actions — the actual fix has to happen on the site's infrastructure.
Why does a site show 502 only sometimes?
Because the cause is often transient: a PHP process crashing and restarting, a brief server reboot, a traffic spike, or a momentary network blip between the CDN and the origin server. If it is frequent, the owner should investigate backend stability.
How do I fix 502 errors on my own server?
Restart PHP-FPM and your web server, check that they stay running, review firewall rules for blocked proxy traffic, verify disk space and server load, and confirm your CDN points at the correct origin IP. The application and server error logs usually name the failing component.