Checked
2026-11-14
/ 502 Bad Gateway

502 Bad Gateway: nginx is up, the app behind it is not.

A 502 from nginx means nginx itself runs and could not get a proper answer from what sits behind it: PHP-FPM, a Node or Python app, or another proxy. The error log says which, usually in one line.

Read the error log first

Every 502 leaves a line in nginx's error log. On Debian and Ubuntu it is /var/log/nginx/error.log, unless the site's config names another file with error_log.

tail -n 50 /var/log/nginx/error.log

Load the page once while you watch the log with tail -f, and the newest line is the one that matters. The words after the error number tell you where to look.

What the log line means

  • connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory): PHP-FPM is not running, or nginx points at the socket of a PHP version that is no longer installed. ls /run/php/ shows the sockets that exist.
  • The same line with (13: Permission denied): the socket's owner or mode does not let the nginx user in. Compare listen.owner, listen.group and listen.mode in the PHP-FPM pool file with the user nginx runs as.
  • connect() failed (111: Connection refused) while connecting to upstream: nothing listens on the address in proxy_pass or fastcgi_pass. The app crashed, or it listens on another port.
  • upstream prematurely closed connection while reading response header from upstream: the app took the request and died, or was killed, while answering it. Its own log, or dmesg for the out-of-memory killer, has the reason.
  • upstream sent too big header while reading response header from upstream: the app answered with headers, usually cookies, bigger than nginx's buffer. Raise proxy_buffer_size, or fastcgi_buffer_size for PHP, one step at a time.

The usual fixes, in order

Use your own PHP version or service name in place of php8.3-fpm.

  1. systemctl status php8.3-fpm, or your app's service, shows whether it runs and its last log lines. systemctl list-units --type=service | grep -i fpm finds the right name.
  2. systemctl restart php8.3-fpm brings a stopped service back. If it stops again within minutes, the restart was not the fix: journalctl -u php8.3-fpm -n 100 says why it stops.
  3. For an app behind proxy_pass, ss -tlnp shows what listens on which port. curl -I http://127.0.0.1:3000, with your app's port, asks the app directly and skips nginx.
  4. After any edit to the nginx config, nginx -t checks it, and systemctl reload nginx applies it without dropping connections.

What not to do

  • Do not reboot the whole server first. If PHP-FPM ran out of workers or memory, the 502 comes back, and the log lines that said so are harder to find afterwards.
  • Do not raise every timeout and buffer at once. A 502 is not a timeout; that is a 504, and it has causes of its own.
  • Do not leave a PHP upgrade half done. If the old PHP package was removed, the site's config must point at the new version's socket.

When to stop and hand it over

Stop if the service keeps dying after a restart, if the log points at memory or a crash you cannot explain, or if the site takes payments and every hour costs money. Our Server Rescue is one incident at a fixed $69, with a same-day start listed at $109. Read-only checks come first, nothing destructive runs without a backup in front of it, and if we cannot fix it, you pay nothing.

/ Questions

Questions people ask

What does 502 Bad Gateway mean?
The server that answered, usually nginx or a CDN, works, but the app or server behind it sent no valid answer. The fault is almost always behind nginx, not in it.
Is a 502 my fault or my host's?
On your own VPS it is usually PHP-FPM or the app on that server. If the 502 page carries a CDN's name, the CDN could not get an answer from your server, so check that the server answers at all.
Why does the 502 come and go?
Most often PHP-FPM runs out of workers under load, or a process is killed for lack of memory and then restarts. Line up the timestamps in the error log with the traffic and it shows which.
What does fixing a 502 cost?
With us, Server Rescue at $69 for the incident, or $109 for a same-day start. If we cannot fix it, you pay nothing.
/ Sources

Where each statement comes from

Checked against the sources on .

More server help