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
- Is the app server running? — Check process status
- Check app server logs — Did it crash? What's the error?
- Verify port/socket — Is the app listening on the right port?
- Test directly — Can you curl the app server directly (bypassing the proxy)?
- 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?
How do I fix a 502 Bad Gateway?
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?
Why do I get 502 errors after deploying?
pm2 reload, rolling restarts in Kubernetes, or blue-green deployments. Also check for startup errors in application logs.