Sub topic 1
What a proxy is and why systems use one
Prerequisites
This chapter assumes you already know the following:
- How a client and a server exchange requests and responses.
- What HTTP methods, status codes and headers are.
- What an IP address, a port and a DNS name are.
- What TLS provides in HTTPS: encryption, server identity and integrity.
Later chapters cover forward proxies, reverse proxies and the HTTP details. This chapter gives the shared model that the rest builds on.
The working definition
A proxy is a program that sits between a client and a server. It receives a request from one side, makes a decision, and sends a request (or a response) on to the other side. Each side of the conversation talks to the proxy. The proxy talks to the other side.
This means a proxied exchange is two connections, not one:
- Client to proxy.
- Proxy to server.
The proxy joins them. Because it ends one connection and starts another, it can read, change, reject, reorder, cache or log what passes through. A plain router forwards packets and does none of this.
The word "proxy" means acting on someone's behalf. The question that separates the proxy types is whose behalf. A forward proxy acts for clients. A reverse proxy acts for servers. The next chapters treat each in detail. Here, remember only that the same mechanism serves both.
Why systems add a proxy
A proxy is worth its cost when you need a control point. Without one, every client talks directly to every server, and each server must handle security, routing and failure on its own. A proxy gives you one place to apply a rule.
| Need | What the proxy does | Typical side |
|---|---|---|
| Control | Allows or denies requests by user, address or destination | Forward |
| Privacy | Hides the real client or server address from the other party | Both |
| Distribution | Spreads requests across several backends | Reverse |
| Availability | Stops sending traffic to a failed backend | Reverse |
| Performance | Answers repeated requests from a cache | Both |
| Security offload | Terminates TLS so backends do not each manage certificates | Reverse |
| Observability | Logs every request in one place | Both |
| Decoupling | Lets you move or replace backends without changing client URLs | Reverse |
Each row becomes its own chapter later. For now, notice the pattern. The proxy turns a many-to-many tangle into many-to-one-to-many, and the "one" is where policy lives.
A concrete example
Suppose a shop runs two application servers at 10.0.0.11:8080 and 10.0.0.12:8080. Without a proxy, the shop has to tell clients about both addresses. It also has to deal with one server going down, and it has to put a TLS certificate on each. With a proxy in front, clients see one name, example.com. This is the smallest useful reverse proxy configuration:
upstream app_servers {
server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;
server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}This does four things at once. It accepts client connections on port 80. It picks a backend, round-robin by default. It stops using a backend for 30 seconds after three failed attempts. It tells the backend who the original client was. Later chapters explain each directive. The point here is that one small file moves several concerns out of the application.
What a proxy can see depends on its layer
Proxies work at different layers, and the layer decides what the proxy can do.
- A layer 4 proxy relays TCP (or UDP) bytes. It knows addresses and ports. It cannot see URLs or headers, so it cannot route by path.
- A layer 7 proxy parses the application protocol, usually HTTP. It can route by
Hostor path, rewrite headers, cache responses and enforce rules per request.
Layer 7 proxies do more work per request and need to understand the protocol. Layer 4 proxies are simpler and faster, but they are blind to content. HTTPS adds a wrinkle. If the traffic is encrypted end to end between client and server, an HTTP proxy cannot read it. It can only relay it, or it must terminate the TLS itself. The mechanics are covered in the chapter on how a proxy relays HTTP and HTTPS.
What the proxy is not
Several neighboring technologies get confused with proxies.
- NAT rewrites IP addresses in packets as they cross a router. It does not end the connection or understand HTTP. A proxy opens its own connection.
- A VPN creates an encrypted network path for all of a device's traffic. A proxy usually handles one protocol or one application's traffic.
- A load balancer is a role, not a different kind of thing. Many load balancers are reverse proxies with a distribution policy.
- A firewall allows or drops traffic by rule. A proxy can also act on content and answer on behalf of another party.
The boundaries blur in products. nginx, HAProxy, Envoy and Squid can each cover several of these roles. When you read a design, ask what the component does with each connection, not what it is called.
How a client knows about a proxy
There are two broad arrangements, and the difference affects how you debug.
- Explicit. The client is configured to use the proxy, through application settings, environment variables or a browser setting. The client sends the proxy a request that names the real destination.
- Transparent or implicit. The client does not know. The network sends its traffic to the proxy, or DNS resolves the public name to the proxy's address.
Reverse proxies are almost always implicit to the client. The client types example.com and reaches the proxy without knowing. Forward proxies are commonly explicit. An explicit request from the command line looks like this:
curl -x http://proxy.internal:3128 https://example.org/The -x flag tells curl to connect to proxy.internal:3128 instead of example.org. Because the URL is HTTPS, curl first asks the proxy to open a tunnel to the destination. The proxy cannot read the encrypted traffic inside that tunnel.
What the backend loses, and how headers restore it
Because the proxy opens its own connection, the backend sees the proxy's IP address as the peer, not the client's. The Host header may also differ from what the client sent, and the backend may think the connection was plain HTTP even if the client used HTTPS. The proxy fixes this by passing the original information in headers:
X-Forwarded-For: 203.0.113.7, 10.0.0.5
X-Forwarded-Proto: https
Forwarded: for=203.0.113.7;proto=https;host=example.comX-Forwarded-For lists addresses from the original client onward, with each proxy appending the address it saw. Forwarded is the standardized form of the same information. The backend must be configured to read them. It also must trust them only when they come from a proxy it controls, because any client can send a fake X-Forwarded-For header.
Common errors when first adding a proxy
- Logging the proxy's address as the client. Rate limits, audit logs and geo rules then treat all users as one address. Read the forwarded headers, or use a mechanism such as the PROXY protocol, and configure your logs to match.
- Wrong scheme or host in generated links. The backend sees HTTP on an internal port, so it builds
http://redirects or the wrong hostname. PassHostandX-Forwarded-Proto, and make the application honor them. - Treating the proxy as free. It adds a network hop, and it adds latency. The extra connection, parsing and buffering are small per request but not zero.
- Creating a single point of failure. One proxy in front of many healthy backends is still one thing that can fail. Plan more than one proxy instance.
- Caching what must not be cached. A shared cache that stores personalized responses can serve one user's page to another. Cache only content that is the same for everyone, or vary the cache key correctly.
- Forgetting timeouts. The proxy has its own timeouts for connecting, sending and reading. If they are shorter than a slow backend response, clients get gateway errors (502 or 504) that the application never logged.
When not to add a proxy
A proxy is not automatically a good idea. A single small service with one server, no TLS needs beyond what the platform provides, and no caching needs gains little from one. Each proxy is another component to deploy, monitor, patch and secure. A good rule is to add a proxy when you can name the specific control point you need from the table above. "Best practice" alone is not a reason.
Where this goes next
The rest of the tutorial builds on this model. The next two chapters separate forward proxies, which act for clients, from reverse proxies, which act for servers. After that come the HTTP mechanics, then the specific jobs: access control, load balancing, health checks, TLS termination, caching and gateway routing. The final chapters cover availability and the trade-offs of the whole design.
Exercise
A client opens a connection to a proxy, and the proxy opens a separate connection to the server. Give two things this allows that a router forwarding packets cannot do, and name one cost it adds.
Show answer
Because the proxy ends the client's connection and starts its own, it can inspect or change the request (for example route by URL path, add headers, or reject a request) and it can answer from a cache without contacting the server. It can also hide the real address of one side from the other. The costs include an extra network hop and added latency, one more component to run and secure, and the backend seeing the proxy's address instead of the client's unless forwarding headers are used. Any one of these costs is a correct answer.
Exercise
Your backend logs show every request coming from 10.0.0.5, the proxy's address. Which header should you read to find the original client, and what must you check before trusting it?
Show answer
Read X-Forwarded-For (or the standard Forwarded header). Before trusting it, make sure the value was added by a proxy you control, because a client can send a forged header. The edge proxy should overwrite or sanitize incoming values, and the backend should accept the header only from known proxy addresses.