HTTPQUERY
English

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. 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.

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: example.com

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:

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