HTTP Status Codes

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

1

Common Cause

Application server (e.g., Node.js, Python, PHP) crashed or stopped

2

Common Cause

Upstream server timeout — backend took too long to respond

3

Common Cause

Firewall blocking the connection between proxy and upstream

4

Common Cause

Upstream server returning garbage or incomplete response data

5

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
6

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:

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.