Sub topic 1
What a proxy is and why systems use one
Prerequisites
This chapter assumes you already know four things:
- 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.
- That HTTPS uses TLS to encrypt traffic and to prove the server's identity.
The working model
A proxy is a server that sits between a client and another server and handles requests on someone's behalf. The client sends its request to the proxy. The proxy then opens its own connection to the destination, sends a request there, and returns the response to the client.
Two separate connections exist. One runs from the client to the proxy, and the other from the proxy to the destination. The proxy is a full participant in each connection, not a wire that passes bytes along. Because of this, it can read, change, block, store or redirect traffic.
Why systems use a proxy
Each reason below comes from the same fact. The proxy is one point that sees every request, so you can put a shared rule or a shared service there.
| Goal | What the proxy does |
|---|---|
| Control | Allows or denies requests by user, address or destination. |
| Privacy and hiding | Hides one side's address from the other. |
| Performance | Stores responses and reuses them. |
| Scale | Spreads requests across several servers. |
| Security | Ends TLS in one place and keeps backends off the public network. |
| Simplicity | Gives clients one address for many internal services. |
Two kinds, defined by who the proxy serves
The two main kinds differ in whose interests the proxy represents.
- A forward proxy acts for clients. Clients are configured to use it, and the destination servers see the proxy's address, not the client's. Companies use it to filter and log outbound traffic.
- A reverse proxy acts for servers. Clients usually do not know it exists. They think they are talking to the real site. Behind it, one or many backend servers do the work.
Later chapters cover each kind in detail. For now, remember the test: ask whose address is hidden and whose side the proxy is on.
A minimal example
The following nginx configuration is a small reverse proxy. It receives requests on port 80 and passes them to two backends in turn.
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;
}
}The upstream block names the backends. Requests rotate between them, which is round-robin. If a backend fails three times, nginx stops sending it traffic for 30 seconds.
The proxy_set_header lines matter because of the two-connection model. The backend sees the proxy as its client. Without these headers, it would not learn the original Host, the real client address, or whether the client used HTTPS.
The cost of the middle
A proxy is not free. Keep these costs in mind as you read the rest of the tutorial.
- Latency. Each request passes through an extra hop and an extra connection.
- Single point of failure. If all traffic goes through one proxy and it fails, everything behind it is unreachable.
- Lost client identity. The backend sees the proxy's address unless you pass the original along.
- More to operate. The proxy needs configuration, monitoring and updates.
Exercise
A team puts a proxy in front of their web servers. The application logs now show the same IP address for every visitor. What causes this, and how do you fix it?
Show answer
The backend opens connections with the proxy, not with the visitors, so it sees only the proxy's address. Configure the proxy to add the client address in a header such as X-Forwarded-For (or Forwarded), and make the application read that header, trusting it only when it comes from the proxy.
Exercise
Your goal is to stop employees' laptops from reaching unapproved websites. Do you need a forward proxy or a reverse proxy, and why?
Show answer
A forward proxy. It acts for the clients, and an access rule on it can allow or deny each outbound destination. A reverse proxy acts for servers and would not control where clients go.