Lesson 4 of 9 · 25m
Reverse Proxying Like You Mean It
Upstreams, headers, path handling, health checks, and load-balancing details that decide whether the proxy is invisible or harmful.
A reverse proxy looks easy because the smallest example is one line. Production reverse proxying is not hard because of the syntax — it is hard because the proxy changes identity, timeouts, paths, trust boundaries, and failure behavior. Caddy gives you clean primitives, but you still have to choose what the application is allowed to believe.
Start with the actual contract
app.sudosecurity.org {
reverse_proxy 127.0.0.1:8080
}This sends requests for app.sudosecurity.org to a local HTTP service. Caddy adds forwarding information such as X-Forwarded-For and X-Forwarded-Proto, and by default preserves the incoming Host header. That last point surprises nginx migrants: if the backend virtual-hosts by name, preserving the original host is often correct; if the backend expects its service name, set it deliberately:
reverse_proxy 10.0.4.18:8080 {
header_up Host {upstream_hostport}
}Do not copy that blindly. Host handling is application policy, not tradition.
Paths are part of the API
handle_path /api/* {
reverse_proxy 127.0.0.1:8080
}handle_path strips /api before proxying. handle does not. If /api/login should reach the app as /login, use handle_path. If the application deliberately mounted its router at /api, use handle or no matcher at all. Half of "the proxy broke the app" incidents are just this mismatch.
Health checks and multiple upstreams
reverse_proxy 10.0.4.11:8080 10.0.4.12:8080 {
lb_policy least_conn
health_uri /healthz
health_interval 10s
health_timeout 2s
health_status 2xx
}Active health checks stop routing traffic to instances that are up at TCP level but dead at application level. Make /healthz cheap and unauthenticated, but do not make it so shallow it never tests the dependency that matters. A health endpoint that returns 200 while every request fails is worse than none because it gives false confidence.
Passive failover also exists: Caddy remembers upstream failures for a limited duration and tries alternatives. That helps with transient faults, but it is not a substitute for health-aware removal.
Know who the client is
Caddy appends forwarding headers, but trusting them is the application's job. If clients can reach the backend directly, they can forge X-Forwarded-For and bypass IP logic. The correct architecture is simple: only the edge can reach the app, or the app only trusts forwarding headers from known proxy networks. Otherwise rate limits, audit logs, and security alerts record proxy addresses — or attacker-chosen fiction.
Field notes
- If redirects point to
httpor the wrong host, the app probably does not understand forwarded proto/host; fix its trusted-proxy settings before rewriting everything at the edge. - WebSockets generally work through Caddy's reverse proxy without special incantations. Long-lived connection timeouts are the part to review.
- Do not use round-robin load balancing as a substitute for graceful shutdown. Drain connections before killing app instances.
- Test failure explicitly: stop one upstream and watch status codes, latency, and the health decision. Untested failover is decorative.
Was this lesson helpful?