In modern multi-tiered architectures, clients rarely connect directly to application servers. Instead, requests pass through reverse proxies, content delivery networks (CDNs), load balancers, or API gateways.

When one of these intermediary nodes fails to receive a valid HTTP response from the backend service behind it, it issues an HTTP 502 Bad Gateway status code.

Core meaning of HTTP 502

An HTTP 502 Bad Gateway response means that a server acting as a gateway or proxy contacted an upstream server to fulfill your request, but received an invalid or unparseable response from that upstream server.

The gateway is operational, but the communication exchange between the gateway and the upstream backend broke down.

What a 502 response tells you

Receiving an HTTP 502 status provides several diagnostic facts:

  1. The edge gateway is reachable: Your client connected to the outer proxy or gateway successfully.
  2. The failure occurred upstream: The issue lies in the link between the proxy and the internal backend service.
  3. The upstream response was corrupted or dropped: The backend service did not return a valid, complete HTTP response stream.
  4. 502 does not pinpoint the exact failure: The error indicates that the communication failed, but you must inspect upstream logs to identify the exact code exception or crash.

HTTP exchange

Intermediary Proxy 502 Response

broken

Request

GET /v1/orders HTTP/1.1
Host: api.example.test
Accept: application/json

Response

HTTP/1.1 502 Bad Gateway
Date: Thu, 10 Sep 2026 10:00:00 GMT
Content-Type: application/json
Via: 1.1 gateway.example.test

{"error": "bad_gateway", "message": "The proxy server received an invalid response from an upstream server."}

The client connected to the API gateway successfully, but the upstream order microservice crashed during execution. The gateway returned status 502 Bad Gateway to the client.

Upstream Protocol Error

unexpected

Request

GET /v1/analytics HTTP/1.1
Host: api.example.test
Accept: application/json

Response

HTTP/1.1 502 Bad Gateway
Date: Thu, 10 Sep 2026 10:00:00 GMT
Content-Type: application/json
Via: 1.1 gateway.example.test

{"error": "bad_gateway", "message": "Upstream response headers were invalid or exceeded buffer limits."}

The upstream application generated unparseable response headers that the reverse proxy rejected before relaying data to the client.

Gateway, proxy, and upstream roles

To understand status 502, it is helpful to visualize the multi-hop request path:

[Client] ---> (HTTP Request) ---> [Reverse Proxy / Gateway] ---> (Internal Request) ---> [Upstream Server]
                                          |                                                    |
[Client] <--- (502 Bad Gateway) <---------+ <--- (Invalid / Dropped Response / Crash) <-------+
  1. Client: Dispatches the HTTP request to the public hostname (api.example.test).
  2. Gateway / Proxy: Terminates TLS, handles routing, and forwards the request to an internal upstream server (http://10.0.0.15:8080).
  3. Upstream Server: Processes the business logic and attempts to send back an HTTP response. If the process crashes mid-stream or sends corrupted bytes, the gateway generates a 502 response for the client.

Common causes of HTTP 502

Upstream Process Crash or Abrupt Termination

The backend application server crashed, restarted, or terminated the connection while generating the HTTP response payload.

Malformed or Oversized Upstream HTTP Headers

The upstream service returned response headers that exceeded proxy buffer limits or violated HTTP protocol formatting standards.

Upstream Connection Reset or Protocol Mismatch

The TCP connection between the gateway and upstream server was abruptly reset, or TLS handshake negotiation failed between the two hops.

Port or Socket Misconfiguration

The reverse proxy forwarded requests to an inactive local socket or closed internal port where no application process was listening.

HTTP 502 vs. HTTP 504

Both status codes are issued by intermediary gateways, but they reflect fundamentally different failure modes:

  • HTTP 502 Bad Gateway: The gateway received an invalid, malformed, or abruptly terminated response from upstream. The upstream responded, but the response could not be parsed or completed.
  • HTTP 504 Gateway Timeout: The gateway received no response at all before its configured timeout deadline expired.

If the upstream process crashes instantly, you often see 502. If the upstream process hangs indefinitely on a heavy computation, you see 504.

How to investigate a 502 safely

Follow a structured diagnostic flow to pinpoint the failing component:

  1. Check proxy logs: Review the gateway access and error logs to identify the specific upstream IP, port, or socket that failed.
  2. Inspect upstream service logs: Look for unhandled exceptions, fatal process crashes, out-of-memory (OOM) events, or core dumps in the backend service.
  3. Audit response header sizes: Ensure your upstream application is not emitting excessively large headers (such as oversized session cookies) that violate proxy buffer settings.
  4. Test upstream connectivity internally: Issue a local request directly to the upstream server within the private network to verify its standalone behavior.

What to check before changing code

Before altering client request logic or gateway configurations, verify:

  1. Inspect reverse proxy access and error logs: Identify whether the proxy recorded an upstream connection reset, invalid header error, or premature socket closure.
  2. Verify upstream application process health: Confirm that the backend application service is running, accepting connections, and not crashing on specific request inputs.
  3. Check response header and buffer constraints: Verify that custom upstream response headers (such as large cookies or metadata) do not exceed proxy buffer limits.
  4. Distinguish 502 Bad Gateway from 504 Gateway Timeout: Determine whether the upstream server replied with an invalid response (502) or failed to respond within the deadline (504).

How to verify the fix

Inspect the gateway endpoint and observe response behavior using curl:

curl -i https://api.example.test/v1/orders

Check the response headers for proxy identification headers (such as Via or Server) and confirm whether the server returns 200 OK once the upstream service is healthy.

Key takeaway

HTTP 502 Bad Gateway means a reverse proxy or gateway received an invalid or abruptly closed response from the upstream application server. Debugging 502 requires inspecting the proxy-to-upstream communication layer and checking backend process logs rather than modifying client-side code.