What 502 Bad Gateway means, what to do as a visitor and how a site owner can find the cause: on cPanel hosting and on your own server with nginx and PHP-FPM.
502 Bad Gateway means that the server your browser contacted did not itself get a response from the next link in the chain. Usually nginx sits in front of the website, and the site runs in PHP-FPM, a Node.js or Python application, or a Docker container. If that application has crashed, is overloaded or is unreachable, nginx returns a 502 error to the visitor.
If you are a visitor
Refresh the page after a minute: most often this is a short fault on the website’s side and there is nothing you can do about it. If every website shows the error, the problem is with your connection, VPN or proxy.
On our hosting
In cPanel, open Errors — it shows the latest errors on your site. Common causes of 502 on hosting:
- a script runs for too long — a heavy import, a report or a backup plugin;
- the site cannot cope with the load — errors appear when there is a rush of visitors or bots: Website slowed down by a flood of requests;
- a PHP error after a CMS or plugin update.
If the error lasts longer than a few minutes, contact our support team through the contact form and tell us the address of the page and the time the error first appeared.
On your own server
The main clue is in the nginx error log:
sudo tail -n 30 /var/log/nginx/error.log| Line in the log | Cause |
|---|---|
connect() to unix:/run/php/php8.4-fpm.sock failed (2: No such file or directory) | PHP-FPM is not running, or the nginx configuration has the wrong socket path |
connect() … (13: Permission denied) | nginx does not have permission to use the PHP-FPM socket — check listen.owner and listen.group in the pool settings |
connect() failed (111: Connection refused) while connecting to upstream | the application on that port is not running — for example, the Docker container has stopped |
upstream prematurely closed connection | the application crashed in the middle of a response — check its own log |
Check whether the thing nginx connects to is actually running:
systemctl status php8.4-fpm # the service name depends on your PHP version
sudo journalctl -u php8.4-fpm -n 50
sudo ss -ltnp # is the application listening on its port?If PHP-FPM or the application keeps disappearing by itself, check whether the system is killing it because it has run out of memory: OOM killer. For how to read service logs, see Logs: journalctl and /var/log.
502 and 504 — what is the difference
502 — the application returned an error or did not respond at all. 504 Gateway Timeout — the application did not respond within the allowed time: nginx waited and gave up. With a 504, look for slow database queries and long-running scripts; the waiting time in nginx is set by proxy_read_timeout and fastcgi_read_timeout.
Didn’t find the answer? Ask our support team — we’ll help.