502 Bad Gateway is one of the most common website errors: you open a page and get a blank screen saying "502 Bad Gateway". Let's look at what it means, who is to blame — the browser, the site or the server — and how to fix it: first for a regular visitor, then for a site owner running a VPS.
What a 502 error means
Code 502 means that an intermediary server (a gateway or proxy) received an invalid response from the server behind it. The typical setup: Nginx accepts the request, while the page itself is built by an application — PHP-FPM, Node.js, Python (Gunicorn, uWSGI) or a Docker container. If the application crashes, is overloaded or doesn't respond the way Nginx expects, the visitor sees a 502.
Important: 502 is a server-side error, not a problem with your computer. But the browser cache, DNS or extensions can sometimes make it look worse.
What the error looks like
502 Bad Gatewaywithnginxat the bottom — the classic version;HTTP Error 502 — Bad Gatewayin Chrome and Edge;- a Cloudflare page "Bad gateway Error code 502" — the error is between Cloudflare and your server;
502 Proxy Error— when Apache with mod_proxy sits in front of the site.
What to do as a visitor
- Reload the page after 30–60 seconds (Ctrl+F5 or Cmd+Shift+R). A 502 is often a short glitch while the server restarts.
- Open the site in incognito mode or another browser to rule out cache and extensions.
- Clear the cache and cookies for that site.
- Flush the DNS cache: on Windows —
ipconfig /flushdns, on macOS —sudo dscacheutil -flushcache. - Turn off your VPN or proxy and try again: sometimes the error happens on an intermediate node.
- Check whether the site works for others with a service like downforeveryoneorjustme.com. If it's down for everyone, the problem is on the owner's side.
Server-side causes of a 502
| Cause | How it shows up |
|---|---|
| The backend (PHP-FPM, Node.js, Gunicorn) crashed or isn't running | 502 on every page at once |
| Out of memory, the process was killed by the OOM killer | 502 under load, gone after a restart |
PHP-FPM worker limit reached (pm.max_children) |
502 at peak hours, "server reached pm.max_children" in the log |
| Wrong backend address in the Nginx config (port, socket) | 502 right after changing settings |
| Response header buffers are too small | "upstream sent too big header" in error.log |
| A firewall or SELinux blocks Nginx from reaching the backend | "Permission denied" / "connection refused" in the log |
| Cloudflare can't reach the server | Cloudflare page with code 502 |
How to fix 502 Bad Gateway: step by step for site owners
Step 1. Check the Nginx log
Connect to the server over SSH and open the error log — the cause is almost always there:
sudo tail -n 50 /var/log/nginx/error.log
Typical lines: connect() failed (111: Connection refused) while connecting to upstream — the backend isn't running; upstream prematurely closed connection — the backend crashed mid-response; upstream sent too big header — you need bigger buffers.
Step 2. Check that the backend is running
sudo systemctl status php8.3-fpm
sudo systemctl restart php8.3-fpm
# Node.js under pm2:
pm2 status
pm2 restart all
# Docker:
docker ps -a
If the service won't start, check its own log: journalctl -u php8.3-fpm -n 50.
Step 3. Verify the backend address in the config
In the site block, the address in fastcgi_pass or proxy_pass must match where the application actually listens:
# PHP-FPM via socket
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
# application on a port
proxy_pass http://127.0.0.1:3000;
After editing, test the syntax and reload Nginx: sudo nginx -t && sudo systemctl reload nginx.
Step 4. Check memory and load
free -h
dmesg -T | grep -i "killed process"
htop
If the OOM killer is terminating processes or free memory is constantly at zero, the site lacks resources. Options: lower pm.max_children, add swap, optimize database queries — or move to a VPS plan with more RAM.
Step 5. Increase buffers and timeouts if needed
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffer_size 16k;
proxy_read_timeout 120s;
Step 6. If the site is behind Cloudflare
Check whether the site opens directly by the server's IP. If it works directly but not through Cloudflare, make sure the server firewall doesn't block Cloudflare's IP ranges and that the SSL mode in Cloudflare matches your server setup (Full / Full strict when a certificate is installed).
502 vs 500, 503, 504 and 524
| Code | Meaning | Where to look |
|---|---|---|
| 500 | An error inside the application itself | PHP / application logs |
| 502 | The gateway got an invalid response from the backend | Nginx error.log, backend status |
| 503 | Service temporarily unavailable: overload or maintenance | load, limits, maintenance mode |
| 504 | The backend didn't respond in time | timeouts, slow queries |
| 524 | Cloudflare didn't get a response within 100 seconds | long-running scripts, server load |
How to prevent 502 errors
- Enable automatic restarts:
Restart=alwaysin systemd, or pm2 for Node.js. - Set up uptime monitoring with Telegram alerts.
- Keep a memory margin — at least 20–30% free RAM under normal load.
- Test the config with
nginx -tbefore every reload. - For busy projects, choose a server with a high CPU clock: PHP and Node.js handle each request in a single thread, so core frequency directly affects response time.
If 502s are caused by a lack of resources, the simplest fix is to move the site to a server with headroom: PSB Hosting VPS with NVMe storage and unmetered traffic, or Hi-CPU VPS on Ryzen 9 7950X for heavy sites.
FAQ
Is a 502 error my problem or the website's?
Almost always the website's. If the site doesn't open in another browser or on your phone over mobile data, all you can do is wait or contact the owner.
How long does a 502 usually last?
If it's a server restart, from a few seconds to a couple of minutes. If the application crashed and nobody restarted it, until the owner fixes it.
Does a 502 hurt SEO?
A short one doesn't. If search bots get a 502 for several days in a row, pages may drop out of the index — that's why monitoring matters.
How do I fix a 502 on WordPress?
Disable recently installed plugins (rename the plugin folder via SFTP), restart PHP-FPM and check the PHP memory limit. Usually it's a plugin or a shortage of PHP-FPM workers.









