SkillPilotAI

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

Forward proxy: the office's one exit point
  1. 1.Office PC 192.168.158.10 has proxy 192.168.158.20:8888 configured

  2. 2.PC sends GET http://web1.lab/ to the proxy

  3. 3.Proxy forwards it over the internet to web1.lab

  4. 4.web1 sees only the proxy's IP

  5. 5.GET ads.lab matches the filter: proxy returns 403 right there

Reverse proxy: in front of the servers
  1. 1.User opens shop.lab (one IP = the reverse proxy)

  2. 2.Request 1 arrives at the reverse proxy -> forwarded to web1 (192.168.158.31)

  3. 3.Request 2 -> forwarded to web2 (192.168.158.32)

  4. 4.User never learns how many servers are behind it

PointForward proxyReverse proxy
Sits onClient side (office, school)Server side
Configured onThe client (proxy setting)The server side; client doesn't know
HidesClients from serversServers from clients
Used forFiltering, logging, caching outboundTLS termination, load balancing, caching
Examplestinyproxy, SquidNginx, HAProxy

138.3 Lab: forward proxy with tinyproxy

Terminal
curl -s http://192.168.158.31/curl -sv --proxy http://proxy:8888 \  http://web1.lab/# web1 server logcat /web.log
Output
Hello 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.
Terminal
# /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>'
Output
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

HAProxy as L4 and L7 load balancer
  1. 1.Client 192.168.158.10 -> HAProxy on 192.168.158.20

  2. 2.L4 (:9000, mode tcp): looks only at IP + port, hands the whole TCP connection to web1 or web2

  3. 3.L7 (:80, mode http): reads the HTTP request

  4. 4.GET /api -> web1; GET /img or Host img.lab -> web2; anything else -> round robin pool

  5. 5.L7 adds X-Forwarded-For with the client's real IP

Algorithm / featureWhat it does
Round robinweb1, web2, web1, web2 ...
Least connectionsSend to the least busy server
IP hash (sticky)Same client always to the same server
Health checkProbe backends (every 1s here); no traffic to a DOWN server
Output
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:80
Terminal
for 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; done
Output
Hello 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

Output
[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.
Terminal
ssh -o StrictHostKeyChecking=accept-new \  -f -N -D 1080 lab@proxycurl -sv --socks5-hostname 127.0.0.1:1080 \  http://web1.lab/
Output
* 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=- /
Terminal
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/
Output
* 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/403

138.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.