Caddy in front of an application server is the most common Caddy deployment shape in the field. This article is the practical setup we run for clients: a Caddyfile that terminates TLS, forwards to an upstream over HTTP or HTTPS, sets the X-Forwarded headers correctly, runs health checks, and behaves under upstream failure. The audience is anyone replacing Nginx + Let’s Encrypt scaffolding with something operationally simpler.
How to verify
A reverse proxy is “working” when these all behave correctly:
curl -I https://app.example.com/
curl -H 'X-Debug: 1' -v https://app.example.com/health
sudo journalctl -u caddy --since '5m ago' | grep -i 'upstream\|status'
ss -tnp 'sport = :3000'
The TLS handshake completes, the upstream sees the right X-Forwarded-For, the application returns the body Caddy proxies through, and the ss shows Caddy’s outbound socket to the upstream port. If any one of those fails, you have a misconfig or an upstream that did not start.
What’s happening
The reverse_proxy directive is Caddy’s HTTP forwarder. It accepts one or more upstream addresses, dials them on every request, opens a connection from the pool, copies the request, awaits the response, and streams it back to the client. Connections are pooled per upstream (default 0 — unlimited; tune with transport), and Caddy automatically sets:
X-Forwarded-For— appends the client IP to any existing header.X-Forwarded-Proto—httporhttpsbased on the listener.X-Forwarded-Host— the original Host header value.Host— the upstream’s host:port (so upstream Host-based routing works against the upstream’s name, not the client’s). Override withheader_up Host {host}to pass through.
The default transport is http, plaintext HTTP/1.1. If the upstream is HTTPS, you say so in the upstream address (https://upstream:443), and Caddy negotiates TLS for the upstream side independently of the client side. If the upstream uses a self-signed cert, you tell Caddy that explicitly in the transport http block — it will not blindly trust anything.
Failures: Caddy treats a connection error or a 5xx from the upstream as an “unhealthy” event. By default the request fails to the client. With multiple upstreams configured, Caddy distributes load (random by default, configurable to round-robin, least-conn, IP-hash, header-hash, URI-hash, cookie-based, or weighted). See caddy load balancing for the full picker semantics.
The procedure
-
The minimal reverse proxy. Single upstream, HTTPS-terminated, no extras:
app.example.com { reverse_proxy localhost:3000 }That is the entire config for HTTPS-with-Let’s-Encrypt + reverse proxy + auto redirect. Restart Caddy, the cert appears, requests forward.
-
Pass the original Host header. Most apps want to know the client’s hostname for canonical URL generation:
app.example.com { reverse_proxy localhost:3000 { header_up Host {host} header_up X-Real-IP {remote_host} } }{host}is the request Host.{remote_host}is the direct peer IP (Caddy’s view of the client — the actual originating IP fromtrusted_proxiesor fromX-Forwarded-Forchains). -
Multiple upstreams with health checks. This is where reverse proxy turns into a load balancer:
app.example.com { reverse_proxy 10.0.1.10:3000 10.0.1.11:3000 10.0.1.12:3000 { lb_policy round_robin health_uri /health health_interval 10s health_timeout 2s health_status 200 fail_duration 30s max_fails 3 } }health_uriis the active health check endpoint.health_intervalis how often Caddy hits it.health_statusis what counts as healthy.fail_durationis how long a passive failure (real request returning 5xx) keeps the upstream out of rotation.max_failsis the consecutive failure count before active eviction. -
HTTPS upstream with a private CA. When the upstream is HTTPS and your CA is private (a service mesh, an internal PKI):
app.example.com { reverse_proxy https://upstream-internal.svc.cluster.local:8443 { transport http { tls tls_trusted_ca_certs /etc/caddy/internal-ca.crt tls_server_name upstream-internal.svc.cluster.local } } }Without
tls_trusted_ca_certs, Caddy uses the system trust store. Thetls_server_nameis what Caddy sends in SNI to the upstream — usually the upstream’s certificate Subject CN. -
Buffering and streaming. By default Caddy streams the upstream response straight to the client. For long-poll or SSE endpoints, that is what you want. For an upstream that returns slowly chunked responses to a slow client, the upstream sees the slow client’s TCP backpressure. Buffer the response in Caddy to release the upstream sooner:
app.example.com { reverse_proxy localhost:3000 { buffer_responses max_buffer_size 4MB } } -
Retry policy. A first attempt failure can re-try on a different upstream:
app.example.com { reverse_proxy 10.0.1.10:3000 10.0.1.11:3000 { lb_try_duration 5s lb_try_interval 250ms } }lb_try_durationis the total time Caddy keeps trying upstreams.lb_try_intervalis the gap between attempts. Set both to zero (the default) to fail fast on the first error.
Common pitfalls
- The default
Hostheader sent upstream is the upstream’s IP:port, not the client’s hostname. Apps that generate links based on Host will produce broken URLs unless youheader_up Host {host}. X-Forwarded-Foraccumulates — if Caddy is behind a CDN, the existingX-Forwarded-Foris the CDN’s chain plus the client. Trust the leftmost only if you control every hop; otherwise pin which proxy is trusted withtrusted_proxies.- An upstream that responds with
5xxto a real request counts as a passive health failure even if the active health check is passing. Tunefail_durationandmax_failsso a flaky upstream does not flap. - WebSocket upgrade requests need no special config in Caddy v2 —
reverse_proxyhandlesUpgrade: websocketnatively. See caddy websocket proxy for the latency and timeout knobs. transport httpdefaults to HTTP/1.1; if the upstream is HTTP/2 cleartext (h2c), you must say so explicitly withversions h2c 2, otherwise Caddy refuses to speak h2c.
Stack Harbor runs Caddy reverse proxies as the standard edge for stateless application fleets at clients — health checks under monitoring, upstream rotation auditable, and TLS state backed up off-box. If you are about to put Caddy in front of an application that matters and want the production knobs set sanely from day one, this is the kind of work covered by our Managed Operations practice.