502 Bad Gateway

Server Error - Invalid response from upstream server

HTTP 502 Bad Gateway

What It Means

The HTTP 502 Bad Gateway error occurs when a server acting as a gateway or proxy received an invalid response from the upstream server. The problem is between your reverse proxy (like Nginx) and your application server.

Request Flow

User → Nginx (proxy) → App Server ❌ → 502

Common Causes

  • App server crashed: Your Node/Python/PHP process died
  • Not listening: App isn't bound to the expected port
  • Firewall: Connection blocked between proxy and app
  • Socket mismatch: Wrong Unix socket path configured
  • Container stopped: Docker container failed to start
  • Overloaded: App server can't handle request volume

How to Debug

# 1. Is the app server running?
ps aux | grep node
systemctl status myapp

# 2. Is it listening on the right port?
ss -tlnp | grep 3000
netstat -tlnp | grep 3000

# 3. Can you reach it directly?
curl http://localhost:3000/health

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

# 5. Check app logs
journalctl -u myapp -f

Nginx Configuration Check

# Verify upstream matches your app
upstream app {
    server 127.0.0.1:3000;  # Must match app's actual port
}

server {
    location / {
        proxy_pass http://app;
        proxy_connect_timeout 60s;
        proxy_read_timeout 60s;
    }
}

502 vs Other 5xx Errors

Code Problem Location
500 App server itself crashed
502 Proxy → App connection failed
503 Server intentionally unavailable
504 App took too long to respond

Prevention

  • Use process manager (PM2, systemd) to auto-restart crashed apps
  • Configure health checks in your load balancer
  • Monitor app server directly, not just the proxy
  • Set up alerts for process crashes
  • Increase worker processes for high traffic

Frequently Asked Questions

What does HTTP 502 Bad Gateway mean?
HTTP 502 Bad Gateway indicates that a server acting as a gateway or proxy received an invalid response from the upstream server. The problem lies between your reverse proxy (like Nginx) and your application server, not with the client.
What causes 502 Bad Gateway errors?
Common causes include the application server crashing or not running, the app not listening on the expected port, firewall rules blocking the proxy-to-app connection, wrong Unix socket path configuration, Docker containers failing to start, and application server overload.
How do I debug a 502 error?
Check if the app server is running (ps aux, systemctl status), verify it is listening on the right port (ss -tlnp), try reaching it directly (curl localhost:3000), check Nginx error logs, and review application logs for crash information.
What is the difference between 502 and 504?
502 means the upstream server returned an invalid response (connection refused, bad data). 504 means the upstream server was too slow to respond (timeout). 502 is usually a connectivity issue; 504 is a performance issue.
How do I prevent 502 errors?
Use a process manager like PM2 or systemd to auto-restart crashed applications. Configure health checks in your load balancer. Monitor the application server directly, not just through the proxy. Set up alerts for process crashes and high resource usage.

Detect 502 errors instantly

UptimeSignal monitors your endpoints and alerts you instantly on 502 errors.

Start monitoring free →

Related Status Codes

More Resources