Proxy, reverse proxy and load balancer
A proxy is a middleman that makes connections on someone else's behalf. A forward proxy works for clients (office exit point), a reverse proxy works for servers (one address in front of many servers), and a load balancer is a reverse proxy that spreads requests across backends at L4 or L7. SOCKS and transparent proxies round out the family.
138.1 Why is it used?
- Forward proxy: 200 office PCs never go to the internet directly; one exit point to filter (ads.lab -> 403) and log.
- Reverse proxy: users know one address (shop.lab); the servers behind it stay hidden; it also handles TLS and caching (Nginx, HAProxy).
- Load balancer: spreads traffic over web1, web2 and stops sending to a server that is down.
- SOCKS: forwards any TCP connection (HTTP, DB, SSH); transparent proxy: filtering without any client setting (schools, hotels).
138.2 Forward vs reverse proxy
- Step 1
1.Office PC 192.168.158.10 has proxy 192.168.158.20:8888 configured
- Step 2
2.PC sends GET http://web1.lab/ to the proxy
- Step 3
3.Proxy forwards it over the internet to web1.lab
- Step 4
4.web1 sees only the proxy's IP
- Step 5
5.GET ads.lab matches the filter: proxy returns 403 right there
- Step 1
1.User opens shop.lab (one IP = the reverse proxy)
- Step 2
2.Request 1 arrives at the reverse proxy -> forwarded to web1 (192.168.158.31)
- Step 3
3.Request 2 -> forwarded to web2 (192.168.158.32)
- Step 4
4.User never learns how many servers are behind it
| Point | Forward proxy | Reverse proxy |
|---|---|---|
| Sits on | Client side (office, school) | Server side |
| Configured on | The client (proxy setting) | The server side; client doesn't know |
| Hides | Clients from servers | Servers from clients |
| Used for | Filtering, logging, caching outbound | TLS termination, load balancing, caching |
| Examples | tinyproxy, Squid | Nginx, HAProxy |
138.3 Lab: forward proxy with tinyproxy
curl -s http://192.168.158.31/curl -sv --proxy http://proxy:8888 \ http://web1.lab/# web1 server logcat /web.logHello from web1 path=/ * Connected to proxy (192.168.158.20) port 8888> GET http://web1.lab/ HTTP/1.1> Host: web1.lab< HTTP/1.0 200 OK< Via: 1.1 proxy (tinyproxy/1.11.2)Hello from web1 path=/ [web1]$ cat /web.logweb1: from 192.168.158.10 xff=- /web1: from 192.168.158.20 xff=- /- Through a proxy the request line has the full URL (GET http://web1.lab/), and the response carries a Via header.
- Direct call: web1 logs the client 192.168.158.10; via proxy: it logs the proxy 192.168.158.20.
# /etc/tinyproxy/tinyproxy.confPort 8888Allow 192.168.158.0/24Filter "/etc/tinyproxy/filter"FilterDefaultDeny No# /etc/tinyproxy/filterads\.lab# clientcurl -si --proxy http://proxy:8888 \ http://ads.lab/ | grep -E 'HTTP/|<title>'HTTP/1.1 403 Filtered<head><title>403 Filtered</title></head>[proxy]$ grep -i filtered /tmp/tiny.logProxying refused on filtered domain "ads.lab"138.4 Load balancer: L4 vs L7
- Step 1
1.Client 192.168.158.10 -> HAProxy on 192.168.158.20
- Step 2
2.L4 (:9000, mode tcp): looks only at IP + port, hands the whole TCP connection to web1 or web2
- Step 3
3.L7 (:80, mode http): reads the HTTP request
- Step 4
4.GET /api -> web1; GET /img or Host img.lab -> web2; anything else -> round robin pool
- Step 5
5.L7 adds X-Forwarded-For with the client's real IP
| Algorithm / feature | What it does |
|---|---|
| Round robin | web1, web2, web1, web2 ... |
| Least connections | Send to the least busy server |
| IP hash (sticky) | Same client always to the same server |
| Health check | Probe backends (every 1s here); no traffic to a DOWN server |
frontend web bind :80 option forwardfor acl is_api path_beg /api acl is_img hdr(host) -i img.lab use_backend api if is_api use_backend img if is_img default_backend poolbackend pool balance roundrobin option httpchk GET /health default-server check inter 1s fall 2 server web1 192.168.158.31:80 server web2 192.168.158.32:80 listen tcp9000 mode tcp bind :9000 balance roundrobin default-server check inter 1s fall 2 server web1 192.168.158.31:80 server web2 192.168.158.32:80for i in 1 2 3 4; do curl -s http://lb/; donecurl -s http://lb/api/userscurl -s http://img.lab/logo.pngfor i in 1 2 3 4; do curl -s http://lb:9000/api/x; doneHello from web1 path=/Hello from web2 path=/Hello from web1 path=/Hello from web2 path=/[web1]$ tail -2 /web.logweb1: from 192.168.158.20 xff=192.168.158.10 / Hello from web1 path=/api/usersHello from web2 path=/logo.png Hello from web1 path=/api/xHello from web2 path=/api/xHello from web1 path=/api/xHello from web2 path=/api/x[web1]$ tail -2 /web.logweb1: from 192.168.158.20 xff=- /api/x- On L7 the backend logs xff=192.168.158.10 (real client); on L4 xff=- because L4 cannot add a header.
- On L4 the path /api/x plays no role: connections simply alternate.
- Simplest L4 relay: socat TCP-LISTEN:7000,fork,reuseaddr TCP:192.168.158.32:80.
138.5 Backend down: health check and 503
[WARNING] (50) : Server pool/web2 is DOWN, reason: Layer4 connection problem[WARNING] (50) : Server tcp9000/web2 is DOWN, reason: Layer4 connection problem $ for i in 1 2 3 4; do curl -s http://lb/; doneHello from web1 path=/Hello from web1 path=/Hello from web1 path=/Hello from web1 path=/ # web1 stopped too:$ curl -si http://lb/HTTP/1.1 503 Service UnavailableNo server is available to handle this request.$ curl -sv http://lb:9000/ 2>&1 | tail -3* Request completely sent off* Empty reply from server* Closing connection- One backend down: users see no error, all traffic goes to web1.
- All down: L7 returns a real 503 page; L4 can only close the connection (Empty reply).
138.6 SOCKS and transparent proxy
- SOCKS forwards any TCP connection: the client says connect to web1.lab:80 and the proxy copies data both ways. SOCKS5 adds UDP and authentication.
- ssh -D 1080 turns an SSH connection into your own SOCKS5 proxy; --socks5-hostname resolves names on the proxy side.
- Transparent proxy: the client has no proxy setting; the router/gateway redirects port 80 to the proxy, which reads the Host header.
- A transparent proxy cannot read HTTPS without a certificate warning.
ssh -o StrictHostKeyChecking=accept-new \ -f -N -D 1080 lab@proxycurl -sv --socks5-hostname 127.0.0.1:1080 \ http://web1.lab/* Connected to 127.0.0.1 (127.0.0.1) port 1080* SOCKS5 connect to web1.lab:80 (remotely resolved)* SOCKS5 request granted.> GET / HTTP/1.1> Host: web1.lab< HTTP/1.0 200 OKHello from web1 path=/[web1]$ tail -1 /web.logweb1: from 192.168.158.20 xff=- /nft add table ip transpnft add chain ip transp pre \ '{ type nat hook prerouting priority -100; }'nft add rule ip transp pre \ ip saddr 192.168.158.10 \ ip daddr 192.168.158.32 tcp dport 80 \ dnat to 192.168.158.20:8888# client: no proxy option, only a routeip route add 192.168.158.32/32 \ via 192.168.158.20curl -sv -m 5 http://192.168.158.32/* Connected to 192.168.158.32 (192.168.158.32) port 80> GET / HTTP/1.1> Host: 192.168.158.32< HTTP/1.0 200 OK< Via: 1.1 proxy (tinyproxy/1.11.2)Hello from web2 path=/ $ curl ... -H 'Host: ads.lab' http://192.168.158.32/403138.7 Common mistakes
- Treating a proxy like a VPN: HTTP proxies and SOCKS encrypt nothing; ssh -D encrypts only up to the SSH server; the proxy owner can see and log every request.
- Reading the client IP from the socket behind a load balancer: you get the LB's IP (192.168.158.20); use X-Forwarded-For (L7) or PROXY protocol (L4).
- No health checks: a dead backend keeps getting traffic.
- Expecting L4 to route by path or show an HTTP error page.
138.8 Interview tips
- Forward proxy = for clients, configured on clients; reverse proxy = for servers, client doesn't know.
- L4: IP + port, whole TCP/UDP connection, fast, any protocol. L7: reads HTTP, path/Host routing, TLS termination, X-Forwarded-For, higher CPU.
- Algorithms: round robin, least connections, IP hash; health checks remove DOWN servers.
- Real client IP behind a LB: X-Forwarded-For or PROXY protocol.