Error Encyclopedia
502

502 Bad Gateway Error

Learn what 502 Bad Gateway means, common causes like proxy or load balancer misconfiguration, and how to fix gateway errors.

What Does This Error Mean?

The 502 Bad Gateway status code means the server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed to fulfill the request.

Common Causes

1

Upstream server (application server) crashed or is unresponsive

2

Nginx/Apache reverse proxy configuration error

3

Load balancer timeout waiting for a backend response

4

Firewall blocking communication between proxy and upstream

5

SSL/TLS handshake failure between proxy and upstream

6

Upstream server returns an empty or malformed response

How to Fix It

Check upstream server health

Verify the upstream server (your app server) is running and responding correctly.

# Check if the upstream server is running
curl -I http://localhost:3000/health
# Should return 200 OK

# Check application process
systemctl status myapp
# or
pm2 status

Fix Nginx proxy configuration

Ensure the proxy_pass directive points to the correct upstream address and port.

server {
  location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    # Increase timeout if needed
    proxy_read_timeout 60s;
  }
}

Check firewall and network

Ensure the proxy server can reach the upstream server through any firewalls or security groups.

# Test connectivity from proxy to upstream
telnet upstream-server 3000
# or
nc -zv upstream-server 3000

Related Tools

Use these tools to debug and fix this error:

Related Errors

Other common errors in this category:

Frequently Asked Questions

What is the difference between 502 and 504?

502 Bad Gateway means the upstream gave an invalid response. 504 Gateway Timeout means the upstream did not respond within the timeout period.

Can Cloudflare cause a 502 error?

Yes. Cloudflare can return 502 if the origin server is unreachable, the SSL handshake fails, or the origin returns an invalid response. Check Cloudflare's analytics for error details.