DevOps Kit Lessons Lesson 7 of 18

Checking your sign-in…

🔒

This lesson is in Phase 1 — ₹399, one-time

Pay once with UPI, card or netbanking. Keep it forever. Your progress saves, so you can stop and come back.

← Back to all lessons

Load Balancer Lab — One Guard, Three Backends

Phase 1 · The Data Tier

What this is and why it matters

A load balancer is a traffic guard. It stands in front of copies of your app and sends each visitor to a different copy.

One copy doing all the work gets overloaded. One dead copy takes the whole site down. The guard fixes both problems.

Think of a school with three identical canteen counters and one person at the door. The person at the door sends the first student to counter 1, the next to counter 2, the next to counter 3, then back to counter 1. No counter gets all the students. If counter 2 closes, the person at the door just sends students to counters 1 and 3. Nobody goes hungry. That person at the door is the load balancer. The counters are the backends.

In this lab you run one guard in front of three backends. You ask the guard for the page six times and see a different backend answer each time. Then you kill one backend and watch the site stay up. That is the whole lab, and it is the whole point of a load balancer.

Your level in Load Balancer
✓ Step 1 · Beginner→✓ Step 2 · Intermediate→✓ Step 3 · Advanced ← You are here
After this lesson you can: Bring a stopped backend back and fix a broken config during an on-call drill.
You have finished this topic’s ladder.
Advanced challenge: Site error 'Bad Gateway' (502) — find the wrong port in the server config (nginx.conf), check it, reload, see the site answer OK (200). — try it in Load Balancer.

Test status

Syntax-checked only (20 November 2026). The compose file and the NGINX settings were checked for correct syntax. This lab was not run end-to-end on this machine, because this machine has no Docker. Follow it on your own machine with Docker. No step here is promised to work on a real machine — if a step fails, read the error first. That is the real skill.

v2 (20 November 2026): the backends now use an image that works on ARM64 as well as x86_64. ARM64 and x86_64 are two kinds of computer chip. x86_64 is the kind in most Windows and Linux PCs. ARM64 is the kind in newer Macs and in many small computers. An image built for only one kind will not run on the other — this one works on both, so the lab works on either kind of machine. Each backend answers with its own name, so you can see which one answered. Still syntax-checked only, not run end to end.

Words you need here

Read this list first. Every word below is used in the steps. Nothing here is used before it is defined.

  • Docker — a tool that runs small sealed boxes on your machine. Each box holds one program and everything that program needs. You need Docker running before Step 1.
  • Container — one sealed box run by Docker. In this lab there are four boxes: lb, web1, web2, and web3. Each box does one job.
  • Image — the recipe Docker uses to build a box. A box is a running image.
  • Service — one named job in the file docker-compose.yml. Each service becomes one container. The four services here are lb, web1, web2, and web3.
  • docker-compose.yml — a recipe file. It lists the four services, what image each one uses, and how they talk to each other. It lives in the folder phase1/load-balancer-lab.
  • Load balancer — a traffic guard. It stands in front of copies of your app and sends each visitor to a different copy.
  • Backend — one copy of your app, behind the load balancer. In this lab the three backends are called web1, web2, and web3. Each one is a tiny program that answers with its own name, so you can see which one answered.
  • NGINX — a free program that can act as a load balancer. Say it "engine-X". In this lab NGINX is the guard. The guard's box is called lb, short for "load balancer".
  • nginx.conf — the settings file for NGINX. It lives in the folder phase1/load-balancer-lab. It holds two things: a list of the three backends, and a front door that hands visitors to them. The file is explained line by line in Step 1 below — you do not need to edit it.
  • Upstream — NGINX's word for the list of backends. In nginx.conf, the list is called backends and holds web1, web2, and web3.
  • Round robin — taking turns. Backend 1, then 2, then 3, then 1 again. That is how NGINX shares the work by default. Like dealing cards around a table, one each in turn.
  • Port — a door number on a machine. The guard listens on door 8080 on your machine. Inside its box it listens on door 80. The recipe file maps door 8080 on your machine to door 80 inside the box, so when you visit localhost:8080 you reach the guard.
  • localhost — a name that means "this machine". http://localhost:8080/ means "the web page served by my own machine, on door 8080". Nothing leaves your machine.
  • curl — a small tool that asks a web page for its content and prints the answer in your terminal. It is a command, not a browser. You type it, it fetches, it prints.
  • HTTP — the language browsers and web programs use to ask for pages. Plain HTTP is not locked or scrambled. This lab uses plain HTTP on your own machine only.
  • Terminal — the black window where you type commands. Every command in this lab is typed in the terminal, then you press Enter.
  • Folder / directory — the same thing. A folder holds files. This lab says "folder" every time.
  • F5 — a paid box that does the load-balancer job in big companies. This lab uses NGINX instead. It is free, and it teaches the same idea. The lab says so honestly wherever it matters.

