502 Bad Gateway
Invalid responses from an upstream server cause the gateway or proxy to return 502 Bad Gateway.
Usage
The 502 Bad Gateway status code means the server is acting as a proxy or gateway for an upstream server. After forwarding the HTTP request, the upstream server returned an invalid response.
When the origin server returns an HTTP error status code, the same status code and related information is expected to be forwarded to the client for the most accurate description of the problem.
When the proxy receives no response from the upstream server at all, a 504 Gateway Timeout status code is more appropriate.
Example
The client requests a resource and the server responds with a 502 Bad Gateway status code because the upstream server returned an invalid response.
Request
GET /news HTTP/1.1
Host: www.example.re
Response
HTTP/1.1 502 Bad Gateway
Content-Type: text/html; charset=UTF-8
Content-Length: 134
<html>
<head>
<title>Bad Gateway</title>
</head>
<body>
<p>The server is unreachable at this time.</p>
</body>
</html>
502 at an AWS Application Load Balancer
An AWS Application Load Balancer answers 502 when the target refuses or breaks the connection. The documented causes are a TCP RST or ICMP unreachable while connecting, a TCP RST or FIN from the target while a request was outstanding, a malformed target response or response headers over 32 KB, a target deregistered before a long request finished, and Lambda targets returning an error, timing out, or sending a body over 1 MB.
The outstanding-request case is the most common one on healthy fleets. A target whose Keep-Alive timeout is shorter than the ALB idle timeout closes an idle connection at the same moment the ALB reuses it. Setting the target's keep-alive timeout above the ALB idle timeout (default 60 seconds) removes the race.
502 behind Cloudflare
Cloudflare serves two different 502 pages. A Cloudflare-branded error page means the origin answered with its own 502 or 504, and the fix lives at the origin. A blank, unbranded 502 comes from Cloudflare itself, most often because the origin sent gzip-compressed content with a stale Content-Length or broken compression. Disabling compression at the origin confirms the diagnosis.
How to fix
Check /var/log/nginx/error.log or the equivalent
proxy error log first. The log message identifies
the upstream failure mode: connection refused,
prematurely closed connection, or invalid header.
Common root causes and their fixes:
- Backend not running. The upstream process
(PHP-FPM, Gunicorn, Node.js, Tomcat) crashed or
was never started. Verify with
systemctl status php-fpmor equivalent, then restart the service. Check the backend's own log for crash details. - Wrong upstream address or port. In nginx,
the
proxy_passorfastcgi_passdirective must point to the correct socket orhost:port. A mismatch between the configured port and the port the backend listens on produces an immediate 502. - DNS resolution failure. When
proxy_passreferences a hostname, DNS failures prevent nginx from reaching the backend. Test withdigornslookupfrom the proxy server. Consider using an IP address or a local/etc/hostsentry to eliminate DNS as a variable. - Upstream response too large for buffers. If
the backend sends headers exceeding the default
buffer size, nginx logs "upstream sent too big
header." Increase
proxy_buffer_size(orfastcgi_buffer_size) andproxy_buffers/fastcgi_buffersin the nginx configuration. - Backend closed the connection mid-response.
nginx logs "upstream prematurely closed
connection while reading response header from
upstream". The backend's own limit ended the
request, for example
request_terminate_timeoutin PHP-FPM killing a slow script, so raise or fix the limit on the backend. An expiredproxy_read_timeoutis a different event and produces 504, not 502. - Firewall blocking. A host-level firewall
(
iptables,nftables, security groups) on the backend server blocking the proxy's IP causes connection refused. Verify connectivity withcurl http://backend:port/from the proxy.
In Apache reverse proxy setups, check
ProxyPass/ProxyPassReverse directives and
confirm mod_proxy and mod_proxy_http are
enabled. Run apachectl configtest before
restarting.
After applying fixes, run nginx -t to validate
configuration, then reload.
Code references
.NET
HttpStatusCode.BadGateway
Rust
http::StatusCode::BAD_GATEWAY
Rails
:bad_gateway
Go
http.StatusBadGateway
Symfony
Response::HTTP_BAD_GATEWAY
Python3.5+
http.HTTPStatus.BAD_GATEWAY
Java
java.net.HttpURLConnection.HTTP_BAD_GATEWAY
Apache HttpComponents Core
org.apache.hc.core5.http.HttpStatus.SC_BAD_GATEWAY
Angular
@angular/common/http/HttpStatusCode.BadGateway
See also
- RFC 9110: HTTP Semantics
- Google: HTTP status codes and network errors
- AWS: Troubleshoot your Application Load Balancers, HTTP 502
- Cloudflare: Error 502 or 504
- 503 Service Unavailable
- 504 Gateway Timeout
- 500 Internal Server Error
- HTTP status codes