TExcelTAD
Home › Web Servers in Depth: Nginx, HAProxy, Caddy & Traefik › The Front Door of Every Stack

The Front Door of Every Stack

Every request to a web application passes through at least one piece of software before it reaches the app: a web server or proxy that accepts the TCP connection, terminates TLS, picks a backend, perhaps answers from a cache or refuses the request because the client is sending too many. When that layer is configured well, nobody notices it. When it isn't, it causes some of the most confusing outages there are: 502s that only some users get, a cache that serves one user's page to another, a reload that drops connections, a rate limit that blocks the office NAT.

Linux Server Administration put an app behind Nginx and Caddy with TLS. This course goes deep on the four servers you are most likely to run in front of real traffic.

The four servers

Server First release Written in What it is best known for
Nginx 2004 C web server + reverse proxy + cache; the most widely deployed; huge ecosystem of examples
HAProxy 2001 C a pure load balancer (TCP and HTTP): ACLs, health checks, stick tables, a runtime API for live changes
Caddy 2015 Go automatic HTTPS by default, a short Caddyfile, a JSON config API
Traefik 2016 Go an edge router configured by providers (files, Docker labels, Kubernetes): routes appear as services start

They overlap heavily: all four proxy HTTP, balance load, terminate TLS and speak HTTP/2. The differences are in what each does best and how you change its configuration while it runs. The last lesson of the operations section puts them side by side.

A first session

Every server in this course runs inside the lesson's sandbox, from a configuration written in the session, and is tested with curl. Here all four put the same tiny Python app behind themselves, each on its own port:

bash · try it yourself
cat > app.py <<'EOF'
from http.server import BaseHTTPRequestHandler, HTTPServer
class H(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200); self.end_headers()
        self.wfile.write(f"app saw {self.path} via {self.headers.get('X-Proxy')}\n".encode())
    def log_message(self, *a): pass
HTTPServer(("127.0.0.1", 9000), H).serve_forever()
EOF
python3 app.py &
for i in $(seq 50); do curl -s 127.0.0.1:9000/ > /dev/null && break; sleep 0.1; done

cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http {
  access_log off;
  server {
    listen 127.0.0.1:8081;
    location / { proxy_pass http://127.0.0.1:9000; proxy_set_header X-Proxy nginx; }
  }
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr

cat > haproxy.cfg <<'EOF'
global
  pidfile haproxy.pid
defaults
  mode http
  timeout connect 2s
  timeout client 10s
  timeout server 10s
frontend web
  bind 127.0.0.1:8082
  http-request set-header X-Proxy haproxy
  default_backend app
backend app
  server app1 127.0.0.1:9000
EOF
haproxy -D -f haproxy.cfg

cat > Caddyfile <<'EOF'
{
	auto_https off
}
:8083 {
	reverse_proxy 127.0.0.1:9000 {
		header_up X-Proxy caddy
	}
}
EOF
caddy start --config Caddyfile > caddy.log 2>&1

cat > traefik.yml <<'EOF'
global: {checkNewVersion: false, sendAnonymousUsage: false}
entryPoints:
  web: {address: "127.0.0.1:8084"}
providers:
  file: {filename: routes.yml, watch: false}
EOF
cat > routes.yml <<'EOF'
http:
  routers:
    app: {rule: "PathPrefix(`/`)", service: app, middlewares: [tag]}
  middlewares:
    tag: {headers: {customRequestHeaders: {X-Proxy: traefik}}}
  services:
    app: {loadBalancer: {servers: [{url: "http://127.0.0.1:9000"}]}}
EOF
traefik --configFile=traefik.yml > traefik.log 2>&1 &
for i in $(seq 100); do curl -sf 127.0.0.1:8084/ > /dev/null && break; sleep 0.1; done

for port in 8081 8082 8083 8084; do echo "$port: $(curl -s 127.0.0.1:$port/hello)"; done
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null
kill $(cat haproxy.pid) %2 %1; caddy stop > /dev/null 2>&1
8081: app saw /hello via nginx
8082: app saw /hello via haproxy
8083: app saw /hello via caddy
8084: app saw /hello via traefik

Same app, four front doors. Each server had to be told to add the X-Proxy header in its own dialect: an Nginx directive, an HAProxy http-request rule, a Caddy header_up subdirective and a Traefik middleware. Learning those dialects, and what each server does with a request between accepting it and answering it, is what this course is about.

How the course is organised

Section You learn
Nginx: Request Routing how a request is matched to a server and a location, try_files, redirects, map
Nginx: Proxying & Performance load-balancing methods, timeouts and retries, caching, rate limits, compression, static files
Nginx: TLS & Protocols TLS settings, client certificates, HTTP/2 and HTTP/3, reloads that drop nothing
HAProxy frontends and backends, ACLs, health checks, stick tables, the runtime API, TLS
Caddy & Traefik the Caddyfile in depth, the internal CA and admin API; Traefik routers, services, middlewares
Operating Them access log formats, Prometheus metrics, choosing between the four

The Toolkit and Field Notes collect commands and habits; the Mini Project builds a load-balanced, cached, rate-limited front end with blue/green switching for linkly, the link shortener of the DevOps Capstone.

Protocol theory (TCP, HTTP versions, TLS, load balancing) stays in Computer Networking; certificates and ACME stay in Linux Server Administration. Here those ideas become configuration.

Test yourself

Which of the four servers is configured mainly through *providers* that discover routes from files, Docker or Kubernetes?

  1. Nginx
  2. HAProxy
  3. Caddy
  4. Traefik

In the first session, how did Traefik add the X-Proxy header?

  1. With a proxy_set_header directive
  2. With a headers middleware attached to the router
  3. With http-request set-header
  4. It can't add headers