What you need first

  • Docker running on your machine. The lesson points to Phase 2, zero-cost-lab.md, for setting Docker up. Do that first if Docker is not installed yet.
  • How to tell Docker is running: open your terminal and type docker ps, then press Enter. docker is the tool. ps means "show the boxes that are running". If Docker is running, you see a list — even an empty list — with no error. If you see an error about the daemon, Docker is not running yet. A daemon is just a background program — here, the Docker program itself running behind the scenes.
  • The files in the folder phase1/load-balancer-lab. That folder holds docker-compose.yml and nginx.conf. Both paths were checked to exist on disk. The folder also holds a helper file called drill.sh. You do not need to open it — the failure drill below uses plain commands instead.

Getting into the right folder

Every command in this lab is run from inside the folder phase1/load-balancer-lab.

  • What: cd means "change folder". It moves your terminal into a folder.
  • Why: the commands below look for docker-compose.yml in the folder your terminal is sitting in. If you are in the wrong folder, they fail.
  • How: open your terminal in the folder phase1/load-balancer-lab on your machine. If your terminal opens somewhere else, type this command, then press Enter:

`` cd phase1/load-balancer-lab ``

Type it exactly as shown, from the folder that holds phase1. cd means "change folder" — it moves your terminal into the folder named after it.

  • Check you are in the right place: type ls and press Enter. ls means "list the files here". You should see docker-compose.yml and nginx.conf in the list. If you do not see them, you are in the wrong folder. Move again until ls shows them.

Step 1 — Start the guard and the three backends

What this step does: it starts four boxes: the NGINX guard called lb, and the three backends web1, web2, and web3.

Why: nothing else works until the guard and the backends are running. The guard has nobody to send visitors to if the backends are down.

The command, word by word, before you run it:

Try it Yourself »Open the practice terminal and run it yourself — nothing installs. Included with Phase 1. Simulation — practice version. Run the real lab with Docker to do it for real.
Command
docker compose up -d
  • docker — the tool that runs boxes.
  • compose — read the recipe file docker-compose.yml and run everything it lists.
  • up — start everything in the recipe.
  • -d — run in the background. Short for "detached". It means: start the boxes and give me my terminal back, so I can type the next command. Without -d, the boxes would hold your terminal and you could not type anything else.

Run it now, from inside the folder phase1/load-balancer-lab.

What you should see:

Command
[+] Running 4/4
 ✔ Container lb    Started
 ✔ Container web1  Started
 ✔ Container web2  Started
 ✔ Container web3  Started

Line by line:

  • [+] Running 4/4 — four out of four boxes are running. All of them.
  • ✔ Container lb Started — the guard box started. ✔ is a tick mark.
  • ✔ Container web1 Started, ✔ Container web2 Started, ✔ Container web3 Started — the three backend boxes started.

How to tell it worked: run the check command below.

The check command, word by word:

Command
docker compose ps
  • docker compose — the tool, reading the recipe file.
  • ps — show the boxes from this recipe and their state.

