Your phone buzzes. The site is showing 502 Bad Gateway. Customers are already emailing.
The good news: a 502 is one of the most diagnosable errors on the web. It almost always means the same thing, and there's a short list of reasons it happens. Here's the checklist I go through, in order, so I don't panic-restart things and lose the evidence.
What a 502 actually means
Your reverse proxy (usually nginx) is fine. It received the request, passed it to the thing behind it (PHP-FPM, Node, Gunicorn, a Docker container), and got back garbage or nothing.
So the question is never "what's wrong with nginx?" It's "what happened to the upstream?"
Step 1: Who is returning the 502?
bash
curl -sI https://yoursite.com | head -5
If you're behind Cloudflare or another CDN, their 502 page looks different from nginx's. A CDN 502 can mean your whole server is unreachable. An nginx 502 means the server is up but the app behind it isn't answering.
Step 2: Read the nginx error log (it usually tells you)
bash
sudo tail -n 50 /var/log/nginx/error.log
What you're looking for:
Log message What it usually means
connect() failed (111: Connection refused) The app isn't running, or isn't listening where nginx expects
upstream prematurely closed connection The app crashed mid-request
no live upstreams Every backend in the pool is marked down
(13: Permission denied) while connecting to upstream Socket permissions or SELinux
(2: No such file or directory) on a .sock The socket file doesn't exist; the app never started
Nine times out of ten, this step alone tells you which branch you're on.
Step 3: Is the upstream alive?
bash
systemctl status php8.2-fpm # or your app's service name
sudo ss -ltnp | grep -E ':3000|:8000|:9000'
If the service is dead, don't restart it yet. First find out why it died, or it'll die again in ten minutes.
Step 4: Why did it die?
bash
journalctl -u your-app --since "1 hour ago" --no-pager | tail -50
sudo dmesg -T | grep -iE 'killed process|out of memory'
The dmesg line is the one people forget. If the kernel's OOM killer took out your app, the app's own logs will just... stop. No error, no stack trace. Only the kernel knows.
Step 5: The boring suspects
These cause a surprising share of 502s:
bash
df -h # full disk: apps can't write sessions, cache, or logs
df -i # out of inodes: same symptoms, disk looks "fine"
free -h # memory pressure, swap thrashing
A full disk is sneaky. The app often starts fine, then fails on the first request that tries to write anything.
Step 6: Did someone change something?
bash
sudo nginx -t
grep -rE 'proxy_pass|fastcgi_pass' /etc/nginx/sites-enabled/
Compare that address or socket path with where your app is actually listening (from Step 3). A PHP upgrade that moved the socket from php8.1-fpm.sock to php8.2-fpm.sock is a classic.
Also worth checking:
bash
grep -E ' install | upgrade ' /var/log/dpkg.log | tail -20
find /etc -mmin -120 -type f 2>/dev/null
"What changed in the last two hours?" answers more outages than any monitoring dashboard.
502 vs 504
If you're seeing 504 Gateway Timeout instead, the upstream is alive but too slow. That's a different hunt: slow queries, a locked database, an external API hanging. Don't mix up the two.
After the fire is out
Add the OOM check to your runbook. It's invisible otherwise.
Alert on disk at 80%, not 100%.
Write down which of the steps above found the problem. After three incidents you'll see your server's personal pattern.
I went through this checklist by hand enough times that I ended up building a tool that runs it for me: MellonBot reads the logs, checks the upstream, and tells you in plain English which step it failed at. But the checklist above works fine with nothing but a terminal and a cup of coffee.
What's the weirdest cause of a 502 you've hit? Mine was a log file that filled the disk at 3am.
