Fixing 502 Bad Gateway in Nginx: 7 Common Causes

The 7 Causes of 502 Bad Gateway in Nginx

A 502 Bad Gateway in Nginx means Nginx received an invalid response from the upstream it proxies to, whether that upstream is PHP-FPM, a Node.js app, or another server. The error looks the same in the browser no matter the cause, which is why fixing it feels frustrating. This guide covers the seven most common causes of a 502 bad gateway in Nginx, in the order worth checking, so you can find yours without guessing.

Start by reading the error log. It usually names the upstream and the reason in one line:

sudo tail -n 50 /var/log/nginx/error.log
sudo systemctl status php8.3-fpm --no-pager
sudo systemctl status nginx --no-pager

Replace 8.3 with your PHP version. If the log says “connect() to unix:/run/php/php8.3-fpm.sock failed”, the problem is between Nginx and PHP-FPM. If it says “upstream prematurely closed connection”, the upstream crashed or timed out. That one line narrows seven causes down to two or three.

1. PHP-FPM Is Not Running

The most common cause on a WordPress server is PHP-FPM being down. It may have failed to start after a reboot, crashed from memory pressure, or been stopped by an update. Restart it and check the status:

sudo systemctl restart php8.3-fpm
sudo systemctl status php8.3-fpm --no-pager
sudo tail -n 30 /var/log/php8.3-fpm.log

If PHP-FPM keeps dying, check memory. A pool sized too large for the server’s RAM gets killed by the system under load. The guide to installing WordPress on a LEMP stack sets up the stack this error usually appears on, and pool sizing is covered in detail in PHP-FPM tuning guides. A Nginx 502 PHP-FPM failure that appears only under traffic usually means the pool is full, not that the service is down, so check the pool status page before raising limits blindly.

2. Wrong Socket or Port in the Nginx Configuration

Nginx must point at the exact socket or port PHP-FPM listens on. If PHP-FPM listens on /run/php/php8.3-fpm.sock but Nginx proxies to /run/php/php7.4-fpm.sock after a PHP upgrade, every PHP request returns 502 while static files still work. That split is the clue: static pages load, PHP pages fail. Compare the two settings:

grep -r "listen =" /etc/php/8.3/fpm/pool.d/
grep -r "fastcgi_pass" /etc/nginx/sites-enabled/

Make them match, test the configuration, and reload Nginx. The same mismatch happens with proxied apps: if your Node.js app listens on port 3000 but Nginx proxies to port 3001, the result is the same 502. Check the app’s actual listening port with ss -tlnp before editing the proxy.

3. Upstream Timeout and Crashed Workers

An upstream connection failed error also appears when the upstream takes too long or crashes mid-request. A PHP script that runs past the FastCGI timeout, a database query that hangs, or a worker killed by memory limits all produce 502. Check the upstream timeout settings in your server block:

fastcgi_connect_timeout 60;
fastcgi_send_timeout 60;
fastcgi_read_timeout 60;

Raising timeouts only helps if the upstream eventually finishes. If the script always hangs, find the slow query or the infinite loop instead of raising the timeout forever. For proxied Docker apps, the same logic applies: the guide to configuring Nginx as a reverse proxy for Docker containers explains how container restarts and port mappings produce this exact error.

4. Permission Problems on the Socket

When PHP-FPM runs as one user and Nginx as another, the socket file permissions decide whether Nginx can connect. If the socket is owned by root with mode 600, Nginx running as www-data cannot reach it. The pool file should set the socket owner and mode explicitly:

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

After changing these, restart PHP-FPM and confirm the socket ownership with ls -l /run/php/. Permission problems often appear after a PHP upgrade that resets the pool file, so this is worth checking whenever a working site breaks right after an update.

5. Full PHP-FPM Pool, Bad Upstream Config, and Server Resource Exhaustion

The remaining causes share one symptom: the upstream exists but cannot take more work. A full pool queues requests until they time out, which the error log reports as a timeout rather than a connection failure. A bad upstream block, such as a server name that does not resolve, fails every request immediately. Resource exhaustion, including a full disk or exhausted memory, kills workers unpredictably and produces intermittent 502 errors that come and go with load.

For a full pool, raise pm.max_children only if free RAM allows it, and check the pool status page for the queue length. For a bad upstream block, verify every proxy_pass and fastcgi_pass target resolves and listens. For resource exhaustion, check df -h and free -m before anything else, because no configuration fix helps a full disk. A beginner-friendly reference for the proxy side is the Nginx reverse proxy setup tutorial, and caching can reduce upstream load enough to stop the errors entirely, as explained in the Nginx caching guide.

Work through the causes in order, fix one at a time, and retest after each change. Changing three settings at once and seeing the error disappear teaches you nothing about which one mattered, and the same error will return the next time that setting breaks. Nginx 502 troubleshooting rewards patience more than speed.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.