What you should see: a table with four names, each with STATUS "Up". "Up" means the box is running.

Command
NAME    STATUS
lb      Up
web1    Up
web2    Up
web3    Up

The real table has more columns than this. What matters is all four names, each with STATUS "Up".

If it breaks: if lb shows "Exited", the NGINX settings file has a mistake. "Exited" means the box ran and then stopped. Read the guard's diary with:

Command
docker compose logs lb
  • docker compose logs — show the diary lines for one box. A log is the box's diary — lines it printed while it ran.
  • lb — the guard box.

The error line names the line number in nginx.conf. Fix that line and run docker compose up -d again.

What is inside nginx.conf? You do not need to edit it, but you should know what it says. Open the file phase1/load-balancer-lab/nginx.conf. It has two parts:

  1. The backend list, called upstream backends. It names the three backends:
  2. server web1:80; — backend 1, on its door 80.
  3. server web2:80; — backend 2, on its door 80.
  4. server web3:80; — backend 3, on its door 80.
  5. The front door, called server. It says listen 80; — take visitors on door 80 — and proxy_pass http://backends; — hand each visitor to the backend list. proxy_pass means "pass the visitor on to". location / means "for every page address", so every visitor is handed on.

If a backend stops answering, NGINX does not notice by itself: one visitor's request to the dead backend fails first, then NGINX tries the next backend and uses the ones that are still alive. After that one failure the dead backend is set aside for 10 seconds (the defaults — max_fails=1, fail_timeout=10s). That skipping, after one failed request, is the whole trick of Step 3. The full correction is in Advanced 1 at the end of this lesson.

One thing the lesson does not spell out: you cannot visit a backend directly. The backends are not reachable from your machine — only through the guard. Do not try to open web1 in a browser. Always go through http://localhost:8080/.

Step 2 — Ask for the page six times

What this step does: you ask the guard for the page six times, and watch a different backend answer each time.

Why: this proves the guard shares the work. If the same backend answered every time, the guard would not be sharing anything.

The command, word by word, before you run it:

Command
curl http://localhost:8080/
  • curl — fetch a web page and print the answer in the terminal.
  • http://localhost:8080/ — the page to fetch. http is the web language. localhost means "my own machine". 8080 is the guard's door number on your machine. / means "the front page".

Run it once now. You should see several lines of answer. The lines include details about the backend that answered. Find the line that starts with:

Command
Hostname: web1

Hostname is the name of the backend that answered. It will say web1, web2, or web3. The other lines in the answer are details about that backend — you can ignore them for this lab. The Hostname line is the proof. The lesson shows only the Hostname line because that is the line that matters.

Now ask again, five more times. Run the same command five more times:

Command
curl http://localhost:8080/
curl http://localhost:8080/
curl http://localhost:8080/
curl http://localhost:8080/
curl http://localhost:8080/

That is one command, typed five times. Each run is one visit. Write down the Hostname line each time.

What you should see: the Hostname line takes turns — web1, web2, web3, web1, web2, web3. The order may start on a different backend — your first answer might be web2 instead of web1 — but all three names appear, and they keep taking turns:

Command
Hostname: web1
Hostname: web2
Hostname: web3
Hostname: web1
Hostname: web2
Hostname: web3

That is round robin — the guard shares the work evenly, one visitor to each backend in turn. The starting backend does not matter: the guard remembers whose turn is next, and it may have already served a visitor before you started counting. What matters is that all three names appear and they keep taking turns.

If it breaks: if curl says "connection refused", the guard is not up yet. "Connection refused" means nothing answered on door 8080. Wait a few seconds and try again. If every answer is the same backend, check docker compose ps — one of the backends may have failed to start, so the guard is only using the ones that are alive. That is still the guard working correctly, but fix the dead backend first (read its diary with docker compose logs web2, using the real name) and try again.

Step 3 — Kill one backend and watch the site stay up

