HTTP Proxy Errors — 502 Bad Gateway and 504 Gateway Timeout Deep Dive
About HTTP Proxy Errors
Comprehensive guide to troubleshooting proxy-related HTTP errors including 502 Bad Gateway and 504 Gateway Timeout from reverse proxies, load balancers, and CDNs. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: 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. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: 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). Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Check if the backend server is running: curl the backend directly bypassing the proxy. Increase proxy timeout for slow endpoints: proxy_read_timeout in Nginx, ProxyTimeout in Apache. Check backend server logs for crashes, OOM kills, or unhandled exceptions. Verify DNS resolution between proxy and backend: dig or nslookup the backend hostname. Monitor backend connection pool usage and increase max connections if needed. Implement health checks in your proxy to route traffic away from unhealthy backends. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our HTTP Status Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
How do I identify which proxy is causing the error?
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.
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