How to Troubleshoot a 502 Bad Gateway on Nginx

A 502 Bad Gateway error means Nginx is running, but the app behind it is not answering. The browser shows the error page, your visitors see a dead site, and Nginx itself is fine, which is why this error confuses people: the server that serves the error is healthy, while the thing it proxies to has gone quiet. When you troubleshoot 502 bad gateway nginx errors, the job is to find which link in the chain broke: Nginx, the connection between Nginx and the app, or the app itself. Work through the checks in order and you will find it in minutes instead of guessing at config files.

Troubleshoot 502 Bad Gateway Nginx: Start With the Error Log

Open the Nginx error log before you touch any config. The log names the upstream address Nginx tried, the exact failure, and the time it happened. On Ubuntu and Debian the log sits at /var/log/nginx/error.log. Read the last lines while you reproduce the error in a browser:

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

Three messages cover most cases. “Connection refused” means nothing listens on the upstream port, so the app is down or bound to the wrong address. “Upstream timed out” means the app accepted the connection but never replied, which points at a slow query, a deadlocked worker, or an app that ran out of threads. “Upstream prematurely closed connection” means the app crashed while handling the request. A message that reads nginx upstream connection failed means Nginx could not open a TCP connection at all, which is the same family as connection refused and usually means the app process stopped. Write down which message you see. Every step after this one depends on it, and skipping the log is how a five-minute fix turns into an hour of random restarts.

Is the App Actually Running?

Check the app process next. If Nginx proxies to a Node app, a Python app, or PHP-FPM, that process has to be alive and listening. Use ss -tlnp to list listening ports and match the port in your proxy_pass line. If nothing listens there, the app is down. Restart it and check its own log, not the Nginx log, for the reason it stopped. Out-of-memory kills are common on small VPS plans: the kernel kills the app, Nginx keeps running, and the site shows 502 until someone restarts the app.

If the app is a PHP site, the upstream is usually a PHP-FPM socket instead of a port. A socket path that changed after a PHP version upgrade, for example from php8.2-fpm.sock to php8.3-fpm.sock, breaks every request at once. Confirm the socket file exists at the path in your fastcgi_pass line and that php-fpm is running. The fastest nginx 502 bad gateway fix is often a restart of the app service, but only restart after you know why it stopped, or it will stop again at the same load level. If the app dies under traffic again and again, the fix is capacity, not restarts: our guide to tuning Nginx concurrent connections shows how worker counts and connection limits shape what the server can carry before it starts dropping requests.

Check the Proxy Settings in Your Server Block

When the app runs fine but Nginx still returns 502, read the server block. The usual suspects are a wrong port, a wrong hostname, and a missing trailing slash on proxy_pass. A proxy_pass http://127.0.0.1:3000; line with a trailing slash behaves differently from one without it when a location path is involved, and that one character can send requests to a path the app does not serve. Compare the port in the config with the port the app actually listens on. A typo here produces a clean connection-refused error on every request, which the log will show.

Timeouts deserve their own look. The default proxy_read_timeout is 60 seconds. An app that takes 90 seconds to build a report will hit that limit and return 502 even though it eventually finishes the work. Raising the timeout to 120 seconds fixes the symptom, but a report that takes 90 seconds is a performance problem that caching or a background job should solve. For WordPress sites, Nginx caching for WordPress at scale shows how to keep slow pages from ever reaching the app on repeat visits. And if the app runs inside Docker, confirm the container name in proxy_pass still resolves: a container that was recreated under a new name breaks the proxy while the app itself runs fine.

Rule Out Resources: Memory, Disk, and File Handles

An app can be running and still fail every request because the server ran out of something. A full disk stops PHP-FPM from writing session files and stops databases from writing, which looks like an app failure from the Nginx side. Check disk space with df -h and memory with free -h. Log files that nobody rotates are the usual cause of a full disk on a long-running server. Check open file handles too: an app that leaks connections will hit its limit and refuse new ones, which reads as a timeout in the Nginx log.

Most of the time, a 502 error nginx upstream logs comes with a resource clue if you look for it. Check dmesg for out-of-memory kills, which the kernel logs when it terminates a process to protect the system. If the app was killed for memory, the fix is a larger plan or a smaller memory footprint, not a bigger timeout. Keeping an eye on these numbers before they bite is the whole point of monitoring: our overview of Linux server monitoring basics covers the metrics worth watching, and if you want tooling, our roundup of the best VPS monitoring tools, from Netdata to Zabbix and HetrixTools, compares options for catching a dying app before your visitors do.

Test the Upstream Directly, Bypassing Nginx

Once the log and the process check disagree, test the app without Nginx in the middle. If the app listens on port 3000, run curl -v http://127.0.0.1:3000/ from the server itself. A fast response means the app is healthy and the problem sits in the Nginx config or the connection between the two. A hang or a refused connection means the app is the problem, whatever the config says. This one test splits the whole problem in half, and it takes five seconds.

Do the same test for a Unix socket with curl --unix-socket /run/php/php8.3-fpm.sock http://localhost/ when PHP-FPM is the upstream. If the direct test works but requests through Nginx fail, compare the headers Nginx sends with what the app expects. A missing Host header or a wrong X-Forwarded-Proto value can make an app reject requests that look fine in a direct test. Add the standard proxy headers, reload Nginx with nginx -t first to validate the config, then systemctl reload nginx, and test again. Never restart Nginx to apply a config change when a reload does the same job without dropping connections.

A Short Checklist You Can Run Every Time

When a 502 appears, run the same sequence so nothing gets skipped under pressure. First, read the error log and note the exact message. Second, confirm the app process is running and listening on the right port or socket. Third, test the app directly with curl, bypassing Nginx. Fourth, check disk, memory, and the kernel log for resource kills. Fifth, review the proxy settings and timeouts in the server block. Sixth, fix the cause, reload Nginx, and watch the log while you load the page again.

That order matters because it moves from evidence to cause instead of from guess to guess. The log tells you what failed, the process check tells you whether the app is alive, the direct curl test tells you which side of the proxy the fault sits on, and the resource check catches the silent killers that restart alone never fixes. Keep this sequence in your runbook and a 502 stops being a mystery: it becomes a ten-minute diagnosis with a named cause. The next time you troubleshoot 502 bad gateway nginx errors on a live site, start at the log, trust what it says, and let each check narrow the field until only one suspect is left.

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.