What this step does: you stop backend 2, ask for the page six more times, and watch the site stay up with only two backends. Then you bring backend 2 back.

Why: this is the real test. A site with one copy dies when that copy dies. A site with a guard stays up when one copy dies, because the guard does not notice by itself — one visitor's request to the dead backend fails first, then the guard tries the next backend, and the dead one is set aside for 10 seconds.

First — stop backend 2. The command, word by word:

Command
docker compose stop web2
  • docker compose stop — stop one box from this recipe, but keep it around so it can be started again later. Different from down, which stops AND removes the boxes.
  • web2 — the box to stop. Backend 2.

What you should see:

Command
✔ Container web2  Stopped

One line. Stopped means the box is off but still exists.

If it breaks: "No such container" means the name is wrong — run docker compose ps and use one of the names it shows.

Next — ask for the page six more times, in one command. The command, word by word, before you run it:

Command
for i in 1 2 3 4 5 6; do curl -s http://localhost:8080/ | grep Hostname; done

This looks long, so here it is in pieces:

  • for i in 1 2 3 4 5 6; do ... ; done — a loop. It means "do the thing inside, six times". i is a counting name that goes 1, 2, 3, 4, 5, 6. do starts the repeated part. done ends it. You are not typing the page request six times by hand — the loop does it for you.
  • curl -s http://localhost:8080/ — fetch the page. -s means "silent": hide the progress lines curl normally prints, so only the page answer shows.
  • | — a pipe. It means "take the output of the command on the left and feed it into the command on the right".
  • grep Hostname — keep only the lines that contain the word Hostname, and throw the rest away. grep means "search lines for this word". This is why you see only the Hostname line six times, instead of the full answer six times.

On Windows, run this in Git Bash, not PowerShell. Git Bash is a terminal that understands this loop language. PowerShell is Windows' own terminal, and it does not understand for ... do ... done in this form. If you do not have Git Bash, it comes with Git for Windows — install Git and you get Git Bash with it.

What you should see: only Hostname: web1 and Hostname: web3:

Command
Hostname: web1
Hostname: web3
Hostname: web1
Hostname: web3
Hostname: web1
Hostname: web3

Backend 2 never answers — one request to it fails, then the guard uses the two backends that are still alive. The site stays up with two backends. That is the whole point of a load balancer: one copy can die and nobody notices.

Next — bring backend 2 back. The command, word by word:

Command
docker compose start web2
  • docker compose start — start a box that was stopped earlier.
  • web2 — the box to start. Backend 2.

What you should see:

Command
✔ Container web2  Started

Ask again, once:

Command
curl http://localhost:8080/

What you should see: a Hostname line again — web1, web2 or web3:

Command
Hostname: web2

Your answer may name a different backend — web1 or web3 are also fine. Any of the three means backend 2 answers again within a few seconds.

If it breaks: if web2 never answers, check it is really up with docker compose ps — if it shows "Exited", read its diary with docker compose logs web2.

Step 4 — See the guard's view (optional)

What this step does: it shows you the guard's diary — one line per visit.

Why: so you can see what the guard records, and what it does NOT record. This step is optional. Skip it and nothing breaks.

The command, word by word:

Command
docker compose logs lb
  • docker compose logs — show the diary lines for one box.
  • lb — the guard box.

What you should see: one line per visit, like this:

Command
172.18.0.1 - - [20/Nov/2026:10:00:00 +0000] "GET / HTTP/1.1" 200 612

Line by line — it has six parts:

  • 172.18.0.1 — the visitor's address inside the Docker network. An address is a number that names one machine or box on a network. This one is your machine as the guard sees it.
  • - - — two dashes. They stand for two empty fields: the visitor gave no login name. Empty is normal here.
  • [20/Nov/2026:10:00:00 +0000] — the date and time of the visit. Your date and time will match when YOU ran it, not the line above.
  • "GET / HTTP/1.1" — the request. GET means "give me this page". / is the front page. HTTP/1.1 is the version of the web language used.
  • 200 — the answer code. 200 means "OK". The page was served fine. Other codes mean other things — 404 means "page not found", 502 means "the guard is up but no backend answered" — but here you want 200.
  • 612 — the size of the answer, in bytes. A byte is the smallest unit of file size. 612 just means the answer was 612 bytes long.

