TodayTools

Reverse proxy and load balancer

How Reverse proxy works

One front door for many servers

Donate
L7ApplicationPort 443TCP
What brings you here?

Watch it happen

How much detail

One public front door: the proxy ends TLS and spreads requests across two app servers.

Ravi's phone
Internet
Reverse proxy
App server 1
App server 2
InternetCableSetupRequestResponseEncrypted

5 steps, 10 packets

First up: TLS to the proxy. Press Play to watch it happen, one step at a time.

0 of 50/5

What is Reverse proxy?

A reverse proxy is the public front door of a website. Visitors connect to it, it handles HTTPS, and then it passes each request to one of several app servers hidden on a private network. The visitor only ever sees one address.

This lets a site add or remove servers without anyone noticing, keep running when one server breaks, renew certificates in one place and block floods and bad requests before they reach the app. Most large sites, and many small ones, work this way.

Think of it like this. A reverse proxy is like a restaurant host at the door. Guests only talk to the host, who seats each party at a free table and sends orders to whichever cook has time. If one cook goes home sick, the host simply stops sending orders to that station.

Step by step

Normal

5 steps

One public front door: the proxy ends TLS and spreads requests across two app servers.

  1. 1

    TLS to the proxy

    The certificate lives here

    shop.example.com points to the reverse proxy. The phone runs its TLS handshake with the proxy, which holds the site's certificate and private key. The app servers do not need any.

  2. 2

    Request decrypted

    TLS termination

    The proxy decrypts the request, reads the path and headers, and decides where it should go. It can also add security headers, compress responses and block bad requests here.

  3. 3

    Forward to an app

    Plain HTTP on the private network

    The proxy sends the request to App server 1 on port 8080 over the internal network. It adds X-Forwarded-For and X-Forwarded-Proto so the app knows who asked and how.

  4. 4

    Response back

    Encrypted again for the phone

    The app answers the proxy, and the proxy encrypts the answer for the phone. The phone never learns the app server exists.

  5. 5

    Load balancing

    The next request goes to server 2

    With round robin, the next request goes to App server 2. Other methods pick the server with the fewest open connections, or keep each visitor on one server.

Server down

4 steps

App server 2 crashes mid-request: the user gets a 502, health checks take it out, and traffic moves to server 1.

  1. 1

    Request to server 2

    POST /checkout

    Ravi taps Pay. The proxy sends the POST to App server 2, as round robin says.

  2. 2

    Crash and 502

    Bad Gateway

    App server 2 crashes while handling it and its connection resets. A POST may already have done something, so the proxy does not retry it blindly. It answers 502 Bad Gateway. A server that never answers at all would give 504 Gateway Timeout instead.

  3. 3

    Health checks

    GET /healthz every few seconds

    The proxy checks each app server on its own. App server 2 now refuses connections. After a few failed checks in a row, the proxy marks it down.

  4. 4

    Traffic moves

    Only server 1 gets requests

    Ravi tries again and the request goes to App server 1, which works. When server 2 passes its checks again, it is put back in the rotation.

Flood blocked

3 steps

A bot hammers the login page, and the proxy turns it away before the app servers feel it.

  1. 1

    A flood starts

    Dozens of logins per second

    A bot tries many passwords on /login from one address. The proxy counts requests per address for each path.

  2. 2

    Limit reached

    429 Too Many Requests

    Above the limit, here 5 logins a minute, the proxy answers 429 itself with a Retry-After header. The app servers never see these requests.

  3. 3

    Everyone else is fine

    Normal visitors pass

    Ravi's requests come from another address and stay under the limit, so they go through as usual.

Myths and mix-ups

  • A 502 does not mean the whole site is down; one server behind the proxy failed.
  • A reverse proxy is not a forward proxy; it works for the website, not for the visitor.
  • The app server does not see your real IP address unless the proxy passes it on.
  • TLS termination at the proxy is not a hole when the inside network is private, but many sites still encrypt that hop too.

Try it yourself

Run these on your own computer to see Reverse proxy at work.

  • Spot a proxy in the headers

    curl -sI https://example.com/

    Headers like Via, Server or Age often show a proxy or cache in front.

  • Time each phase

    curl -s -o /dev/null -w 'connect %{time_connect} tls %{time_appconnect} first byte %{time_starttransfer}\n' https://example.com/

    The gap between TLS and first byte is mostly the app server behind the proxy.

  • Run a quick backend

    python3 -m http.server 8080

    A tiny app server on port 8080 you can put behind a reverse proxy to experiment.

Reverse proxy at a glance

Layer
Application (7); some balancers work at 4
Public port
443 (and 80 to redirect)
Transport
TCP, often QUIC for HTTP/3 too
Main jobs
TLS termination, load balancing, caching, filtering
Balancing
Round robin, least connections, sticky sessions
Visitor's address
Passed in X-Forwarded-For or Forwarded
Error codes
502 Bad Gateway, 503 Unavailable, 504 Timeout
Standard
A role, not a protocol; it speaks plain HTTP

Defined inRFC 9110 (HTTP semantics, 502, 503, 504, Via)RFC 7239 (Forwarded header)RFC 6585 (429 Too Many Requests)

Header fields

What Reverse proxy puts in its messages, with example values from the stories above.

HTTP/1.1

Request
GET /healthzA tiny page that says OK only if the app and its database work.
X-Forwarded-For
203.0.113.80Tells the app the visitor's real address, which it would otherwise never see.
X-Forwarded-Proto
httpsThe app gets plain HTTP, so this says the visitor actually used HTTPS.
Status
200 OK
Content-Type
application/json

Test yourself

Question 1 of 5Score 0

What does TLS termination mean?

Questions people ask

What is the difference between a reverse proxy and a load balancer?

A load balancer spreads traffic across servers. A reverse proxy does that and more: TLS, caching, compression and request rules. Many products do both, so the names overlap.

What does 502 Bad Gateway mean?

The proxy reached a server behind it, but got no valid answer, often because the app crashed or restarted. Try again in a moment. Site owners should check the app server's logs.

What is the difference between 502 and 504?

502 means the server behind the proxy gave a broken answer or refused. 504 means it did not answer in time.

Does a reverse proxy hide my server's IP address?

Yes, visitors only see the proxy's address. Keep the app servers firewalled so they only accept connections from the proxy.

Is a CDN a reverse proxy?

Yes. A CDN is a large network of reverse proxies placed near visitors, which cache content and pass the rest to your own servers.

Donate