Proxies, reverse proxies and a first look at load balancing
What breaks
Section titled “What breaks”The salon booking page from lesson 1.1 runs on one server. It listens on port 443, the default for HTTPS (RFC 9110), so a customer’s link needs no port number. It works.
Then the product grows. Owners need a dashboard to manage their bookings: a second program. Both call an API: a third. All three want port 443, because nobody types a port into a phone. You start the dashboard and the operating system refuses; the Linux manual’s wording is “The given address is already in use.” Two programs cannot listen on the same address and port. (A socket option lets copies of one program share a port; three different apps is not that case.)
So you give each app its own server, its own public address and its own certificate. Three certificates to renew, three sets of logs, three machines open to the internet.
Then evenings get busy, and the booking page needs two servers. How do customers reach both? You put both addresses in DNS, and resolvers hand them out in turn. At 7 p.m. server B crashes. DNS does not know. It keeps giving out B’s address, and every phone that already looked it up keeps it until the TTL runs out (lesson 1.1). Some customers get a blank page; others are fine. You cannot fix it quickly, because the bad answer sits in caches you do not own. RFC 1794 named both problems in 1995: early DNS round robin “didn’t do much with regard to the particular loads on the target machines”, and a machine can crash “during the period within the TTL”.
What broke? The client was choosing the server. A phone knows a name and an address. It does not know which server is alive, which one is busy, or which app owns which name. The fix is to stop letting it choose: put one program in front that the phone talks to, and let that program choose. That program is a reverse proxy.
Terms used in this lesson:
- An origin server really holds the answer: the booking app, the dashboard, the API. A backend is an origin server that sits behind a proxy. (nginx calls a group of them an “upstream”.)
- A forward proxy is chosen by the client and sends its requests on for it. RFC 9110 just calls it a “proxy”.
- A reverse proxy is chosen by the server’s owner. To the client it looks like the origin server; behind it, it forwards requests to backends. RFC 9110 calls it a “gateway”.
- A tunnel passes bytes both ways without reading them.
- A load balancer spreads requests across backends that do the same job. A reverse proxy is often also a load balancer.
- A health check is how a load balancer decides that a backend is alive.
- TLS termination is where TLS ends: the program that holds the certificate and decrypts.
Mental model
Section titled “Mental model”A proxy is a stand-in. A forward proxy stands in for clients; the client chooses it. A reverse proxy stands in for servers; the owner chooses it, and the client thinks it is the server.
Hold one more fact next to it: a proxy is two connections, not one. The phone has a connection to the proxy; the proxy opens its own to the backend. RFC 9110 says a message through three intermediaries passes “through four separate connections”. Everything a proxy can do, and everything it costs, comes from standing between those two connections.
Diagram
Section titled “Diagram”The salon platform with a reverse proxy in front. A phone asks for a salon’s booking page while booking server B is down.
The phone never learns that A, B or D exist. It sees one address and one certificate.
Forward and reverse
Section titled “Forward and reverse”A forward proxy sits near the clients. A company sets every laptop to send its web traffic through one: RFC 9110 says proxies “group an organization’s HTTP requests through a common intermediary for the sake of security services, annotation services, or shared caching”.
What about HTTPS, which the proxy cannot read? The browser asks it to become a tunnel. It sends
CONNECT salon.example.com:443, and the proxy opens a connection to that server and then restricts
itself to “blind forwarding of data, in both directions”. TLS runs end to end through the tunnel. The
proxy sees the host and port, nothing else.
| Forward proxy | Reverse proxy | |
|---|---|---|
| Who chooses it | the client, by configuration | the server’s owner, by DNS |
| Who it stands in for | clients | servers |
| Who knows it is there | the client | the owner; the client does not |
| Typical jobs | filtering, shared caching, one exit for an organisation | one front door, routing, TLS, load balancing |
What the reverse proxy does for the salon platform
Section titled “What the reverse proxy does for the salon platform”Walk the request in the diagram, one job at a time.
One front door. DNS for every name points at the proxy. Only the proxy listens on 443; the apps listen on private addresses. The port clash from the opening is gone, and so are two of the three public machines.
Routing by name. How does the proxy know the request is for the booking app? The same way the
server did in lesson 1.1: SNI at the TLS step, Host at the HTTP step. A proxy that reads HTTP can
also route by path, so /api/ could go to the API on the same name.
TLS in one place. The proxy holds the certificates and does the handshake. RFC 7239 calls this “crypto offload”. What happens to the traffic behind it is a trade-off, below.
Hiding the backends. You can add or replace a backend without a DNS change, so no cache holds a stale answer about it.
What it costs the backend. The backend’s connection comes from the proxy, so the backend sees the
proxy as the caller. RFC 7239: proxies “make the requests appear as if they originated from the
proxy’s IP address”. The app loses the customer’s address, which it needs for “diagnostics, access
control, and abuse management”. The fix is for the proxy to write the original address into a
header. The standard one is Forwarded, as in Forwarded: for=192.0.2.60;proto=http;by=203.0.113.43.
The one you will meet most is X-Forwarded-For, which RFC 7239 calls “non-standard” and MDN calls
the “de-facto standard”. Each proxy appends the address its connection came from, so the list reads
client first, most recent proxy last. Whether to believe that list is the hardest trade-off here.
A first look at load balancing
Section titled “A first look at load balancing”Spreading load is the job RFC 9110 names when it lists “load balancing of HTTP services across multiple machines” as a reason gateways exist. Server B is down. Which backend gets the next request, and how does the proxy know B is dead? Lesson 2.3 implements the algorithms; this is the shape.
Choosing a backend. nginx’s guide lists three methods:
- Round robin: each request goes to the next backend in turn. It is nginx’s default, and it assumes every request costs about the same.
- Least connections: the “next request is assigned to the server with the least number of active connections”. The guide says it is fairer “when some of the requests take longer to complete”.
- IP hash: a hash of the client’s address picks the backend, so the same client lands on the same server “except when this server is unavailable”. That is a sticky session. You need it only when a server keeps a client’s state in its own memory; lesson 2.1 argues for not doing that.
Knowing a backend is dead. A passive health check watches real traffic. In nginx, a backend
whose request fails is marked failed and avoided “for a while”: by default after one failure
(max_fails is 1), for ten seconds (fail_timeout). Then nginx tries it again “with the live
client’s requests”. An active health check sends its own probe on a timer, so a dead backend can be
found before a customer finds it. In nginx, active checks are part of the paid NGINX Plus (checked
2026-11-29).
Retrying. When a backend fails before answering, nginx by default passes the request to the next
one: proxy_next_upstream defaults to error timeout. That is how the diagram’s phone got its page
from A. But requests “with a non-idempotent method (POST, LOCK, PATCH) are not passed to the next
server if a request has been sent to an upstream server”. Why? B may have saved the booking before
it died, and sending the POST to A could book the slot twice. It is the line lesson 1.1 drew for
0-RTT: repeat reads freely, never repeat a write you are not sure failed. Lesson 2.6 makes writes
safe to repeat.
Layer 4 or layer 7. Everything so far reads HTTP, so it is a layer 7 balancer, after the
application layer of the OSI model. AWS’s Application Load Balancer “functions at the application
layer, the seventh layer” and routes “based on the content” of the request. A layer 4 balancer
works on TCP or UDP connections: AWS’s Network Load Balancer “functions at the fourth layer”. It
forwards connections without reading the requests inside, so it cannot route by Host, retry one
request or add a header. In return it does less work per request, and it carries protocols other
than HTTP.
Real case
Section titled “Real case”Roost is a booking platform for businesses that sell their time, such as salons. Lesson 1.1 described its booking pages: one host name per business.
The problem. Roost runs several web apps, among them the customers’ booking pages and the owners’ dashboard. Each must answer on the standard HTTPS port under its own name, and a new business’s booking address must work the moment it signs up, with no change made by hand.
The design. Public requests for every app arrive at one reverse proxy, which passes each one to the right app by its host name. A new business’s address works without any change to the proxy. The proxy’s routing is a short text file kept in version control, so a change to it is reviewed, read as a diff and reverted like code.
The trade-off. One front door means one place to look after routing, and adding an app is one new route. The cost is shared fate: a wrong routing change, or a proxy that will not start, takes every app down at once, booking pages and dashboard together. And the apps now depend on the proxy for facts about the request they used to see directly, which the trade-offs below take apart.
Honest about scale. Roost is early-stage, and its proxy layer is sized for that. At larger scale the proxy itself needs redundancy, which lesson 2.2 takes up. This lesson has no traffic or failure measurements from it.
Trade-offs
Section titled “Trade-offs”One shared proxy, and its fate. Before, one booking server could die and the dashboard lived on. Now, if the proxy dies or takes a bad change, everything goes with it. Two fixes, each with a cost. Run two proxies and list both in DNS: you are back to the DNS failure from the opening, but for fewer, simpler machines (lesson 2.2 does the arithmetic). Or give each group of apps its own proxy: less shared damage, more to run. This course’s suggestion: share while the system is small, and split later where an outage would hurt different customers.
An extra hop. Inside a data centre a round trip is about 0.5 ms (lesson 0.3), small next to the
150 ms phone link from lesson 1.1. The real cost is new connections: by default nginx sends
Connection: close to the backend over HTTP/1.x, so each request may open a fresh TCP connection
unless you turn on keep-alive.
Where TLS ends. If TLS ends at the proxy, the hop to the backend is plain HTTP, readable by
anything on that network. That is acceptable only if you trust the network. You can re-encrypt, but
read the defaults: nginx’s proxy_ssl_verify is off, so it encrypts without checking the
backend’s certificate. That stops someone reading the traffic, not someone pretending to be the
backend. Or a layer 4 balancer can pass TLS through untouched, which keeps encryption end to end
and gives up everything that needs to read HTTP.
Believing the forwarded address. Can the app just read X-Forwarded-For? Only part of it. RFC
7239 warns the header “cannot be relied upon to be correct, as it may be modified … by every node
on the way to the server, including the client making the request”. A phone can send
X-Forwarded-For: 1.2.3.4 itself, and nginx’s $proxy_add_x_forwarded_for appends the real
address after it rather than replacing it. So count from the right, and trust only entries added by
proxies you run. MDN: “if there is only one reverse proxy, that proxy will add the client’s IP
address, so the rightmost address should be used.” Get it wrong one way and every customer looks
like the proxy. Get it wrong the other and any customer can claim any address. Both are silent.
Layer 4 against layer 7. Choose layer 7 to route by name or path, retry a single request or add headers: a web platform with several apps. Choose layer 4 for non-HTTP traffic, for TLS that must pass through unopened, or when per-request work costs too much.
Drill 1 — interview question
Section titled “Drill 1 — interview question”“What is the difference between a forward proxy and a reverse proxy? Our booking platform has a booking app on two servers, an owners’ dashboard and an API. Where would you put a reverse proxy, and what does it do for us?”
Model answer
The difference. Both forward requests for someone. A forward proxy is chosen by the client and
stands in for clients: a company routes its laptops through one for filtering or shared caching,
and for HTTPS it becomes a tunnel with CONNECT. A reverse proxy is chosen by the server’s owner
and stands in for servers; the client thinks it is the origin server.
Where it goes. In front of everything. DNS for every name points at the proxy, the only thing on 443. The apps sit on private addresses.
What it does. It terminates TLS, so certificates live in one place. It routes by Host, SNI or
path: booking names to the booking servers, the dashboard name to the dashboard. It spreads booking
requests across both servers. It health-checks them, so a dead server stops getting traffic in
seconds, not after DNS TTLs expire. It retries safe requests on the other server, never a POST that
may have been processed. It passes the customer’s address in Forwarded or X-Forwarded-For.
What it costs. A new single point of failure, so run two. And the apps must read the client address from a header, trusting only what our proxy added.
Rubric
- Distinguishes the two by who chooses the proxy and who it stands in for — 2
- Places the reverse proxy as the single public entry, apps on private addresses — 2
- Names at least three jobs: TLS, routing by name, load balancing, health checks — 2
- Explains why health checks beat DNS for a dead server (TTL caching) — 2
- Names a cost: single point of failure, lost client address, or retry of writes — 2
Total 10. 8 or more is a pass.
Drill 2 — interview question
Section titled “Drill 2 — interview question”The interviewer follows up: “You add the proxy. Next day the search API’s limit of 100 requests a
minute per address either never triggers, or blocks every customer at once. Logs show every request comes
from 10.0.0.5. What happened, and how do you fix it without letting attackers dodge the limit?”
Model answer
What happened. 10.0.0.5 is the proxy. Every backend connection comes from it, so a limit keyed
on the connection’s address sees one client. Whether it never trips or trips for everyone depends
on how it counts; either way it is counting the proxy.
The fix. Have the proxy pass the original address: append it to X-Forwarded-For, or set
Forwarded: for=. Then have the app read only the part our proxy wrote. A client can send its own
X-Forwarded-For: 1.2.3.4, and the proxy appends the real address after it. With one proxy, take
the rightmost entry; with two we run, the second from the right. Never the leftmost, which the
client controls.
Make it hold. The backend must be reachable only through the proxy, or anyone can connect directly and send any header; the app trusts the header only on connections from the proxy. Then test it: send a fake header from outside and check the logged address is your real one.
Rubric
- Identifies
10.0.0.5as the proxy and explains why every request shares it — 2 - Proposes a forwarded header written by the proxy — 2
- Explains that the client can forge the header, and reads from the right — 3
- Requires backends to be reachable only through the proxy, or trusts the header only from it — 2
- Proposes a test that proves the fix — 1
Total 10. 8 or more is a pass.
Drill 3 — hands-on exercise
Section titled “Drill 3 — hands-on exercise”Build the diagram on your laptop with nginx (any recent version; stable was 1.30.8 on 2026-11-29) and two tiny backends.
- Make folders
aandb, each with anindex.htmlthat says “A” or “B”. Serve them withpython3 -m http.server 8001inaandpython3 -m http.server 8002inb. - Write an nginx config: an
upstreamblock naming127.0.0.1:8001and127.0.0.1:8002, and aserveron port 8080 that usesproxy_passto send requests to it. Start nginx. - Predict what six requests to
http://localhost:8080/return, then runcurl -s localhost:8080/six times. Which client address do the Python logs show, and why? - Stop B. Predict: does the next request fail, or return A? Run it and read nginx’s error log. Restart B. When does it get traffic again?
- Replace B with
nc -l 8002, which prints the raw request it receives. Send one request through the proxy. WhichHostandConnectionheaders arrived? Compare them with what curl sent and with nginx’sproxy_set_headerdocumentation. - Add
proxy_set_headerlines that pass the original host and append the client address. Sendcurl -H 'X-Forwarded-For: 1.2.3.4' localhost:8080/and read whatncreceives. Which entry would your app trust?
Write one sentence per step on what surprised you.
Exit test
Section titled “Exit test”Answer without notes.
- A forward proxy and a reverse proxy both forward requests. Name two ways they differ.
- Two booking servers are listed in DNS and one dies. Why do some customers keep failing for minutes, and what does a reverse proxy with health checks change?
- Your app behind a reverse proxy logs every customer as the same address. Why? How do you recover
the real address, and which
X-Forwarded-Forentry do you trust with one proxy in front? - A booking server dies after receiving a POST that books a slot. Should the proxy retry it on the other server? What about a GET?
- When would you choose a layer 4 balancer over a layer 7 one? Name one thing a layer 4 balancer cannot do.
Further reading
Section titled “Further reading”- RFC 9110, HTTP Semantics, section 3.7 (proxy, gateway, tunnel) and section 9.3.6 (
CONNECT) — https://www.rfc-editor.org/rfc/rfc9110. - RFC 7239, Forwarded HTTP Extension — https://www.rfc-editor.org/rfc/rfc7239. Why a proxy hides the client’s address, and why the header that restores it cannot be trusted blindly.
- nginx, Using nginx as HTTP load balancer — https://nginx.org/en/docs/http/load_balancing.html. The three methods and passive health checks; checked 2026-11-29.
- nginx, ngx_http_proxy_module — https://nginx.org/en/docs/http/ngx_http_proxy_module.html, and
ngx_http_upstream_module — https://nginx.org/en/docs/http/ngx_http_upstream_module.html.
Header defaults, retries,
proxy_ssl_verify,max_failsandfail_timeout. - AWS, What is an Application Load Balancer? — https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html, and What is a Network Load Balancer? — https://docs.aws.amazon.com/elasticloadbalancing/latest/network/introduction.html. Layer 7 against layer 4.
- RFC 1794, DNS Support for Load Balancing — https://www.rfc-editor.org/rfc/rfc1794. Balancing with DNS, and its limits.
- MDN, X-Forwarded-For — https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Forwarded-For. No specification exists for this header; MDN’s page is the usual reference.
- Linux
bind(2)andsocket(7)— https://man7.org/linux/man-pages/man2/bind.2.html, https://man7.org/linux/man-pages/man7/socket.7.html. Why two programs cannot share a port, and theSO_REUSEPORTexception.