- Checked
- 2026-11-14
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.logLoad 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. Comparelisten.owner,listen.groupandlisten.modein 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 inproxy_passorfastcgi_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, ordmesgfor 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. Raiseproxy_buffer_size, orfastcgi_buffer_sizefor 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.
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 fpmfinds the right name.systemctl restart php8.3-fpmbrings a stopped service back. If it stops again within minutes, the restart was not the fix:journalctl -u php8.3-fpm -n 100says why it stops.- For an app behind
proxy_pass,ss -tlnpshows 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. - After any edit to the nginx config,
nginx -tchecks it, andsystemctl reload nginxapplies 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 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.
Where each statement comes from
Checked against the sources on .
- nginx: ngx_http_proxy_module (nginx.org)
- nginx: ngx_http_fastcgi_module (nginx.org)
- PHP manual: FPM configuration (php.net)
- MDN: 502 Bad Gateway (developer.mozilla.org)
More server help
- 504 Gateway Timeout
- Server down: what to check first
- Disk full: no space left on device
- WordPress white screen or critical error
- Error establishing a database connection
- Too many redirects
- 403 Forbidden on nginx or Apache
- SSL certificate expired: the fix
- Renewing a Let's Encrypt certificate
- DNS not resolving or still on the old server
- Email from the server going to spam
- Website hacked: the first steps
- Moving a site to a new server
- Linux server hardening checklist
- All server help guides
