An independent engineering field journalUnderstanding the systems behind the screen
Tested experiment   ·   November 2026

The service returned 502. That’s actually an important clue.

A controlled local reverse-proxy experiment shows why a refused TCP connection and an HTTP 502 point to different failure boundaries.

Documenting the mechanism. Testing assumptions. Keeping the unknowns visible.

A failed request can fail before HTTP even begins.

The phrase “website error” blurs an important distinction. Did the client establish a connection and receive an HTTP response? Or did it fail to connect before any HTTP status existed? Those symptoms direct the investigation toward different components.

I tested this using a fixed-origin Python HTTP reverse proxy on a Mac and a separate Python origin server. Both ran on localhost, but they were independent processes using independent TCP connections.

One machine, two services, two TCP connections.

Reproducible local topology
curl client
   |
   |  TCP #1
   v
HTTP proxy 127.0.0.1:8080
   |
   |  TCP #2
   v
HTTP origin 127.0.0.1:8000
All endpoints are localhost. This is a deliberately limited failure experiment, not an enterprise reverse-proxy deployment.

The proxy forwards a request for /README.md to the origin. The origin serves the file when running. The experiment then deliberately stops one component at a time.

Four observations, four questions.

01 — Both services work: HTTP 200

With the proxy and origin running, the client requests the resource through port 8080 and gets HTTP/1.1 200 OK. It demonstrates a working end-to-end request path for that resource.

02 — The proxy is stopped: no HTTP response

The client cannot connect to port 8080. In this local experiment, curl reports a connection failure:

curl: (7) Failed to connect to 127.0.0.1 port 8080

No proxy process was there to generate an HTTP status. Calling this an “HTTP 502” would be incorrect.

03 — The proxy runs; the origin is stopped: HTTP 502

Now the client successfully connects to the proxy. The proxy tries to reach port 8000, cannot establish its upstream connection, and generates a response:

HTTP/1.1 502 Upstream server unavailable
Content-Type: text/html;charset=utf-8

The 502 status is evidence that an HTTP-speaking intermediary was reachable and answered, even though its upstream request failed.

04 — Restore origin: HTTP 200 returns

After restarting the origin, the same request through the same proxy succeeds again. This establishes a controlled failure-and-recovery sequence—not a high-availability claim.

What 502 means—and what it does not.

In the HTTP specification, a 502 Bad Gateway means a server acting as a gateway or proxy received an invalid response from an inbound server while fulfilling the request. Proxies may generate 502 responses in upstream failure situations, including the connection problem demonstrated here. Actual behavior depends on the proxy and its configuration.

Don't automatically blame DNS or the client whenever you see 502. You know the client received an HTTP response from something. Investigate the intermediary's logs, the configured upstream hostname or IP, its port, health, and TLS connection to the origin.

The first troubleshooting question isn't “What caused the 502?” It's “Who generated this response?”

Reproduce the failure and the recovery.

# Terminal A — start the origin
python3 -m http.server 8000 --bind 127.0.0.1

# Terminal B — start the proxy
python3 labs/http-proxy/proxy.py

# Terminal C — make the client request
curl -i http://127.0.0.1:8080/README.md

Stop the origin, repeat the request, and inspect both status and proxy logs. Start the origin again and confirm the response returns to 200. Full steps and real test evidence are in the published incident-style report.

Scope matters

This local experiment demonstrates a specific 200 → connection failure → 502 → 200 sequence. It does not test production load balancers, retries, timeouts, or all possible reasons a gateway emits 502.

Learning checkpoint

A browser shows 502. Is that evidence the browser failed to reach any server?

No. The status indicates an HTTP response was received. Determine which intermediary generated it, then inspect the next dependency in the request path.

← All articlesFollow the learning path ↗