There is no line here that says which backend answered. NGINX's normal diary does not record that. The curl answers in Step 2 are the proof, not this diary.

Clean up

What this step does: it stops all four boxes and removes them.

Why: the lab is done. The boxes would otherwise keep running and using your machine.

The command, word by word:

Command
docker compose down
  • docker compose — the tool, reading the recipe file.
  • down — stop all the boxes in the recipe and remove them.

Run it from inside the folder phase1/load-balancer-lab.

What you should see:

Command
✔ Container lb    Removed
✔ Container web1  Removed
✔ Container web2  Removed
✔ Container web3  Removed

Four lines, one per box. ✔ is a tick mark. Removed means the box is gone.

If it breaks: "No such container" means the services are already stopped — nothing to do.

What you can now say, truthfully

"I ran NGINX as a load balancer in front of three backends, watched it share visitors round robin, killed one backend, and the site stayed up."

What this lab does NOT cover

  • Real load balancers also check backend health on their own — health checks, meaning the guard asks each backend "are you alive?" on a schedule — and can share work by least-busy instead of round robin. "Least-busy" means sending each new visitor to the backend with the fewest visitors right now, instead of taking strict turns. NGINX can do both — this lab keeps the default, round robin, so you learn the idea first.
  • HTTPS is the locked version of web traffic. "Locked" means the traffic is scrambled so nobody in the middle can read it. HTTPS is a later topic. This lab uses plain HTTP on your own machine only.
  • A paid F5 box has more features than NGINX. The idea — spread traffic, skip dead backends — is the same.
  • The Advanced section at the end of this lesson now covers health checks, weights, sticky sessions, and TLS — and corrects the "NGINX skips a dead backend by itself" claim.

Failure drill — Lose a backend, stay up

This drill is the SAME test as Step 3, repeated on its own so you can practise it from a cold start. If you already did Step 3, you have already done this drill once. Do it again here if you want the practice — it is the same moves, in the same order.

This drill proves the site stays up when one backend dies. The guard does not notice by itself — one visitor's request to the dead backend fails first, then the guard tries the next backend, and the dead one is set aside for 10 seconds.

Do Steps 1 and 2 first, so all three backends are answering.

Drill Step 1 — Stop backend 2. Run this from inside the folder phase1/load-balancer-lab:

Command
docker compose stop web2
Expected output
✔ Container web2  Stopped

Drill Step 2 — Ask for the page six times:

Command
for i in 1 2 3 4 5 6; do curl -s http://localhost:8080/ | grep Hostname; done

This is the loop from Step 3: fetch the page six times, keep only the Hostname line each time. See Step 3 for the word-by-word breakdown.

You should see only Hostname: web1 and Hostname: web3:

Command
Hostname: web1
Hostname: web3
Hostname: web1
Hostname: web3
Hostname: web1
Hostname: web3

Backend 2 never answers. One request to it fails, then the guard uses the two backends that are still alive. The site stays up with two backends.

Drill Step 3 — Bring backend 2 back:

Command
docker compose start web2
Expected output
✔ Container web2  Started

Ask again:

Command
curl http://localhost:8080/
Expected output
Hostname: web2

Backend 2 answers again within a few seconds.

Checklist — tick each one when you have seen it:

  • ☐ The site stayed up with web2 stopped.
  • ☐ Only web1 and web3 answered the six curls — web2 never did.
  • ☐ Web2 answered again after it was started back.

Reset — wipe the drill and start fresh. Run this from the phase1 folder (one folder up from the lab folder):

