Request Routing: Web Servers & Proxies
When you type a URL and press Enter, who answers the door? Learn how Web Servers (Nginx, Apache, IIS) serve files, host 10 websites on one IP, act as a Reverse Proxy shield, and lock traffic with HTTPS.
Start Module 09 ↓01 How a Web Server Works
Define: What is a Web Server?
A Web Server is a piece of software running on a computer that listens on network ports (usually TCP 80 for HTTP and TCP 443 for HTTPS), waits for incoming requests from browsers, and sends back a response — either a ready-made file or output from an application.
Understand: The Post Office & Menu Analogy
Imagine ordering food from a restaurant by phone:
- DNS Lookup is looking up the restaurant's phone number in your contacts (turning
shop.example.cominto203.0.113.10). - TCP Connection (Port 80/443) is dialing the front desk and hearing "Hello, how can I help you?"
- Static Files (HTML, CSS, images, PDFs) are pre-packaged snacks sitting on the counter — the receptionist hands them to you immediately.
- App Server (Node.js, Python, PHP, .NET) is the kitchen — when you ask for your live bank balance or shopping cart, the receptionist passes the ticket to the kitchen to cook up a fresh response.
Know & When: Static vs. Dynamic
Web servers excel at reading files from disk and streaming them over the network at blistering speed. Application servers (like Gunicorn, Node.js, or ASP.NET Core) excel at running business logic and querying databases. In production, you almost always put a web server in front of your app server.
🧠 Memory Trick: Port 80 vs Port 443
80 is Plain (HTTP — open postcard anyone can read), 443 is Fortress (HTTPS — 4-4-3 has three digits like a 3-dial padlock). Never leave production logins on Port 80!
Execute: Check What is Listening on 80 & 443
# Linux: see which process owns ports 80 and 443
sudo ss -tulpn | grep -E ':80|:443'
# Windows PowerShell: see which PID owns ports 80 and 443
Get-NetTCPConnection -LocalPort 80,443 -State Listen
02 Nginx vs Apache vs Windows IIS
Define: The Big Three Web Servers
Almost every website on the internet is served by one of three engines: Nginx (pronounced "Engine-X"), Apache HTTP Server, or Microsoft IIS (Internet Information Services).
Understand: Receptionist vs Dedicated Waiters
Why does Nginx handle 10,000+ simultaneous visitors on tiny RAM while classic Apache struggles? It comes down to how they handle connections:
- Nginx (Event-Driven Receptionist): One super-fast receptionist sits at the front desk with an event loop. When Visitor A is waiting for a slow database, the receptionist doesn't freeze — they immediately greet Visitors B, C, and D, and hand A their food the instant it's ready.
- Apache (Dedicated Waiters — Process/Thread): Classic Apache assigns a dedicated
waiter (process or thread) to each table. While the customer reads the menu, that waiter stands
idle, holding RAM. Great for compatibility and
.htaccess, heavier under massive concurrency. - Windows IIS (Kernel-Integrated Kitchen): Built right into Windows Server with
http.sysin the kernel, managed via GUI or PowerShell, and deeply integrated with Active Directory and ASP.NET.
Know: Side-by-Side Comparison
| Feature | Nginx | Apache HTTP Server | Windows IIS |
|---|---|---|---|
| OS Support | Linux, BSD, Windows | Linux, BSD, Windows | Windows Server only |
| Architecture | Event-driven, async | Process / Thread (MPM) | Kernel http.sys + Worker Pools |
| Per-Folder Config | No (centralized config = faster) | Yes (.htaccess) |
Yes (web.config) |
| Best Use Case | Reverse proxy, static files, high traffic | Shared hosting, legacy PHP / WordPress | Enterprise .NET, Active Directory SSO |
When & Execute
Choose Nginx (or modern Caddy) for reverse proxies, containers, and
high-concurrency APIs. Choose Apache if you need per-directory
.htaccess rules on shared hosting. Choose IIS when your company runs
Windows Server and ASP.NET.
✅ Checkpoint: Sections 01–02
- Web servers listen on TCP 80 (HTTP) and TCP 443 (HTTPS).
- Static files are served straight from disk; dynamic requests go to an App Server.
- Nginx uses an event-driven receptionist model, keeping memory usage tiny at high scale.
03 Virtual Hosts & Server Blocks
Define: What is a Virtual Host?
A Virtual Host (called a Server Block in Nginx, VirtualHost in Apache, and Site Binding in IIS) lets a single server with one public IP address host 10, 50, or 500 completely different websites.
Understand: The Apartment Building Mailbox Analogy
Think of your server's IP address (203.0.113.10) as the street address of an apartment
building. All mail arrives at the same front door. How does the building manager know which
apartment gets the letter? They read the name on the envelope!
In HTTP, every browser automatically puts an envelope label inside the request called the
Host: header (for example, Host: shop.example.com). The web server reads
that header and routes the request to the matching folder or backend app.
Know & Execute: Nginx Server Block Example
In Ubuntu/Debian Nginx, each website gets its own file in /etc/nginx/sites-available/,
then you enable it with a symlink into /etc/nginx/sites-enabled/:
server {
listen 80;
server_name shop.site.com www.shop.site.com;
root /var/www/shop;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
✏️ Practice Problem: Missing Host Header
You configure two Nginx server blocks: alpha.com and beta.com on the
same IP 203.0.113.10. A user types http://203.0.113.10 directly into
their browser instead of a domain name. Which site will Nginx serve?
Show Solution
Nginx will serve whichever server block has default_server on
listen 80 default_server; — or if none is marked, the first
server block loaded alphabetically. Best practice: always define an
explicit catch-all default_server block that returns 444 or a
neutral page so scanners hitting your raw IP don't accidentally see a tenant site.
04 Reverse Proxy & Load Balancing
Define: Forward Proxy vs Reverse Proxy
- Forward Proxy sits in front of clients (e.g., an office VPN/proxy hiding employees' IPs from the internet).
- Reverse Proxy sits in front of servers — clients think they are talking directly to your app, while the proxy quietly shields, caches, and distributes traffic behind the scenes.
Understand: The Nightclub Bouncer & Traffic Cop
A Reverse Proxy is your backend's bodyguard and dispatcher. It handles four critical jobs so your application code doesn't have to:
SSL Termination
The proxy decrypts heavy HTTPS traffic once at the front door, then forwards fast plain HTTP
to internal backend apps on 127.0.0.1 or a private VPC.
Load Balancing
Spreads incoming requests across multiple app instances (Round Robin, Least Connections, or IP Hash) so no single server melts down.
Health Checks
If App Node 2 crashes or stops responding, the proxy marks it unhealthy and routes 100% of users to healthy nodes automatically.
Caching & Compression
Caches popular responses in RAM and compresses payloads with Gzip/Brotli before sending them over slow mobile networks.
Execute: Nginx Upstream Load Balancer Config
upstream app_cluster {
server 10.0.1.11:3000 max_fails=3 fail_timeout=30s;
server 10.0.1.12:3000 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://app_cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
X-Real-IP and
X-Forwarded-For, your backend app logs will show every single visitor as coming from
127.0.0.1 (the proxy itself)!
05 SSL/TLS & Let's Encrypt HTTPS
Define: What is HTTPS & TLS?
HTTPS is HTTP wrapped inside TLS (Transport Layer Security — the modern replacement for SSL). It guarantees three things: Encryption (nobody on public Wi-Fi can read your data), Integrity (nobody can tamper with it), and Authentication (you are talking to the real server, not an impostor).
Understand: How the Browser Padlock Works
How does your browser trust a server it has never met before? Through a Certificate Authority (CA) — a trusted passport issuer like Let's Encrypt. When the browser connects on Port 443, the server presents its signed digital passport (TLS Certificate). The browser verifies the signature, agrees on a shared secret session key via the TLS Handshake, and locks the tunnel.
Know & Execute: Practical Certbot & nginx -t Workflow
In 2026, you never buy basic TLS certificates manually — Certbot (or Caddy's
built-in ACME) fetches and auto-renews free 90-day Let's Encrypt certificates. Always test your
config syntax with nginx -t before reloading!
# 1. Obtain & auto-configure a free Let's Encrypt HTTPS certificate
sudo certbot --nginx -d app.example.com
# 2. ALWAYS test Nginx syntax before applying (prevents downtime!)
sudo nginx -t
# Output: nginx: configuration file /etc/nginx/nginx.conf test is successful
# 3. Reload gracefully without dropping active user connections
sudo systemctl reload nginx
🧠 Memory Trick: Test Before Reload!
Remember nginx -t = Test, Then Trigger. Never run
systemctl restart nginx blindly on a live server — a single missing semicolon
(;) will take every site offline. nginx -t catches syntax errors
first, and reload applies changes with zero dropped connections.
✏️ Practice Problem: Broken Config Recovery
You edit /etc/nginx/sites-available/api.conf and run sudo nginx -t.
It reports: nginx: [emerg] unexpected "}" in /etc/nginx/sites-available/api.conf:14.
What should you do next, and are live users currently affected?
Show Solution
Live users are NOT affected yet! Because you ran nginx -t
instead of restarting, the old working configuration is still running in memory. Fix
the missing semicolon (;) on line 13 (just before the closing
} on line 14), re-run sudo nginx -t until it says
test is successful, and then run sudo systemctl reload nginx.
🎯 Knowledge Check
1. Why can Nginx handle thousands of concurrent connections using far less RAM than classic process-per-connection servers?
2. How does a single web server with one IP address know whether to serve
shop.site.com or blog.site.com?
Host header (and TLS SNI) in the incoming
request
3. What does "SSL Termination" at a Reverse Proxy mean?
4. Which command safely verifies your Nginx configuration syntax before applying changes to a live server?
sudo nginx -t
sudo systemctl restart nginx
sudo certbot --verify