502 Bad Gateway
HTTP response status code 502 Bad Gateway is a server error indicating the server, while acting as a gateway or proxy, received an invalid response from the upstream server.
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. The time to resolve this problem is unpredictable.
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.
SEO impact
Googlebot treats
5xx errors as a
signal to temporarily reduce the crawl rate
across the site. Previously indexed URLs are
preserved initially, but extended downtime
causes Google to drop them from the index. Any
content returned with a 502 response is
ignored. Google treats 500, 502, and
503 identically in this regard. A
5xx error on the
robots.txt
file is far more severe, even when every other
page on the site returns 200. Google halts
crawling for up to 12 hours, then falls back to
a cached copy for up to 30 days. If the error
persists, Google fully deindexes the site.
Bingbot also treats
5xx responses as
crawl failures. Persistent
5xx errors prevent
indexing in Bing.
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>
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 timeout. A slow backend triggers a
502 when the connection closes mid-response.
Increase
proxy_read_timeoutin nginx orProxyTimeoutin Apache. Also raise the backend's own timeout (e.g.request_terminate_timeoutin PHP-FPM). - 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
Takeaway
The 502 Bad Gateway status code is a server error indicating an upstream server returned an invalid response to the gateway or proxy.
See also
- RFC 9110: HTTP Semantics
- Google: HTTP status codes and network errors
- 503 Service Unavailable
- 504 Gateway Timeout
- 500 Internal Server Error
- HTTP status codes