A forward proxy works for clients, a reverse proxy for servers A forward proxy sits next to clients, for example a company egress proxy. Clients are configured to use it, it reaches any site for them, and those sites see the proxy instead of the client.
A reverse proxy sits in front of your servers. Clients do not know it is a proxy. They connect to one public address, and the proxy decides which backend answers. Load balancers, Ingress controllers and API gateways are all reverse proxies.
An L4 balancer moves connections, not requests An L4 load balancer works with TCP or UDP. It sees source and destination IP and port, picks a target when a connection opens, and keeps all bytes of that connection on the same target. It cannot read the URL or headers.
That makes it fast and protocol-agnostic: databases, gRPC, MQTT and TLS all pass through unchanged. Cloud network load balancers, IPVS and HAProxy in mode tcp work this way.
An L7 proxy reads each HTTP request An L7 reverse proxy ends the client's HTTP connection and parses every request. It sees the Host, the path, headers and cookies, so it can route /api to one pool and / to another, rewrite paths, add headers, retry or return errors itself.
It then sends the request over a separate connection to the backend. Two connections, two sets of timeouts. This is what nginx, HAProxy in mode http and Envoy do, and what an Ingress or Gateway controller runs underneath.
Key idea L4 decides once per connection. L7 decides per request, so one keep-alive connection can still spread across backends.
Round robin, least connections, consistent hash Round robin takes backends in turn, and is the default in nginx and Envoy. It is fine when requests cost about the same. Least connections (least_conn in nginx, leastconn in HAProxy, LEAST_REQUEST in Envoy) sends to the backend with the fewest active requests, which helps when some requests are slow.
Consistent hashing maps a key such as the URL or a user ID to a backend. The same key keeps landing on the same backend, which is good for caches, and adding or removing a backend moves only a small share of keys instead of reshuffling all of them.
A dead backend must leave the pool App 3 stops answering. Active health checks probe each backend on a timer, for example GET /healthz, and remove it after several failures. HAProxy uses check on a server line and Envoy health_checks on the cluster. Open-source nginx has no active checks.
Passive checks watch real traffic instead. In nginx, max_fails failures within fail_timeout (by default 1 and 10s) mark a server unavailable for fail_timeout. Envoy calls this outlier detection. Passive checks need no extra traffic but only notice after real requests fail.
Watch out Use both when you can: active checks catch a dead backend before users do, passive checks catch one that answers /healthz but fails real requests.
Sticky sessions pin users, and that hurts later Sticky sessions send a client to the same backend every time, by a cookie the proxy sets or by hashing the client IP. Apps that keep sessions in memory seem to need this.
The cost shows up later. Load becomes uneven when a few heavy users stick to one backend. When that backend dies or is redeployed, its users lose their sessions. Draining takes long because pinned users keep coming back. Keep session state in a shared store or a token, so any backend can serve any request.
The app sees the proxy, not the client At L7 the backend's TCP peer is the proxy, so logs, rate limits and geo checks all see 10.0.0.5. The proxy adds X-Forwarded-For: 203.0.113.7, and each later proxy appends its peer. The standard Forwarded header (RFC 7239) carries the same idea as for=203.0.113.7.
Clients can send their own X-Forwarded-For, so the left-most value can be fake. Trust only the entries added by proxies you run: walk the list from the right and stop at the first address that is not your proxy. In nginx that is set_real_ip_from with real_ip_recursive on. At L4 there is no header, so use the PROXY protocol or a balancer that keeps the source IP.
Watch out Never take the first X-Forwarded-For value at face value. Configure which proxies are trusted.
Terminate TLS at the proxy, or pass it through With TLS termination, the proxy holds the certificate and decrypts. Only then can it read the path and headers, add X-Forwarded-For and route at L7. It can send plain HTTP to the backend or open a new TLS connection to it (re-encryption).
With TLS passthrough, the proxy forwards encrypted bytes untouched. It can only route by IP, port and the SNI name in the ClientHello, and the backend holds the certificate. Use it when the backend must see the client's TLS itself, for example for end-to-end mTLS.
502 is a bad answer, 504 is no answer in time 502 Bad Gateway means the proxy reached for the upstream and got something unusable: connection refused, connection reset, or an invalid response. Typical causes are a crashed app, the wrong port, or the app closing a connection the proxy was about to use.
504 Gateway Timeout means the upstream accepted the request but did not answer within the proxy's timeout. In nginx, proxy_read_timeout defaults to 60s and counts the gap between two reads, not the whole response. The work may still finish on the backend. Envoy returns 503 when no healthy upstream is left.
Watch out 502: look at the app and the connection. 504: look at slow queries and the timeout chain. Each hop's timeout should be longer than the one behind it.
Reuse upstream connections, and get idle timeouts in order Opening a new TCP and TLS connection for every request wastes time and ports. Proxies keep a pool of idle keep-alive connections to each backend and reuse them. In nginx this is the keepalive directive in the upstream block, which needs HTTP/1.1 to the upstream (the default only since nginx 1.29.7).
Idle timeouts must line up. If the app closes idle connections after 5s and the proxy keeps them for 60s, the proxy sometimes sends a request on a connection the app just closed, and the user gets a random 502. Make the backend's idle timeout longer than the proxy's.
One address outside, choices and checks inside An L4 balancer moves connections by IP and port. An L7 proxy reads each request, picks a healthy backend with an algorithm, and adds headers that carry the real client.
Most surprises come from the edges between hops: a trusted-proxy list for X-Forwarded-For, where TLS ends, timeouts that grow toward the client, and idle timeouts that shrink toward the proxy.
Summary When users see 502 or 504, read the proxy's access and error logs first. They say which upstream it tried, how long it waited and why it gave up.