Skip to main content
App basics: HTTP, auth, dataLesson 3 of 11

Reverse Proxies and Load Balancing

Almost every request you serve passes through a reverse proxy such as nginx, HAProxy or Envoy before your code sees it. Follow one request through an L4 load balancer and an L7 proxy and see how a backend is chosen and checked, what headers get added, and where 502 and 504 come from.

Layer—
Proxy sees—
App sees client as—
Response—
Your serversClientIP 203.0.113.7Forward proxyActs for clientsL4 load balancerSees IP:portReverse proxyL7 · host, path, headersApp 110.0.1.11:8080App 210.0.1.12:8080App 310.0.1.13:8080Outboundshop.example.com
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Forward proxy vs reverse proxy, L4 vs L7
  • Round robin, least connections and consistent hashing
  • Active and passive health checks, and why to avoid sticky sessions
  • X-Forwarded-For and which client IP to trust
  • TLS termination vs passthrough, timeouts, 502 vs 504, keep-alive pools

L4 or L7, and what each status code means​

L4 load balancerL7 reverse proxy
Works onTCP or UDP connectionsHTTP requests
SeesIP and port (SNI with TLS passthrough)Host, path, headers, cookies
DecidesOnce per connectionPer request
Client IP for the appKept or via PROXY protocol, depending on modeX-Forwarded-For or Forwarded header
Can doAny protocol, very fastPath routing, rewrites, retries, auth, header changes
ExamplesCloud network LB, IPVS, HAProxy mode tcpnginx, HAProxy mode http, Envoy, cloud application LB
Status from the proxyMeaningCommon causeCheck
502 Bad GatewayUpstream refused, reset or sent an invalid responseApp crashed, wrong port, upstream closed an idle keep-alive connectionApp logs, upstream port, idle timeouts
503 Service UnavailableNo backend can take the requestAll backends failed health checks, or none are configuredHealth check results, endpoints behind the proxy
504 Gateway TimeoutUpstream did not answer within the proxy timeoutSlow query or downstream call, timeout set too lowUpstream response time, proxy_read_timeout or the route timeout

Try it yourself​

curl -sv -o /dev/null https://example.com/ 2>&1 | grep -iE '^< (server|via|x-cache)' # Which proxy answered
curl -s -o /dev/null -w '%{http_code} %{time_total}s\n' https://example.com/ # Status and total time
curl -s -H 'X-Forwarded-For: 1.2.3.4' http://localhost:8080/ip # Does your app trust a forged header? (your own test endpoint)
nginx -T 2>/dev/null | grep -E 'proxy_(connect|read|send)_timeout|keepalive|real_ip' # Effective nginx timeouts and trust settings
echo 'show servers state' | socat stdio /var/run/haproxy.sock # HAProxy backend state (needs a stats socket)
curl -s localhost:9901/clusters | grep health_flags # Envoy admin: health of each upstream host
kubectl logs -n ingress-nginx deploy/ingress-nginx-controller | grep -E '" 50[234] ' # 502/503/504 in Ingress access logs

Check yourself

  1. A user's requests all arrive on one keep-alive connection. Behind an L4 balancer they always hit the same backend, behind an L7 proxy they spread out. Why?
  2. Your app reads the client IP from the first value of X-Forwarded-For to rate-limit logins. What is wrong?
  3. Users get a few random 502s per hour. The app is healthy. The app closes idle connections after 5s and the proxy keeps idle upstream connections for 60s. What is going on?

Further reading: nginx upstream module · HAProxy configuration manual · Envoy load balancing · RFC 7239: Forwarded · When You Type an Address · How a Request Reaches a Pod · Ingress Rules and TLS