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
- 1Check if the backend server is running: curl the backend directly bypassing the proxy
- 2Increase proxy timeout for slow endpoints: proxy_read_timeout in Nginx, ProxyTimeout in Apache
- 3Check backend server logs for crashes, OOM kills, or unhandled exceptions
- 4Verify DNS resolution between proxy and backend: dig or nslookup the backend hostname
- 5Monitor backend connection pool usage and increase max connections if needed
- 6Implement health checks in your proxy to route traffic away from unhealthy backends
Tags
502504proxyreverse-proxygateway-error
Related Items
More in 5xx Server Error
http-500-internal-server-errorHTTP 500 Internal Server Error — What It Means & How to Fix It
Criticalhttp-501-not-implementedHTTP 501 Not Implemented — What It Means & How to Fix It
Criticalhttp-502-bad-gatewayHTTP 502 Bad Gateway — What It Means & How to Fix It
Criticalhttp-503-service-unavailableHTTP 503 Service Unavailable — What It Means & How to Fix It
Criticalhttp-504-gateway-timeoutHTTP 504 Gateway Timeout — What It Means & How to Fix It
Criticalhttp-505-http-version-not-supportedHTTP 505 HTTP Version Not Supported — What It Means & How to Fix It
CriticalFrequently 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.