502 Bad Gateway
A 502 means a server acting as a gateway (like Nginx or a load balancer) tried to forward your request to an upstream server, but the upstream server didn’t respond properly.
In simple terms: the middleman (Nginx) is working, but the actual app behind it is down or broken.
If You’re a Developer
Your App Server Crashed
The most common cause. Your Node.js/Python/Go app crashed or isn’t running.
# Check if your app is running
ps aux | grep node
ps aux | grep python
docker ps
# Check the logs
pm2 logs
journalctl -u myapp
docker logs <container>
Fix: Restart the app:
pm2 restart all
sudo systemctl restart myapp
docker compose restart
Your App Is Too Slow
If your app takes too long to respond, the proxy times out and returns 502.
Nginx fix — increase timeout:
location / {
proxy_pass http://localhost:3000;
proxy_read_timeout 120s;
proxy_connect_timeout 120s;
proxy_send_timeout 120s;
}
Wrong Proxy Configuration
Nginx is forwarding to the wrong port or address.
# ❌ App runs on 3000, Nginx points to 8080
proxy_pass http://localhost:8080;
# ✅ Match the actual port
proxy_pass http://localhost:3000;
App Ran Out of Memory
# Check memory
free -h
docker stats
# Check if OOM killer struck
dmesg | grep -i "out of memory"
See: Node heap out of memory fix
If You’re a User
If you’re seeing 502 on someone else’s website:
- Wait and retry — the server might be restarting
- Clear browser cache —
Ctrl+Shift+R/Cmd+Shift+R - Try a different browser or incognito mode
- Check if the site is down for everyone — use downdetector.com or isitdownrightnow.com
Common 502 Scenarios
| Scenario | Cause | Fix |
|---|---|---|
| After deploying | App crashed on startup | Check logs, fix the error |
| Random 502s | App running out of memory | Increase memory, fix leaks |
| Under heavy traffic | App can’t handle the load | Scale up, add caching |
| After server restart | App didn’t auto-start | Set up process manager (pm2, systemd) |
| Docker setup | Wrong port mapping or network | Check docker-compose.yml ports |