Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 65 ยท Building Blocks of the Web Stack

Proxies - Forward and Reverse

Understand forward and reverse proxies, what each hides, TLS termination, caching and routing, and build a small routing reverse proxy in Python.

A proxy is a server that sits between two parties and passes messages on behalf of one of them. Instead of talking directly, the client talks to the proxy, and the proxy talks to the destination. That small indirection is surprisingly powerful: the proxy can hide identities, filter traffic, store copies, encrypt, compress, route and measure.

There are two kinds, and the difference is who the proxy works for. A forward proxy works for the client. A reverse proxy works for the server. Mixing them up is one of the most common confusions in system design interviews, so it is worth getting precise.

Forward proxy: acting for clients

A forward proxy sits in front of clients. The clients are configured to send their outbound requests through it. The destination server sees the proxy's address, not the client's.

flowchart LR
  C1["Client A"] --> FP["Forward proxy"]
  C2["Client B"] --> FP
  FP --> I(("Internet"))
  I --> S["Website"]

Figure: clients are behind the forward proxy; the website sees only the proxy.

Typical uses:

  • Anonymity and privacy: hide the client's IP address from the destination.
  • Access control and filtering: a company blocks sites, scans downloads, or requires login before internet access.
  • Caching: many employees download the same update; the proxy fetches it once.
  • Bypassing geographic limits (and the reverse: an organization forcing all traffic through one exit).
  • Logging and auditing of outbound traffic.

Reverse proxy: acting for servers

A reverse proxy sits in front of servers. Clients think they are talking to the real website, but they are talking to the proxy, which forwards each request to one of several origin servers behind it. The client does not know the origin servers exist.

flowchart LR
  U1["User 1"] --> RP["Reverse proxy"]
  U2["User 2"] --> RP
  RP --> A["App server A"]
  RP --> B["App server B"]
  RP --> C["Static files"]

Figure: users see one address; the proxy decides which origin handles each request.

This is the proxy you meet in nearly every production architecture. Common jobs:

  • Load balancing: spread requests across servers (load balancers are specialized reverse proxies).
  • TLS termination: the proxy handles HTTPS encryption so application servers can speak plain HTTP on the private network, and certificates live in one place.
  • Caching: serve repeated responses (images, pages) without bothering the origin.
  • Compression of responses (gzip, Brotli).
  • Routing by path or host: /api goes to the API servers, /static to file servers, shop.example.com to a different service. This is the core idea behind an API gateway.
  • Security: hide internal topology, filter malicious requests, rate-limit (rate limiting), block by IP, act as a web application firewall.
  • Request buffering: absorb slow clients so app servers are not tied up waiting on them.

Well-known reverse proxies include Nginx, HAProxy, Envoy and Traefik; cloud services such as CDNs also act as global reverse proxies.

Forward or reverse?

Forward proxy Stands in for the clients. Server sees the proxy.
Reverse proxy Stands in for the servers. Client sees the proxy.
Quick test Who is hidden: the client or the server?

Same technology, opposite point of view.

1 / 2

Side by side

Forward proxy Reverse proxy
Sits in front of Clients Servers
Configured by Client or network admin Service owner
Hides Client identity Server identity and layout
Clients know about it? Yes (usually configured) No (transparent)
Typical goals Privacy, filtering, outbound control Load balancing, TLS, caching, routing, protection

Seeing the client's real address

A proxy replaces the connection's source address with its own, so the application behind it sees only the proxy's IP. Reverse proxies add headers such as X-Forwarded-For (the original client IP), X-Forwarded-Proto (http or https) and Host so the app can still log the real client, build correct links and apply per-user limits. Trust those headers only when they come from your own proxy; a client can send fake ones.

Building a reverse proxy in miniature

The simulation below implements the heart of a reverse proxy: route by path prefix, rotate among origin servers, serve cacheable responses from a small cache, and add forwarding headers. It runs entirely in memory so you can see each decision.

Python A routing, caching reverse proxy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second request to /static/logo.png never reaches an origin; the proxy answers it from its own cache. The two /api requests alternate between servers, and the origins see the real client addresses only because the proxy forwarded them.

A simple forward proxy filter

A forward proxy usually applies policy before letting a request out. This sketch shows an allow list and a log, the basis of corporate web filtering.

Python A forward proxy with an allow list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Costs and risks

A proxy is also a new component, so it brings its own concerns:

  • Single point of failure: run at least two and fail over (failover).
  • Added latency: one more hop, usually under a millisecond inside a data center.
  • Bottleneck: it must handle all traffic; scale it out like anything else.
  • Complexity and security: a misconfigured proxy can expose internal services or cache private pages for the wrong user. Never cache responses that vary by user without a correct cache key.

A related term: a gateway proxy or tunnel carries traffic for protocols other than HTTP, and a transparent proxy intercepts traffic without the client being configured. Keep the forward/reverse distinction as your starting point.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A company routes all employee web traffic through one server that blocks certain sites. This is:

    It stands in for the clients and applies policy to their outbound requests.
  2. 2. Which is a typical reverse proxy job?

    A reverse proxy fronts the servers: TLS, routing, caching and load balancing.
  3. 3. Why do reverse proxies add headers such as X-Forwarded-For?

    The connection comes from the proxy, so the real client address must be passed along in a header.
  4. 4. What is a risk when a proxy caches responses?

    Responses that vary by user must not share a cache entry; use correct cache keys or do not cache them.

Summary

  • A proxy is an intermediary; forward proxies act for clients, reverse proxies act for servers.
  • Forward proxies provide privacy, filtering, caching and audit of outbound traffic.
  • Reverse proxies provide load balancing, TLS termination, caching, compression, routing and protection.
  • Pass the real client address in forwarded headers and trust them only from your own proxy.
  • A proxy must itself be redundant and scalable, and its cache keys must be correct.

Next: API Gateway