🚧
Hosting

502 Bad Gateway: finding and fixing the cause when the backend is down

18.11.2026
← All articles

A 502 Bad Gateway means Nginx itself is healthy, but the backend it forwarded the request to (PHP-FPM, Node.js, Gunicorn or another app server) didn't return a valid response. Unlike a 504, where the backend answers too late, with a 502 the connection fails outright, drops immediately or returns garbage. So the first question is always: is the backend alive, and is Nginx pointing at the right address?

Reading the Nginx error log

sudo tail -n 50 /var/log/nginx/error.log
Log messageMeaning
connect() failed (111: Connection refused)Nothing listens on that port; the app is down or on another port
(2: No such file or directory)The Unix socket doesn't exist: PHP-FPM isn't running or the path is wrong
(13: Permission denied)The Nginx user can't access the socket
upstream prematurely closed connectionThe backend died mid-response, often a crash or out of memory
upstream sent too big headerResponse headers didn't fit the buffer, often a huge cookie
(11: Resource temporarily unavailable)Socket backlog full; all workers busy

PHP-FPM is stopped

systemctl status php8.2-fpm
sudo journalctl -u php8.2-fpm -n 50
sudo php-fpm8.2 -t
sudo dmesg -T | grep -i -E "killed process|out of memory"

php-fpm8.2 -t reveals config syntax errors that stop the service from starting. If it keeps dying on its own, check dmesg for the OOM killer.

Socket path mismatch

A 502 right after a PHP upgrade is classic: Nginx still points to php8.1-fpm.sock while the new version created a different socket. Compare the real file with the pool's listen value:

ls -l /run/php/
grep -E "^listen" /etc/php/8.2/fpm/pool.d/www.conf

For permission errors, make sure listen.owner and listen.group match the Nginx user (usually www-data on Debian and Ubuntu).

A crashed Node.js or Python app

sudo ss -ltnp | grep 3000
pm2 status
pm2 logs --lines 100

For systemd services, use journalctl -u myapp -n 100. Common culprits after a deploy are a missing package, a missing .env variable or a failed database connection. Another classic: the app listens on localhost, which resolves to IPv6 ::1, while Nginx connects to 127.0.0.1. Use one explicit address on both sides.

Oversized headers

fastcgi_buffer_size 32k;
fastcgi_buffers 8 32k;
proxy_buffer_size 32k;
proxy_buffers 8 32k;

Larger buffers fix upstream sent too big header, but it's worth finding out why the cookie grew so large.

Example: a hotel booking site in Bukhara

A small Bukhara hotel's booking site runs on a VPS. The admin upgraded PHP and removed the old version, and by morning every page returned 502. The log showed (2: No such file or directory) with the old socket path, and ls /run/php/ listed only the new one. Updating fastcgi_pass, running nginx -t and reloading fixed it instantly.

Intermittent 502s under load

server reached pm.max_children setting in the PHP-FPM log means all workers were busy. Don't raise pm.max_children blindly, since each worker uses memory and the server may hit OOM. Measure per-process memory, size the pool to available RAM, speed up slow requests, and rate-limit bots with limit_req.

On shared hosting

You can't restart services yourself, but you can check error logs in the ISPmanager panel, revert a recent PHP version change, or disable a newly installed plugin. If the 502 persists for more than a few minutes, open a ticket from the sayt.uz cabinet with the URL and time. Behind a CDN like Cloudflare, the 502 page may come from the CDN, meaning the problem is between the CDN and your server.

Related articles

🧭 Self-Hosting a Vector Database: Qdrant and pgvector 🧭 Portainer: Managing Docker from Your Browser 💾 How to Restore a Site and Database from Backup in ISPmanager 🤝 Reseller hosting in Uzbekistan: hosting client sites the organised way
🌐 Language
🇺🇿 O'zbek 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