Notes
What is a proxy?
A proxy is a middleman. Instead of talking to the destination directly, you talk to the proxy, and the proxy talks to the destination for you. The question is: whose side is the middleman on?
Forward proxy
A forward proxy works for the clients. Picture an office network. Every laptop sends its web traffic through the company's proxy, and the proxy goes out to the internet for them.
The website sees the proxy's address, not yours. Companies and schools use forward proxies to filter and log browsing, and to cache popular downloads. Privacy tools use the same idea to hide who you are.
- hide the clients’ IP addresses
- block or log sites (company, school)
- cache popular downloads
- reach blocked content (VPN-like)
Reverse proxy
A reverse proxy is the mirror image. It works for the servers. Clients think they're talking to the website, but they're talking to the proxy, which forwards each request to the right server behind it.
Here's the one-line difference. A forward proxy hides the clients from the servers. A reverse proxy hides the servers from the clients. You set up a forward proxy on your side. The website owner runs the reverse proxy on theirs.
- forward proxy: hides the clients
- reverse proxy: hides the servers
- clients choose a forward proxy;
- the site owner runs the reverse proxy
Job 1 · TLS
So what does a reverse proxy actually do? Job one: TLS termination. It holds the certificate and does the encryption work, so the application servers don't have to.
Job 2 · Routing
Job two: routing. It reads each request and decides where it goes. Slash api goes to the application servers. Slash images goes to a static file server. You can even send a small share of users to a new version, to test it safely.
Job 3 · Caching
Job three: caching. If many users ask for the same poster, the proxy keeps a copy and answers straight from memory. The application servers never even see those requests.
Job 4 · Compression
Job four: compression. It shrinks responses with gzip or brotli, so a hundred and twenty kilobytes of JSON travels as maybe twenty. That's a big win on slow mobile networks.
- gzip / brotli
- 120 KB of JSON → ~20 KB
- faster on slow phone networks
Job 5 · Security
Job five: security. The servers' real addresses stay hidden. The proxy can rate limit, so one client can't flood the system, block bad traffic with a web application firewall, and absorb attacks before they reach your servers.
Job 6 · Load balancing
Job six: load balancing. A reverse proxy that spreads requests across several servers is acting as a load balancer. That's why the two terms get mixed up.
Related terms
So how do they relate? A reverse proxy is the general idea. A load balancer is a proxy focused on spreading load. An API gateway is a proxy for APIs that adds authentication, rate limits, keys, and versioning. Many tools do all three.
And a CDN, like Cloudflare or CloudFront, is really a huge network of reverse proxies in hundreds of cities, caching your content close to your users.
- reverse proxy: the general middleman
- load balancer: a proxy focused on
- spreading load across servers
- API gateway: a proxy for APIs, adding
- auth, rate limits, API keys, versioning
- a CDN = reverse proxies in
- hundreds of cities, caching
- your content near users
- Cloudflare, Akamai, CloudFront
Trade-offs
The trade-offs: one more hop adds a little latency. It must be redundant, or it becomes a single point of failure. And because servers now see the proxy's address, the proxy passes on the real client address in a header called X-Forwarded-For.
- one more hop: a little latency
- must be redundant (or it’s a SPOF)
- pass the client’s real IP on
- X-Forwarded-For header