Forward Proxy vs Reverse Proxy Explained
At work, a site you open freely at home shows a company block page. Later, your team's shop runs behind nginx, which "proxies" requests to the app. Both are proxies, doing opposite jobs. This lesson explains forward proxy vs reverse proxy: where each sits, what it does, who sees it, and how the client's address survives the trip.
Two go-betweens, facing opposite ways
In lesson 15 you met the proxy: a go-between that ends the client's connection and opens its own to the next server. A forward proxy sits on the clients' side and sends their requests out. A reverse proxy sits on the servers' side and takes requests in for them.
In the flowchart, notice the dashed boxes: each proxy sits inside the side it works for, and the far end of each lane sees only the proxy.
Forward proxy: going out for the clients
A client uses a forward proxy because it is told to: an app or the operating system has a proxy setting, such as proxy.example.com:3128, and sends each request there. A transparent proxy needs no setting: the network diverts traffic to it, unseen by the client.
For plain HTTP, the proxy fetches the page itself. For HTTPS, the client sends CONNECT www.example.com:443, and the proxy opens a TCP connection to that site and relays the encrypted bytes, a tunnel. It sees the site name and how much data passes, not the pages.
That checkpoint on the way out gives an organisation:
- Control: an office web filter blocks sites by name or category and logs who went where.
- Caching: it serves repeat plain-HTTP requests from stored copies, saving bandwidth; tunnelled HTTPS cannot be cached.
- Anonymity: sites see the proxy's address, not yours, unless it passes yours on in a header (see the last section). The proxy sees everything, so you trust whoever runs it.
Some companies also run TLS inspection, decrypting HTTPS with certificates their laptops are set to trust. Squid is a widely used open-source forward proxy, and 3128 is its default port.
Reverse proxy: taking requests in for the servers
A reverse proxy receives requests on behalf of the servers behind it, its backends. The site's DNS name points at the proxy (lesson 3), so the client needs no setting and believes it is talking to the site.
It handles jobs every backend would otherwise repeat:
- TLS termination: it holds the certificate and decrypts HTTPS (lesson 14).
- Caching: it answers repeat requests from a stored copy (What Is Caching?).
- Compression: it shrinks text such as HTML with gzip or Brotli.
- Routing: it picks a pool by path or host name, like lesson 15's L7 rules.
- Hiding: backends keep private addresses, and their firewalls can admit only the proxy (lesson 13).
nginx and HAProxy are widely used open-source reverse proxies. In this nginx sketch, upstream blocks name two pools and location blocks route by path. It goes in nginx's http section, for example in a file under /etc/nginx/conf.d/; addresses are illustrative.
upstream api_pool {
server 10.0.1.21:8080;
server 10.0.1.22:8080;
}
upstream web_pool {
server 10.0.1.31:3000;
server 10.0.1.32:3000;
}
server {
listen 443 ssl;
server_name shop.example.com;
ssl_certificate /etc/nginx/certs/shop.crt;
ssl_certificate_key /etc/nginx/certs/shop.key;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
location /api/ {
proxy_pass http://api_pool;
}
location / {
proxy_pass http://web_pool;
}
}
nginx picks the location with the longest matching prefix, like the most specific route in lesson 12: /api/orders goes to api_pool, and /web/cart and every other path to web_pool. Within each pool, servers take turns: round robin, nginx's default.
Both locations inherit the proxy_set_header lines, which pass on the site name, the client's address and the scheme, http or https. In the diagram, notice that both requests reach one address; only the path picks the pool.
Who knows what, and where load balancers and CDNs fit
The two hide opposite ends. With a forward proxy, the client usually knows the proxy is there, but the server sees only the proxy. With a reverse proxy, the client sees only the proxy and never learns which backend answered.
In the side-by-side view, notice that the test is whose side the proxy works for, not its software: Squid can also run as a reverse proxy.
Load balancers and CDNs are reverse proxies with extra jobs. An L7 load balancer, such as an AWS Application Load Balancer, is a reverse proxy whose main job is spreading requests across healthy servers; nginx with several servers in an upstream is both.
A CDN, or content delivery network, is many caching reverse proxies around the world, close to users (lesson 17). The exception is an L4 balancer that only rewrites packet addresses (lesson 15): it never ends the client's connection, so it is not a proxy.
X-Forwarded-For: passing on the client's address
Because a reverse proxy opens its own connection, the backend sees the proxy's address as the source (lesson 14). The app's logs would show one address for every visitor, and per-address limits would lump everyone together.
The fix is the X-Forwarded-For request header: each proxy appends the address it received the connection from. Follow one request, with illustrative addresses:
- The client,
198.51.100.23, sends no such header. - A load balancer at
10.0.0.12addsX-Forwarded-For: 198.51.100.23. - nginx at
10.0.1.10extends it toX-Forwarded-For: 198.51.100.23, 10.0.0.12. - The app sees a connection from 10.0.1.10, and the header lists who came before.
In the nginx sketch, $proxy_add_x_forwarded_for is step 3: the incoming header plus the connecting address. But a client can send its own header, such as X-Forwarded-For: 192.0.2.99, and proxies keep it at the left.
So trust only entries your own proxies added: read from the right, skip your proxies' addresses, and the first one left is the client; nginx's realip module can do this. The standard Forwarded header (RFC 7239) carries the same facts plus the scheme, as in Forwarded: for=198.51.100.23;proto=https, though fewer tools read it.
Check yourself
Company laptops reach the web through a gateway that blocks gambling sites, and sites see its address. Forward or reverse proxy?
Forward: it works for the clients, sits on their side and hides them from servers. The side it serves decides, not the software.
Behind nginx, your app logs 10.0.1.10 for every visitor. Why, and what should it read instead?
nginx opens its own connection, so every request arrives from nginx's address. Read X-Forwarded-For, where nginx appends the address it received each request from.
Your only proxy forwards a request with X-Forwarded-For: 192.0.2.99, 198.51.100.23. Which is the real client, and why not the first?
198.51.100.23: your proxy appended the address it actually saw. The first entry came from the client itself, so it could be anything.
Go deeper
- NGINX ingress controller – Deep dive with AWS EKS: sets up NGINX on Kubernetes as a reverse proxy that routes by host and path, behind an AWS Network Load Balancer.