502 502 Bad Gateway
The server acting as a gateway received an invalid response from an upstream server.
What Is This?
The HTTP 502 Bad Gateway status code indicates that the server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed to fulfill the request. This is common in architectures with reverse proxies (Nginx), load balancers, or API gateways where the intermediate server cannot get a valid response from the backend application server.
Common Causes & Solutions
Common Cause
Application server (e.g., Node.js, Python, PHP) crashed or stopped
Common Cause
Upstream server timeout — backend took too long to respond
Common Cause
Firewall blocking the connection between proxy and upstream
Common Cause
Upstream server returning garbage or incomplete response data
Check upstream server health
Verify that your application server is running and responding correctly. Check if it crashed, is overloaded, or has a deadlock.
# Check if the upstream process is running ps aux | grep node # Check upstream port availability curl -I http://localhost:3000/health # Restart the application server pm2 restart myapp # or systemctl restart myapp
Increase proxy timeouts
If the upstream server is slow but functional, increase the proxy timeout settings.
# Nginx — increase proxy timeouts
location / {
proxy_pass http://localhost:3000;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
}Related Entries
More from this reference:
500 500 Internal Server Error
The server encountered an unexpected condition that prevented it from fulfilling the request.
503 503 Service Unavailable
The server is temporarily unable to handle the request, typically due to maintenance or overload.
504 504 Gateway Timeout
The server acting as a gateway did not receive a timely response from an upstream server.
Frequently Asked Questions
Is 502 always the fault of the upstream server?
Almost always. 502 means the proxy received an invalid response (or no response) from the upstream. The fix is usually on the upstream server side — check its logs, restart it, or increase its timeouts.
Can Cloudflare or CDNs cause 502 errors?
Yes. CDNs like Cloudflare return 502 when they cannot reach your origin server. If you see 502 from Cloudflare, the issue is with your hosting server. Check your server status and firewall rules to ensure Cloudflare's IPs are allowed.