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](/blog/docker-compose-validator/) 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 |
Related: EADDRINUSE: Address Already in Use β How to Fix It