Sub topic 14
Trade-offs and common pitfalls
This chapter assumes you know HTTP methods and headers, how TLS works at a basic level, and the roles of forward and reverse proxies from earlier chapters. A proxy solves real problems, but it also adds a hop, a component that can fail, and a place where information about the client gets lost. Most proxy incidents come from a small set of causes.
What a proxy costs you
Every proxy adds work to each request. The cost is usually small, but it is never zero.
| Cost | What happens | How to limit it |
|---|---|---|
| Latency | One more network hop and one more parse of the request | Keep proxies close to backends, reuse upstream connections |
| Single point of failure | If the only proxy goes down, every backend behind it is unreachable | Run at least two instances behind a shared address |
| Lost client identity | Backends see the proxy's IP, not the client's | Forward headers and configure backends to trust them |
| Extra configuration | Timeouts, buffers and headers now exist in two places | Keep one documented set of values |
| Hidden failures | Errors may come from the proxy, not the app | Log both sides and tag proxy-generated errors |
Pitfall 1: Wrong client IP
Behind a reverse proxy, the TCP connection to the backend comes from the proxy. Without help, the backend logs the proxy's address for every request. Rate limits and audit logs then treat all users as one.
The proxy fixes this by adding headers, as in the earlier configuration:
X-Forwarded-For: 203.0.113.7, 10.0.0.5
X-Forwarded-Proto: https
Forwarded: for=203.0.113.7;proto=https;host=example.comThe leftmost entry in X-Forwarded-For is the address the first proxy saw. Clients can send this header themselves, so a client can write a false value at the start of the list.
Pitfall 2: Losing the Host and scheme
If the proxy does not pass Host, the backend may see its own internal name. Generated links and redirects then point to internal addresses. If TLS ends at the proxy and the backend is not told through X-Forwarded-Proto, the backend thinks the request was plain HTTP. It may redirect to HTTPS forever, because the proxy keeps sending plain HTTP.
Pitfall 3: Caching the wrong thing
A cache is shared by all users. If you cache a response that depends on who is asking, one user can receive another user's data. Cache only content that is the same for everyone, such as files under /static/. Check that responses with cookies or Authorization headers are not cached by default. Also decide how long stale content is acceptable, because cached copies do not reflect backend changes until they expire.
Pitfall 4: Timeout mismatches
The proxy and backend each have timeouts. If the proxy gives up before the backend finishes, the client gets a 504 Gateway Timeout while the backend keeps working. For non-idempotent requests, a client retry can then repeat the action. Set the proxy timeout higher than the backend's expected worst case, and make retried operations safe.
Pitfall 5: Misleading health and failure handling
Passive failure detection, such as max_fails=3 fail_timeout=30s, only notices a failure after real requests fail. Some users see errors before the backend is removed. Also, a proxy that removes every backend returns errors for all traffic. Test what happens when each part fails.
Pitfall 6: Treating TLS termination as free
When the proxy ends TLS, traffic from the proxy to the backend is plain text. On a trusted private network that may be acceptable. Where it is not, re-encrypt to the backend. The proxy also holds the private key, so protect that file.
Choosing whether to add a proxy
Add one when it gives a clear benefit: load balancing, TLS handling in one place, caching, or egress control. Skip it when a single backend serves a small internal tool and the extra component would only add failure modes.
Exercise
A backend behind your reverse proxy logs 10.0.0.5 for every request, and users report an endless redirect loop to HTTPS. Name the likely cause of each problem and the fix.
Show answer
The logged address is the proxy's, because the backend sees the proxy's connection. Pass X-Real-IP or X-Forwarded-For from the proxy and configure the backend to read it from trusted proxies only. The redirect loop happens because TLS ends at the proxy, so the backend sees plain HTTP and keeps redirecting. Send X-Forwarded-Proto: $scheme from the proxy and make the backend honor it.
Exercise
Why is it risky to cache a response that was generated for a logged-in user, and what can you do to avoid it?
Show answer
The cache is shared, so another user requesting the same URL could receive the first user's private content. Cache only responses identical for all users, such as static files, and exclude requests with cookies or Authorization headers.