Sub topic 3
Reverse proxies: acting on behalf of servers
Prerequisites
This chapter assumes you know HTTP requests, methods and headers, and what IP addresses, ports and DNS do. It also assumes you know that TLS provides encryption and server identity.
What a reverse proxy does
A reverse proxy is a server that receives requests from clients and forwards them to one or more backend servers. It then returns the backend's response to the client. The client believes it is talking to the site itself. It never needs to know which backend did the work.
This is the opposite of a forward proxy, which acts for clients. A reverse proxy acts for servers. The operator of the backends deploys it, and the clients usually don't know it exists.
Why systems use one
A reverse proxy gives you one public entry point in front of many servers. That single position lets it do several jobs:
- Load balancing: spread requests across backends.
- TLS termination: handle encryption so backends don't have to.
- Caching: answer repeated requests without contacting a backend.
- Routing: send different URL paths to different services, which is the basis of an API gateway.
- Protection: hide backend addresses and reject bad traffic early.
Later chapters cover each job in depth. This chapter focuses on the basic mechanism.
A minimal configuration
This nginx configuration defines two backends and forwards all requests to them.
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 pool of backends. By default nginx sends requests to them in turn (round-robin). After three failed attempts (max_fails=3), a backend is skipped for 30 seconds (fail_timeout=30s).
The server block accepts requests for example.com on port 80. The proxy_pass line sends them to the pool.
Why the forwarding headers matter
The backend sees a connection from the proxy, not from the client. Its view of the request would be wrong without help. The proxy_set_header lines pass the original details along:
| Header | What it tells the backend |
|---|---|
| `Host` | The hostname the client asked for |
| `X-Real-IP` | The client's IP address |
| `X-Forwarded-For` | The chain of addresses the request passed through |
| `X-Forwarded-Proto` | Whether the client used `http` or `https` |
A backend that ignores these headers will log the proxy's address for every request. It may also build redirect links with the wrong scheme or hostname.
Common errors
- Backends log the proxy's IP. The application reads the connection address instead of the forwarded header. Configure it to trust
X-Forwarded-Forfrom the proxy only. - Redirects point to
http://. The backend does not know the client used HTTPS. PassX-Forwarded-Protoand make the application honor it. - Wrong site is served. Without
Hostset to$host, nginx sends the upstream name, such asapp_servers, as the host. Virtual-host routing on the backend then fails. - The proxy becomes a single point of failure. One proxy in front of many backends has no redundancy of its own. A later chapter covers running several.
Exercise
A backend application logs 10.0.0.5 as the client address for every request, and 10.0.0.5 is the reverse proxy. What change fixes this, and what must you be careful about?
Show answer
Have the proxy set X-Forwarded-For (or X-Real-IP) with the real client address, and configure the application to read that header. Trust the header only when the request comes from your own proxy, because clients can send forged values.