Command
bash reset.sh load-balancer-lab

Word by word:

  • bash — run this file with Bash, the terminal's command language.
  • reset.sh — a script file that stops the lab and deletes its data, so the lab starts fresh. It lives in the folder phase1 — the file is phase1/reset.sh. That path was checked to exist on disk.
  • load-balancer-lab — which lab to reset. The script needs the lab folder's name.

You should see: the script stops the services and wipes the data, then prints that it is done.

If it breaks: "Permission denied" means the file is not marked as runnable. "Permission denied" is the machine saying "you are not allowed to run this file". Fix it with:

Command
chmod +x reset.sh
  • chmod — change a file's permissions (who may do what with it).
  • +x — add the "may run this" permission.
  • reset.sh — the file to fix.

Then run the reset command again.

Or, from inside the folder phase1/load-balancer-lab, the plain way: run docker compose down. That stops the guard and the three backends. No script needed.

Save your proof

The proof for this lab is the curl loop with web2 missing: six answers, only web1 and web3. Save it with the commands below, run from inside the folder phase1/load-balancer-lab while web2 is still stopped (Drill Step 2). Timing matters: the file must be saved while web2 is still stopped. If you start web2 back first, the file will hold web2 answers and prove nothing.

First — make the proof folder, word by word:

Command
mkdir -p ../portfolio/proof
  • mkdir — make a folder. Short for "make directory".
  • -p — make the parent folders too if they are missing, and do not complain if the folder already exists.
  • ../portfolio/proof — the folder to make. .. means "go up one folder" — from phase1/load-balancer-lab up to phase1. Then portfolio/proof is the folder inside phase1/portfolio. The full path is phase1/portfolio/proof, and it holds the proof files. That folder was checked to exist on disk.

Then — save the six answers into a file:

Command
for i in 1 2 3 4 5 6; do curl -s http://localhost:8080/ | grep Hostname; done > ../portfolio/proof/load-balancer-web2-down.txt

This is the same six-time loop from Step 3, with one extra piece at the end:

  • > — send the output into a file instead of onto the screen. It overwrites the file if the file already exists.
  • ../portfolio/proof/load-balancer-web2-down.txt — the file to write into. A plain text file. Its name says what it holds: the load balancer's answers with web2 down.

What you should see: nothing in the terminal when you run the save command. That is correct — the six answers went into the file, not the screen. Silence means it worked here.

Check the file: open phase1/portfolio/proof/load-balancer-web2-down.txt and check web2 never appears in it. That file is your proof.

If it breaks: if the file is empty, web2 was already started again when you saved — stop web2 first (Drill Step 1), then run the save command again.

Advanced — Health checks, weights, sticky sessions, TLS, and the correction

This section is for the reader who already did the lab above. It covers the four things the review of this kit flagged as missing for advanced readers: health checks, weights, sticky sessions, and TLS — plus the full explanation of what NGINX really does when a backend dies. The commands were checked against this lab's own files and the NGINX docs. The tuned config below was not run end to end on this kit — read the nginx -t check before you reload, as the section says.

Advanced 1 — The correction: NGINX does NOT check backends by itself

The short version in the steps above — "one request fails, then NGINX tries the next backend" — is easy to misread as "NGINX notices a dead backend by itself". It does not. With the lab's default config, that reading is only half true, and the half that is wrong matters. Here is what the default config actually does, in plain words.

Open-source NGINX never asks a backend "are you alive?" on its own schedule. It has no active health check. It only learns from real visitor requests — that is called a passive check. With the lab's default config, this is the sequence when a backend dies:

  1. A visitor's request is sent to the dead backend. That one request fails.
  2. NGINX tries the same request on the next backend (proxy_next_upstream — "try the next upstream" — defaults to retrying on error and timeout, meaning a failed connection or a wait that ran out). So the visitor usually still gets an answer, but one request was spent learning the backend is dead.
  3. After 1 failure (max_fails=1, the default), the dead backend is set aside for 10 seconds (fail_timeout=10s, the default). After 10 seconds NGINX tries it again with live traffic — if it answers, it is back in rotation.

