HTTP Error Codes

502 Bad Gateway

HTTP 502

The gateway or proxy server received an invalid response from the upstream server.

What It Means

A 502 error occurs when a server acting as a gateway or proxy (like Nginx, a CDN, or a load balancer) tried to contact your application server but got a bad or no response.

Request flow:
User → Nginx (proxy) → Your App Server (crashed/unreachable) → 502

Common Causes

  • Application server is down — Your Node.js/Python/PHP process crashed or isn't running
  • App server not listening — Server is running but not bound to the expected port
  • Firewall blocking traffic — Connection between proxy and app server is blocked
  • App server overloaded — Too many requests, not enough workers
  • Socket path mismatch — Nginx configured for wrong Unix socket path
  • Container not running — Docker container stopped or failed to start

How to Debug

  1. Is the app server running? — Check process status
  2. Check app server logs — Did it crash? What's the error?
  3. Verify port/socket — Is the app listening on the right port?
  4. Test directly — Can you curl the app server directly (bypassing the proxy)?
  5. Check proxy config — Is Nginx pointing to the right upstream?

Quick Checks

# Check if app is running
ps aux | grep node
systemctl status myapp

# Check if port is listening
ss -tlnp | grep 3000
netstat -tlnp | grep 3000

# Test app directly
curl http://localhost:3000/health

# Check Nginx logs
tail -f /var/log/nginx/error.log

Nginx Configuration Check

# Common upstream config
upstream app {
    server 127.0.0.1:3000;  # Make sure this matches your app
}

server {
    location / {
        proxy_pass http://app;
    }
}

Prevention

  • Use a process manager (PM2, systemd) to auto-restart crashed apps
  • Set up health checks in your load balancer
  • Monitor your application server directly, not just the proxy
  • Configure proper timeout and retry settings

How to Monitor for 502 Errors

502 errors are common during deployments and app crashes. UptimeSignal monitors your endpoints and alerts you immediately when 502 errors appear. Monitor both the proxy endpoint and the application server directly for maximum visibility. See also: 500 Internal Server Error, 504 Gateway Timeout.

Frequently Asked Questions

What causes a 502 Bad Gateway error?
A 502 occurs when a reverse proxy (Nginx, HAProxy, CDN) receives an invalid or no response from the upstream application server. The backend app has crashed, the app server isn't running, the upstream socket/port is misconfigured, or the app is returning malformed responses.
How do I fix a 502 Bad Gateway?
Check if your app is running (systemctl status myapp, pm2 status). Verify the upstream address in Nginx matches your app's listen port. Test the app directly: curl http://localhost:3000/health. Check /var/log/nginx/error.log for the specific upstream error.
What is the difference between 502 and 504?
504 Gateway Timeout means the proxy waited but got no response in time (backend is slow). 502 means the proxy got an invalid or no response (backend is broken/down). 502 = backend crashed or isn't running. 504 = backend is alive but too slow.
Why do I get 502 errors after deploying?
During deployment, the application restarts. For the seconds it takes to restart, the proxy has no upstream to forward to, causing 502 errors. Fix with zero-downtime deployments: use pm2 reload, rolling restarts in Kubernetes, or blue-green deployments. Also check for startup errors in application logs.
How do I prevent 502 errors?
Use a process manager (PM2, systemd) to auto-restart crashed apps. Configure health checks in your load balancer. Implement graceful shutdown and startup. Monitor both the proxy and the app server directly with UptimeSignal. Set up proper connection refused handling in your proxy config.

Detect 502 Bad Gateway errors instantly

UptimeSignal monitors your endpoints and catches proxy and gateway failures the moment they happen.

25 monitors free, unlimited for $15/month. 1-minute checks on Pro.

Related Errors & Resources