Error Codes Wiki

HTTP Proxy Errors — 502 Bad Gateway and 504 Gateway Timeout Deep Dive

Error5xx server error

Overview

Comprehensive guide to troubleshooting proxy-related HTTP errors including 502 Bad Gateway and 504 Gateway Timeout from reverse proxies, load balancers, and CDNs.

Key Details

  • 502 Bad Gateway means the proxy received an invalid response from the upstream server
  • 504 Gateway Timeout means the proxy did not receive any response from the upstream server in time
  • Common proxies: Nginx, Apache (mod_proxy), HAProxy, AWS ALB/ELB, Cloudflare, Fastly
  • Proxy timeout settings must be configured to match backend processing time expectations
  • Health checks can detect unhealthy upstream servers before users encounter 502/504 errors

Common Causes

  • Backend application server crashed or is not running
  • Proxy timeout shorter than the backend's processing time for slow endpoints
  • Backend server's connection pool exhausted, refusing new connections
  • DNS resolution failure between the proxy and backend server
  • SSL/TLS handshake failure between proxy and backend (protocol mismatch)

Steps

  1. 1Check if the backend server is running: curl the backend directly bypassing the proxy
  2. 2Increase proxy timeout for slow endpoints: proxy_read_timeout in Nginx, ProxyTimeout in Apache
  3. 3Check backend server logs for crashes, OOM kills, or unhandled exceptions
  4. 4Verify DNS resolution between proxy and backend: dig or nslookup the backend hostname
  5. 5Monitor backend connection pool usage and increase max connections if needed
  6. 6Implement health checks in your proxy to route traffic away from unhealthy backends

Tags

502504proxyreverse-proxygateway-error

Related Items

More in 5xx Server Error

Frequently Asked Questions

Check the Server or Via headers in the error response. Cloudflare adds cf-ray, AWS adds x-amzn-requestid. The error page style also varies by proxy vendor.