And here is the part the lesson gets wrong: a backend that answers with 500 errors is NOT skipped by default. A 500 is an answer — the backend is alive, it is just broken — and 500 is not in the default retry list. So NGINX keeps sending visitors to a backend that errors on every request, forever. The fix is to add the error codes to the retry list — next section — but know the cost first: retrying can repeat a request that changes something (a payment, an order) twice. That is why the default list is narrow. Never add http_500 to a retry list for requests that change data unless the app makes repeats safe.

Advanced 2 — Tune it: weights, least-busy, and failure counts

What this is: the upstream block in nginx.conf is where you tell NGINX how to share the work and when to set a backend aside. Why tune it: the defaults (strict turns, 1 failure, 10 seconds) are a learning setup. Real setups share work by load and give a flaky backend more chances before dropping it.

Try this in the lab. Open phase1/load-balancer-lab/nginx.conf and change the upstream block to:

Command
upstream backends {
    least_conn;
    server web1:80 weight=3 max_fails=3 fail_timeout=30s;
    server web2:80 weight=1 max_fails=3 fail_timeout=30s;
    server web3:80 max_fails=3 fail_timeout=30s;
}

Word by word:

  • least_conn — instead of strict turns, send each new visitor to the backend with the fewest live connections right now. "Least-busy" in plain words. Use it when requests take different amounts of time — strict turns overload the backend holding the slow requests.
  • weight=3 — give web1 three shares of the traffic for every one share web2 gets. Use weights when backends are not equal — a bigger machine deserves more traffic. No weight means 1.
  • max_fails=3 — a backend is set aside only after 3 failures, not 1. Use it when a backend is flaky but usually recovers.
  • fail_timeout=30s — the 3 failures must happen within 30 seconds, and then the backend stays set aside for 30 seconds. One number, two jobs: the counting window and the time-out.

And in the server block, under the proxy_pass line, add what counts as a failure:

Command
proxy_next_upstream error timeout http_500 http_502 http_503;

Word by word: retry the request on the next backend when the first one fails to connect (error), times out (timeout), or answers with a 500, 502, or 503 error. 502 means "bad gateway" — the backend gave a broken answer. 503 means "service unavailable" — the backend is up but refusing work. Do NOT add http_500 for requests that change data (see Advanced 1). Before you reload, test the config — a typo in nginx.conf stops NGINX from starting:

Command
docker compose exec lb nginx -t

Word by word: nginx -t means "test the config files and report errors, but do not start anything". What you should see: syntax is ok and test is successful. Only then reload:

Command
docker compose exec lb nginx -s reload

Word by word: nginx -s reload means "reload the config without dropping visitors". If the test fails, fix the file and test again — never reload a failed config. To go back to the lab's original config, restore the upstream block to the three plain server web1:80; lines and remove the proxy_next_upstream line, then test and reload again.

Advanced 3 — Sticky sessions: one visitor, one backend

What this is: a sticky session means the same visitor always goes to the same backend. Why it matters: some apps keep a visitor's login or shopping cart in one backend's memory. If the next request goes to a different backend, the cart is gone.

The one-line way in NGINX is ip_hash; — put it in the upstream block, instead of least_conn:

Command
upstream backends {
    ip_hash;
    server web1:80;
    server web2:80;
    server web3:80;
}

Word by word: ip_hash hashes the visitor's address into a number and uses it to pick a backend, so the same address always picks the same one. The cost, stated plainly: traffic is no longer even (one busy office behind one address overloads one backend), and when a backend dies, its visitors lose their sessions. The better fix in real work is to store sessions in a shared store (like the Redis in this kit) so any backend can serve any visitor — then you do not need sticky sessions at all.

Advanced 4 — Active health checks: the paid feature

