Skip to content

Caddy as a reverse proxy in production

Setting up Caddy as a reverse proxy for upstream applications — health checks, headers, transport, retries, and the production knobs an MSP tunes before launch.

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 — http or https based 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 with header_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

  1. 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.

  2. 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 from trusted_proxies or from X-Forwarded-For chains).

  3. 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_uri is the active health check endpoint. health_interval is how often Caddy hits it. health_status is what counts as healthy. fail_duration is how long a passive failure (real request returning 5xx) keeps the upstream out of rotation. max_fails is the consecutive failure count before active eviction.

  4. 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. The tls_server_name is what Caddy sends in SNI to the upstream — usually the upstream’s certificate Subject CN.

  5. 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
        }
    }
  6. 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_duration is the total time Caddy keeps trying upstreams. lb_try_interval is the gap between attempts. Set both to zero (the default) to fail fast on the first error.

Common pitfalls

  • The default Host header 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 you header_up Host {host}.
  • X-Forwarded-For accumulates — if Caddy is behind a CDN, the existing X-Forwarded-For is the CDN’s chain plus the client. Trust the leftmost only if you control every hop; otherwise pin which proxy is trusted with trusted_proxies.
  • An upstream that responds with 5xx to a real request counts as a passive health failure even if the active health check is passing. Tune fail_duration and max_fails so a flaky upstream does not flap.
  • WebSocket upgrade requests need no special config in Caddy v2 — reverse_proxy handles Upgrade: websocket natively. See caddy websocket proxy for the latency and timeout knobs.
  • transport http defaults to HTTP/1.1; if the upstream is HTTP/2 cleartext (h2c), you must say so explicitly with versions 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.