Put a load balancer in front of two real servers
Task
Run two backend web servers and a load balancer in front of them, then watch requests distribute. The appliance definitions in objective 1.2 get much easier to keep straight once you have seen a request arrive at a server you did not address.
Steps
- On Host B, start two backends that identify themselves. In one terminal:
mkdir -p /tmp/a && echo "backend A" > /tmp/a/index.html && cd /tmp/a && python3 -m http.server 8001In another, the same with/tmp/b, "backend B" and port 8002. - On the router, create
/etc/nginx/sites-enabled/lbwith an upstream block naming both backends —server 192.168.20.10:8001;andserver 192.168.20.10:8002;— and a server block on port 80 whose location doesproxy_pass http://app;. - Test the syntax before reloading:
sudo nginx -t. Doing that first is the habit worth forming — it catches the typo before the reload takes the service down. Thensudo systemctl reload nginx. - From Host A, request the balancer several times and watch the responses alternate between the two backends.
- Stop backend A with Ctrl-C. Request again, and note both what nginx returns and how long it takes to stop trying the dead server.
Verify
for i in 1 2 3 4 5 6; do curl -s http://192.168.10.1/ ; done
curl -s -o /dev/null -w "%{http_code}\n" http://192.168.10.1/
The six responses must not all be identical — round robin alternates A and B. After you stop backend A, every response must say "backend B" and the status code must stay 200. A 502 means nginx has not marked the dead backend down yet, which is itself the lesson about health checks.
Notes
You have now built three of the appliances in the objective at once: a load balancer distributing across backends, a reverse proxy, because nginx terminates the client connection and makes its own, and the place a web application firewall would sit if you added rule inspection.
The distinction the exam draws is that a proxy sits between clients and servers and changes the conversation, while a load balancer distributes across identical backends for capacity and availability. nginx is doing both here, which is exactly why the two terms get muddled in practice.