🔧 Error Fixes

502 Bad Gateway — What It Means and How to Fix It


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:

  1. Wait and retry — the server might be restarting
  2. Clear browser cacheCtrl+Shift+R / Cmd+Shift+R
  3. Try a different browser or incognito mode
  4. Check if the site is down for everyone — use downdetector.com or isitdownrightnow.com

Common 502 Scenarios

ScenarioCauseFix
After deployingApp crashed on startupCheck logs, fix the error
Random 502sApp running out of memoryIncrease memory, fix leaks
Under heavy trafficApp can’t handle the loadScale up, add caching
After server restartApp didn’t auto-startSet up process manager (pm2, systemd)
Docker setupWrong port mapping or networkCheck docker-compose.yml ports