What this is: an active health check is NGINX asking each backend "are you alive?" on its own schedule, even when no visitor is asking. Why it matters: with passive checks only, the first visitor after a backend dies pays for the discovery. With active checks, NGINX knows before any visitor is hurt.

The honest fact: open-source NGINX does not have active health checks. The health_check setting is a paid NGINX Plus feature. If you need active checks for free, the tools that ship them are HAProxy, Envoy, Traefik, and Caddy — named here so you know what to reach for. The app-side fix works with any of them: give each backend a health endpoint — a tiny address that answers 200 only when the backend can really serve (its database answers, its disk is not full) — and point the health check at that, not at the home page. A home page can answer 200 while the app behind it is broken. In real work that endpoint is often named /healthz.

Try it in this lab, with a health endpoint that already exists. This lab's backends are traefik/whoami, and whoami ships a /health address: asking it answers 200 while that backend is healthy. Run this, with the lab still up, from inside the folder phase1/load-balancer-lab:

Command
curl -i http://localhost:8080/health

Word by word: curl asks a web address and prints the answer. -i means "show the answer's header lines too, not just the body". http://localhost:8080/health is the guard's address, plus /health — the guard passes it to one of the backends. What you should see: a first line that says HTTP/1.1 200 OK. 200 means "healthy". Ask a few times: the guard shares the work, so the answers come from different backends, and each one answers 200.

You can even make one backend unhealthy and watch it. Pick one backend and tell it to answer 500 instead, with a POST — a request that sends data, not just asks:

Command
curl -X POST -d '500' http://localhost:8080/health

Word by word: -X POST means "send a POST request". -d '500' means "send the number 500 as the data". The guard passes it to one backend, and that backend now answers 500 to health checks until you restart it. Run the first command again a few times: most answers are still 200, but one in three is now 500 — the unhealthy backend, still in rotation, because open-source NGINX has no active check to notice it. That is exactly the gap this section describes. To undo it, restart the lab (docker compose down then docker compose up -d) — the health setting lives only in the running backend, not in any file.

If it breaks: "Failed to connect" means the lab is not up. Run docker compose up -d first, then try again. A 404 means you typed the address wrong — it is /health, not /healthz: whoami has no /healthz address.

Advanced 5 — TLS basics

What this is: TLS is the scrambling that turns HTTP into HTTPS. HTTPS is HTTP with the traffic scrambled, so nobody in the middle can read it. Why it matters: this lab uses plain HTTP on your own machine only. That is fine for learning and unsafe on the open internet — anyone on the same network can read the traffic, including logins.

The real setup, in plain words: a certificate is a file that proves the server is who it says it is, and it is what makes the scrambling possible. In real work the load balancer holds the certificate and ends the TLS there — visitors talk scrambled traffic to the balancer, and the balancer talks plain HTTP to the backends inside a private network. This lab has no certificate files, so no commands here. Know the shape: TLS at the balancer, plain traffic behind it, never plain traffic facing the internet.

What you can now say, truthfully — advanced

  • "I can explain what the default NGINX config actually does when a backend dies — one failed request, then set aside for 10 seconds — and why a backend returning 500s is NOT skipped by default." (Only if you did Advanced 1.)
  • "I tuned an upstream block with least_conn, weight, max_fails, fail_timeout, and proxy_next_upstream, and tested it with nginx -t before reloading." (Only if you did Advanced 2.)
  • "I know sticky sessions (ip_hash) cost even traffic, and that a shared session store is the better fix." (Only if you did Advanced 3.)
  • "I know active health checks are a paid NGINX Plus feature, and what HAProxy, Envoy, Traefik, and Caddy offer instead." (Only if you did Advanced 4.)

Quick check — try it yourself

Question: In the terminal, type: curl http://localhost:8080/ — which backend server (app copy behind the traffic guard) answers?

Open the practice terminal to test this →

Show answer

You should see Hostname: web1, showing that the first backend server answered.