Chapter 22 of 33

Proxies: Forward & Reverse

When you open a website from your college lab, your request may not go directly to that website. It may first pass through the college's proxy server, which checks whether the site is allowed. And when that request reaches a big website, it probably does not land on the application server directly either. It first hits a server such as Nginx, which decides which of many backend machines should handle it.

Both of these "middlemen" are proxies. The first one works for the client; the second one works for the server. In earlier chapters we already met special kinds of proxies without naming them that way: Content Delivery Networks (CDN), Load Balancing and API Gateway & Service Discovery. This chapter gives the general idea behind all of them.

A proxy server is an intermediary that receives requests from one party and forwards them to another, often inspecting, modifying, caching or filtering them on the way. A forward proxy acts on behalf of clients; a reverse proxy acts on behalf of servers.


Forward Proxy

A forward proxy sits in front of a group of clients. The clients are configured (by the user, the browser settings or the network admin) to send their requests to the proxy, and the proxy talks to the internet on their behalf.

   Clients (one organisation)            The Internet
  ┌──────────┐
  │ Laptop A │──┐
  └──────────┘  │     ┌───────────────┐       ┌─────────────┐
  ┌──────────┐  ├────→│ Forward Proxy │──────→│ example.com │
  │ Laptop B │──┤     └───────────────┘       └─────────────┘
  └──────────┘  │      (knows the clients)    (sees only the proxy's IP)
  ┌──────────┐  │
  │ Laptop C │──┘
  └──────────┘

The website sees the proxy's IP address, not the laptops'. The clients know they are using a proxy; the server usually does not care.

What Forward Proxies Are Used For

  • Access control and filtering: colleges and companies block certain sites or categories.

  • Caching: if 200 students download the same software update, the proxy can fetch it once and serve the copy to everyone else, saving the institution's bandwidth.

  • Logging and auditing: organisations record which sites are visited, for security or compliance.

  • Privacy: the destination server sees the proxy's IP instead of the user's.

  • Controlled egress: inside a data centre, backend servers are often not allowed to reach the internet directly; their outgoing calls must pass through an egress proxy that only allows approved destinations.

How HTTPS Passes Through a Forward Proxy

With HTTPS, the traffic is encrypted end to end, so the proxy cannot read it. The client asks the proxy to open a raw tunnel using the HTTP CONNECT method:

Client → Proxy:   CONNECT example.com:443 HTTP/1.1
Proxy  → Client:  HTTP/1.1 200 Connection Established
Client ⇄ Proxy ⇄ example.com:  encrypted TLS bytes, passed through blindly

The proxy knows which host you connected to, but not the pages or data inside. Some corporate proxies do "TLS inspection" by decrypting and re-encrypting traffic. That only works because the company installs its own root certificate on employees' machines; otherwise the browser would show a certificate warning.

Other variants you may hear about: a transparent proxy intercepts traffic at the network level without any client configuration (common in public Wi-Fi), and a SOCKS proxy (SOCKS5 is defined in RFC 1928) forwards raw TCP connections (and optionally UDP) rather than understanding HTTP.


Reverse Proxy

A reverse proxy sits in front of a group of servers. Clients think they are talking to "the website", but they are actually talking to the reverse proxy, which forwards each request to one of the hidden backend servers and passes the response back.

   The Internet                          Private network
                                         ┌────────────┐
  ┌────────┐      ┌───────────────┐ ┌───→│ App server │
  │ Client │─────→│ Reverse Proxy │─┼───→│ App server │
  └────────┘      │  (Nginx etc.) │ └───→│ App server │
                  └───────────────┘      └────────────┘
  sees only the proxy                    (no public IPs)

Here the client does not know a proxy exists. The DNS name of the website simply points to the reverse proxy. Popular reverse proxies include Nginx, HAProxy, Envoy, Caddy and Traefik, and cloud load balancers do the same job as a managed service.

What Reverse Proxies Are Used For

  1. Load balancing: spread requests across many backend servers and skip unhealthy ones (covered in depth in Load Balancing).

  2. TLS termination: the proxy holds the certificate and does the expensive HTTPS handshake and decryption. Backends receive plain HTTP on the private network (or re-encrypted traffic, if internal encryption is required). Certificates are managed in one place instead of on every server.

  3. Caching: responses for popular pages or static files can be served straight from the proxy (see Caching).

  4. Compression: gzip or Brotli compression of responses, done once at the edge.

  5. Security: backends have no public IP, so attackers cannot reach them directly. The proxy can block bad paths, enforce request size limits, hide server version headers, and act as a web application firewall.

  6. Rate limiting: reject clients that send too many requests (see Rate Limiting).

  7. Routing: send /api/* to the API servers, /static/* to a file server, and /blog/* to a different application, all under one domain.

  8. Absorbing slow clients: a client on a slow mobile network may take seconds to upload or download. The proxy buffers the data, so the expensive application server is busy only for the few milliseconds it takes to produce the response.

Layer-7 load balancers, API gateways and CDN edge servers are all reverse proxies with a particular focus. (A pure layer-4 load balancer that only forwards TCP packets, without terminating the connection, is not really a proxy.) A load balancer focuses on spreading traffic, an API gateway on API concerns such as authentication and per-key limits, and a CDN on caching content close to users.


Forward vs Reverse Proxy

Feature

Forward Proxy

Reverse Proxy

Acts on behalf of

Clients

Servers

Placed near

The client network (office, college, data-centre egress)

The server side (in front of the application)

Who knows about it

The client is configured to use it

The client usually has no idea it exists

Hides

The client's identity from servers

The backend servers from clients

Typical tasks

Filtering, auditing, caching for many users, privacy

Load balancing, TLS termination, caching, security, routing

Examples

Squid, corporate web gateways

Nginx, HAProxy, Envoy, cloud load balancers, CDNs

A simple memory trick: a forward proxy protects the people who browse; a reverse proxy protects the servers that answer.


Keeping Track of the Real Client

A reverse proxy creates a problem: the backend sees every request coming from the proxy's IP address. But the application often needs the real client IP (for rate limiting, fraud checks, logs) and whether the original request was HTTPS. Proxies solve this with extra headers:

Header

Purpose

X-Forwarded-For

List of client IP addresses, one appended by each proxy. A de facto standard, very widely used

X-Forwarded-Proto

Original protocol (http or https) used by the client

X-Forwarded-Host

Original Host header requested by the client

Forwarded

The official standard version of all of the above, defined in RFC 7239, e.g. Forwarded: for=203.0.113.7;proto=https

Via

Standard HTTP header (RFC 9110) recording the proxies a message passed through

Client 203.0.113.7 → CDN 198.51.100.2 → Nginx 10.0.0.5 → App

At the app:   X-Forwarded-For: 203.0.113.7, 198.51.100.2
              (connection itself comes from 10.0.0.5)

Security warning: a client can send a fake X-Forwarded-For header of its own. The application must trust only the entries added by its own known proxies, reading the list from the right and skipping trusted proxy addresses. Blindly taking the leftmost IP lets attackers bypass IP-based rate limits.


A Typical Nginx Reverse Proxy Configuration

This configuration terminates HTTPS, serves static files directly, and spreads the rest of the traffic across two application servers:

upstream app_servers {
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
}

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate     /etc/ssl/example.com.crt;
    ssl_certificate_key /etc/ssl/example.com.key;

    # Static files served by Nginx itself, no app server involved
    location /static/ {
        root /var/www;
        expires 7d;
    }

    # Everything else is forwarded to the app servers
    location / {
        proxy_pass http://app_servers;
        proxy_set_header Host              $host;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Notice how much responsibility moved out of the application: certificates, static files, load spreading and client IP forwarding are all handled here. $proxy_add_x_forwarded_for appends the client's address to any existing X-Forwarded-For value.


Building a Tiny Reverse Proxy in Python

To see that a reverse proxy is "just a server that makes requests to other servers", here is a small working one using only Python's standard library. It starts two backend servers and a proxy, round-robins requests between the backends, adds X-Forwarded-For and Via, and blocks the /admin path at the edge.

import itertools
import threading
import urllib.error
import urllib.request
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer


def make_backend(name):
    class Backend(BaseHTTPRequestHandler):
        def do_GET(self):
            body = (f"{name} served {self.path} | "
                    f"X-Forwarded-For={self.headers.get('X-Forwarded-For')} | "
                    f"Via={self.headers.get('Via')}\n").encode()
            self.send_response(200)
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        def log_message(self, *args):     # keep the console quiet
            pass
    return Backend


def make_reverse_proxy(backend_urls):
    rr = itertools.cycle(backend_urls)    # simple round robin

    class ReverseProxy(BaseHTTPRequestHandler):
        def do_GET(self):
            if self.path.startswith("/admin"):        # policy at the edge
                self.send_error(403, "Blocked by proxy")
                return
            upstream = next(rr)
            req = urllib.request.Request(upstream + self.path)
            client_ip = self.client_address[0]
            prior = self.headers.get("X-Forwarded-For")
            req.add_header("X-Forwarded-For", f"{prior}, {client_ip}" if prior else client_ip)
            req.add_header("Via", "1.1 demo-proxy")
            with urllib.request.urlopen(req) as resp:
                body = resp.read()
            self.send_response(200)
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        def log_message(self, *args):
            pass
    return ReverseProxy


def start(handler):
    server = ThreadingHTTPServer(("127.0.0.1", 0), handler)   # port 0 = any free port
    threading.Thread(target=server.serve_forever, daemon=True).start()
    return f"http://127.0.0.1:{server.server_address[1]}"


b1 = start(make_backend("backend-1"))
b2 = start(make_backend("backend-2"))
proxy = start(make_reverse_proxy([b1, b2]))

# The client only knows the proxy's address, never the backends'.
for path in ["/products", "/products", "/cart"]:
    with urllib.request.urlopen(proxy + path) as r:
        print(r.read().decode().strip())

try:
    urllib.request.urlopen(proxy + "/admin")
except urllib.error.HTTPError as e:
    print("/admin ->", e.code, e.reason)

Output:

backend-1 served /products | X-Forwarded-For=127.0.0.1 | Via=1.1 demo-proxy
backend-2 served /products | X-Forwarded-For=127.0.0.1 | Via=1.1 demo-proxy
backend-1 served /cart | X-Forwarded-For=127.0.0.1 | Via=1.1 demo-proxy
/admin -> 403 Blocked by proxy

The client called only the proxy's URL. The proxy alternated between the two backends, told each backend who the real client was, and refused /admin before any backend saw it. A production proxy adds connection pooling, timeouts, health checks, streaming and much more, but the core idea is exactly this.


Sidecar Proxies and Service Meshes

In a microservices system (see Monolith vs Microservices), services call each other thousands of times per second. Instead of putting retry, timeout, encryption and metrics code inside every service, many teams run a small proxy (often Envoy) next to each service instance, called a sidecar. All traffic in and out of the service goes through its sidecar.

┌───────────── Pod / host ─────────────┐     ┌───────────── Pod / host ─────────────┐
│  Order service ⇄ sidecar proxy       │ ⇄⇄⇄ │  sidecar proxy ⇄ Payment service     │
└──────────────────────────────────────┘ mTLS└──────────────────────────────────────┘

A set of sidecars managed by a central control plane is called a service mesh (Istio and Linkerd are well-known examples). The sidecar acts as a forward proxy for outgoing calls and a reverse proxy for incoming calls at the same time.


Drawbacks of Proxies

  • Extra hop: every proxy adds a little latency (usually small inside a data centre).

  • Single point of failure: if your only reverse proxy dies, the whole site is down. Run at least two, with a floating IP or DNS-level failover in front.

  • Bottleneck: all traffic passes through it, so it must be sized and monitored carefully.

  • Hidden complexity: timeouts must be set consistently. If the proxy times out after 30 s but the app takes 60 s, users get 504 Gateway Timeout even though the work finishes. If the proxy cannot get a valid response from the backend at all, it returns 502 Bad Gateway.


Common Mistakes

  • Mixing up forward and reverse. Ask "whose side is the proxy on?" Client side = forward, server side = reverse.

  • Saying a reverse proxy and a load balancer are completely different things. A load balancer is one job a reverse proxy can do; most reverse proxies can load balance, and most L7 load balancers are reverse proxies.

  • Trusting the client-supplied X-Forwarded-For value without checking which proxy added it.

  • Forgetting X-Forwarded-Proto after TLS termination, so the app thinks every request is plain HTTP and builds wrong redirect URLs (or loops redirecting to HTTPS forever).

  • Thinking a forward proxy can read HTTPS content. With a CONNECT tunnel it only sees the host name, unless TLS inspection with an installed company certificate is used.


Interview Questions

Q1. What is the difference between a forward proxy and a reverse proxy?

A forward proxy works for clients: it sits in front of them and sends their requests to the internet, hiding the clients from servers. A reverse proxy works for servers: it sits in front of them, receives client requests, and forwards them to backends, hiding the servers from clients.

Q2. What is TLS termination and why do it at the reverse proxy?

TLS termination means decrypting HTTPS at the proxy and forwarding the request to backends over the internal network. It centralises certificate management, removes the CPU cost of handshakes from application servers, and lets the proxy inspect and route requests by path or header.

Q3. Behind a reverse proxy, how does the application learn the client's real IP?

The proxy adds it in a header such as X-Forwarded-For or the standard Forwarded header (RFC 7239). The application should trust only values added by its own known proxies.

Q4. Is a CDN a forward or reverse proxy?

A reverse proxy. It acts on behalf of the website (the origin), and users do not configure anything to use it; DNS simply sends them to the nearest CDN edge.

Q5. A user sees "502 Bad Gateway" and another sees "504 Gateway Timeout". What do they mean?

Both come from a proxy or gateway. 502 means the proxy got an invalid response (or no valid connection) from the upstream server. 504 means the upstream did not respond within the proxy's timeout.

Q6. How does a forward proxy handle HTTPS without breaking encryption?

The client sends a CONNECT request for host:port; the proxy opens a TCP connection and then just relays the encrypted bytes in both directions. The TLS session is between the client and the real server.


Summary

  • A proxy is a middleman that forwards requests, often adding filtering, caching or security.

  • Forward proxy: on the client side; used for filtering, auditing, shared caching, privacy and controlled egress. HTTPS passes through via CONNECT tunnels.

  • Reverse proxy: on the server side; used for load balancing, TLS termination, caching, compression, routing, rate limiting and hiding backends.

  • Layer-7 load balancers, API gateways and CDNs are specialised reverse proxies; a sidecar acts as a forward proxy for outgoing calls and a reverse proxy for incoming ones.

  • Use X-Forwarded-For / Forwarded to pass on the real client IP, and trust only your own proxies' entries.

  • Run proxies redundantly and set timeouts consistently to avoid them becoming a single point of failure or a source of 502/504 errors.

Forward proxy: speaks for the clients. Reverse proxy: speaks for the servers.