How to Fix 502 Bad Gateway in Nginx
Your site shows "502 Bad Gateway" and nothing else. Here's how to read the Nginx error log, find out why the app behind it stopped answering, and fix the most common causes on WordPress, PHP-FPM, Node.js and Docker.

Your site was working an hour ago. Now every visitor gets a plain white page that says:
502 Bad Gateway
nginx
Here's what that means. Nginx itself is fine: it took the request and passed it on to the program that actually builds your pages, such as PHP-FPM for WordPress, a Node.js or Python app, or a Docker container. That program didn't give a proper answer, so Nginx gave up and showed a 502.
So the fix is almost never in Nginx alone. You need to find out why the program behind it isn't answering. The good news is that Nginx writes down the exact reason, and this guide shows you how to read it and fix each case.
Step 1: Read the Nginx Error Log
Don't guess. Open the error log and look at the last lines, right after you load the broken page:
sudo tail -n 30 /var/log/nginx/error.log
If your site has its own log, it's set by the error_log line in its server block, usually in /etc/nginx/sites-available/ or /etc/nginx/conf.d/. You'll find a line like this:
connect() failed (111: Connection refused) while connecting to upstream, client: 198.51.100.7, server: example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/"
The part in the middle is the reason, and the upstream part tells you which program Nginx tried to reach. Match your message to the table, then jump to its fix.
| Message in error.log | What it means |
|---|---|
(111: Connection refused) | Nothing is running on that port |
(2: No such file or directory) on a .sock | PHP-FPM is stopped, or the socket path is wrong |
(13: Permission denied) | Nginx isn't allowed to reach the socket or port |
upstream prematurely closed connection | The app crashed or was killed mid-request |
upstream sent too big header | The response headers don't fit Nginx's buffer |
(11: Resource temporarily unavailable) | PHP-FPM is overloaded and its queue is full |
Fix 1: "Connection Refused" Means Your App Isn't Running
This is the most common 502. Check whether anything is listening on the port from the log:
sudo ss -tlnp | grep 3000
No output means the app is down. Start it and look at why it stopped:
# an app run by systemd
sudo systemctl status myapp
sudo journalctl -u myapp -n 50
# a Node.js app run by PM2
pm2 status
pm2 logs --lines 50
If the app is running but on a different port, make the proxy_pass line in Nginx match the port the app really uses.
The "localhost" trap with Node.js
Here's one that catches a lot of people. The app is clearly running, Nginx points to 127.0.0.1:3000, and you still get a 502. Since Node.js 17, an app started with listen(3000, 'localhost') can end up listening only on IPv6 (::1), so nothing answers on 127.0.0.1. Check which address the app really uses:
sudo ss -tlnp | grep 3000
If you see [::1]:3000, make the app listen on 127.0.0.1 instead, and keep Nginx pointed at that same address:
proxy_pass http://127.0.0.1:3000;
Apps running in Docker
Check the container is up with docker ps, and read its log with docker logs --tail 50 CONTAINER. If Nginx runs on the server itself, the container's port must be published, for example -p 127.0.0.1:3000:3000. If Nginx runs in Docker too, point it at the container's name on the shared network, not at localhost.
Fix 2: PHP-FPM Socket Not Found (WordPress and PHP Sites)
For WordPress and other PHP sites, the 502 usually looks like this:
connect() to unix:/run/php/php8.1-fpm.sock failed (2: No such file or directory)
Either PHP-FPM isn't running, or Nginx points to a socket that doesn't exist. That happens a lot after upgrading PHP: the server now has php8.3-fpm.sock, but your site still asks for the 8.1 one. See which sockets really exist and whether the service runs:
ls /run/php/
sudo systemctl status php8.3-fpm
Then fix the fastcgi_pass line in your site's config so it uses the socket you found, test the config and reload:
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
sudo nginx -t
sudo systemctl reload nginx
On AlmaLinux and Rocky Linux, the service is called php-fpm and the socket is usually /run/php-fpm/www.sock.
Fix 3: "Permission Denied" While Connecting to Upstream
The app is running, but Nginx isn't allowed to talk to it.
For a PHP-FPM socket: the socket must belong to the user Nginx runs as. On Ubuntu and Debian that's www-data. Check these lines in the pool file, for example /etc/php/8.3/fpm/pool.d/www.conf, then restart PHP-FPM:
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
On AlmaLinux and Rocky Linux: SELinux blocks Nginx from connecting to app ports by default. If getenforce says Enforcing, allow it:
sudo setsebool -P httpd_can_network_connect 1
Fix 4: "Upstream Prematurely Closed Connection"
The app accepted the request and then died before answering. The two usual reasons are a crash and running out of memory. Check if the system killed it:
sudo dmesg -T | grep -i "killed process"
free -h
If you see your app, PHP-FPM or MySQL in that list, the server ran out of RAM. You can add a swap file as a stopgap, lower PHP-FPM's pm.max_children so it stops starting more workers than memory allows, or move to a plan with more RAM. If nothing was killed, the app's own log (see Fix 1) will show the error that crashed it.
A full disk causes this too, because apps and databases fail when they can't write. Run df -h, and if / is at 100%, follow our guide to fixing "No space left on device".
Fix 5: "Upstream Sent Too Big Header"
Big cookies or long redirect headers, common with WordPress plugins and login systems, can overflow Nginx's default buffer. Make it bigger inside the server or location block. For a proxied app:
proxy_buffer_size 16k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 32k;
For PHP-FPM:
fastcgi_buffer_size 32k;
fastcgi_buffers 16 16k;
Then run sudo nginx -t and sudo systemctl reload nginx.
Fix 6: PHP-FPM Is Overloaded
If the 502 only shows up when traffic is high, and the log says (11: Resource temporarily unavailable), every PHP-FPM worker is busy and new requests can't get in. The PHP-FPM log confirms it with a line like server reached pm.max_children setting. Raise the limit in the pool file:
pm = dynamic
pm.max_children = 20
Don't just pick a big number. Each worker uses memory, often 50 to 100 MB on a WordPress site. Divide the RAM you can spare by that amount to find a safe value. Turning on a page cache helps even more, because cached pages don't need PHP at all.
502 or 504? Know the Difference
A 502 Bad Gateway means the app gave no usable answer: it was down, crashed or couldn't be reached. A 504 Gateway Timeout means the app was reached but took too long. For a 504, look at slow database queries or raise proxy_read_timeout or fastcgi_read_timeout. For a 502, the fixes above are the place to look.
Quick Checklist
sudo tail -n 30 /var/log/nginx/error.logright after the error.- Connection refused: start the app, match the port, use
127.0.0.1instead oflocalhost. - No such file (.sock): start PHP-FPM and fix the socket path in
fastcgi_pass. - Permission denied: socket owner
www-data, or the SELinux setting. - Prematurely closed: check memory, the app's log and disk space.
- Too big header: raise the buffers. Overloaded: raise
pm.max_childrencarefully. - Always
sudo nginx -tbefore you reload.
Most 502 errors come down to an app that stopped or a server that ran short of memory. If your sites have outgrown their server, a website VPS or a WordPress VPS from NodexVPS gives you dedicated resources and full root access, so you can tune Nginx and PHP-FPM exactly the way your site needs.