Zum Inhalt springen
TutorialsNetzwerkeFortgeschritten

How to Set Up Caddy as a Reverse Proxy

Set up Caddy in Docker for automatic HTTPS in front of your homelab: Caddyfile examples, shared Docker networks, DNS-challenge certificates for internal names, auth, headers and troubleshooting.

von HomeServersGuide TeamAktualisiert 3. November 20265 Min. Lesezeit

Why Caddy

Caddy is a modern web server whose killer feature is automatic HTTPS: it obtains and renews Let's Encrypt certificates for you with zero configuration. Combined with a remarkably readable config file, it's the fastest way to give every homelab service a clean hostname and a valid certificate.

By the end you'll route several internal services through Caddy with HTTPS, using either public DNS or a DNS challenge for fully internal names. If you are new to the concept, read about reverse proxies and the networking guide first.

Prerequisites

  • A domain name you control (even a cheap one).
  • DNS records you can edit.
  • Docker (recommended) or a Linux host.
  • Ports 80 and 443 free on the server.

Step 1: Run Caddy in Docker

Create a folder and a compose.yaml:

services:
  caddy:
    image: caddy:2
    container_name: caddy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"   # HTTP/3
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
      - caddy_config:/config
    networks:
      - proxy
 
networks:
  proxy:
    name: proxy
 
volumes:
  caddy_data:
  caddy_config:

The caddy_data volume is important — it stores your certificates so they aren't re-requested on every restart (which could hit Let's Encrypt rate limits). Back it up. The proxy network lets Caddy reach other containers by name, as described in Docker networking explained.

Step 2: Write the Caddyfile

Create a file named Caddyfile:

jellyfin.example.com {
    reverse_proxy jellyfin:8096
}
 
nextcloud.example.com {
    reverse_proxy 192.168.1.10:8080
}

The first example uses a container name (the Jellyfin container must join the proxy network and listen on port 8096 inside it). The second forwards to an address on your network. That's genuinely all it takes. Caddy will:

  1. Request a certificate for each hostname.
  2. Redirect HTTP to HTTPS automatically.
  3. Renew certificates before they expire.

Point the subdomains' DNS A records at your public IP (and forward ports 80/443 to the server), then:

docker compose up -d
docker compose logs -f caddy

The log shows certificate issuance. After editing the Caddyfile later, validate and reload without downtime:

docker exec caddy caddy validate --config /etc/caddy/Caddyfile
docker exec caddy caddy reload --config /etc/caddy/Caddyfile

Step 3: Internal-only services with a DNS challenge

If you don't want to expose ports 80/443 to the internet, use a DNS challenge so Caddy can prove domain ownership through your DNS provider instead. This lets you get valid certificates for purely internal hostnames.

The standard Caddy image does not include DNS provider plugins. Build an image with your provider's module (for example Cloudflare) using xcaddy, in a small Dockerfile:

FROM caddy:2-builder AS builder
RUN xcaddy build --with github.com/caddy-dns/cloudflare
 
FROM caddy:2
COPY --from=builder /usr/bin/caddy /usr/bin/caddy

Then use the image in Compose (build: .), pass the token as an environment variable, and reference it in the Caddyfile:

{
    acme_dns cloudflare {env.CF_API_TOKEN}
}
 
home.example.com {
    reverse_proxy 192.168.1.10:3000
}

Create the Cloudflare token with permission to edit DNS records for just your zone, and keep it in an .env file with restricted permissions. Now home.example.com resolves on your LAN (add a record to your local DNS such as Pi-hole pointing at the server's private IP), gets a real certificate, and is never exposed to the public internet.

For lab-only names you can also use tls internal, which makes Caddy issue certificates from its own private certificate authority. You would then need to install that root certificate on each device.

Step 4: Handy Caddy features

  • Basic auth — protect an app that has no login:
grafana.example.com {
    basic_auth {
        admin JDJhJDE0...   # generate with: caddy hash-password
    }
    reverse_proxy grafana:3000
}
  • Compression and headers — add encode gzip zstd and security headers in a reusable snippet:
(secure) {
    encode gzip zstd
    header {
        Strict-Transport-Security "max-age=31536000"
        X-Content-Type-Options "nosniff"
        Referrer-Policy "strict-origin-when-cross-origin"
    }
}
 
app.example.com {
    import secure
    reverse_proxy app:8080
}
  • Path routing — send /api/* to one service and everything else to another with handle blocks.
  • Multiple upstreams — Caddy can load-balance across several backends.
  • Wildcard certificates — one *.example.com site block with a DNS challenge and @name host matchers.
  • Access logs — add a log block to see requests and feed them to Fail2ban.

Caddy vs. Nginx Proxy Manager

Prefer Caddy if you like config-as-code, want the simplest possible TLS and want a file you can keep in version control. Prefer Nginx Proxy Manager if you want a point-and-click UI. Both are excellent; the choice is about workflow.

Security notes

  • Only expose ports 80 and 443; never publish your apps' own ports unnecessarily.
  • Put authentication in front of anything without its own login, and prefer a VPN for admin tools.
  • Keep the image updated and pinned to the major version.
  • Consider Fail2ban for exposed services.

Troubleshooting

  • Certificate not issued — check that ports 80/443 reach the server, that DNS points at it, or that your DNS-challenge token is valid. The logs explain which challenge failed.
  • 502 Bad Gateway — the upstream address or port is wrong, the container is not on the shared network, or the service is down.
  • Rate-limited by Let's Encrypt — make sure the caddy_data volume persists so certs are reused, and avoid repeated recreation while testing (use the staging CA while experimenting).
  • Works on LAN but not outside — check port forwarding, your ISP (CGNAT blocks inbound), and DNS.
  • Caddyfile errors — run caddy validate, and check that site blocks use tabs or spaces consistently and braces are matched.

Next steps

Put your dashboards, media server and cloud behind Caddy, then add Fail2ban and a VPN for defense in depth. Monitor the certificates and the proxy with Uptime Kuma.

Verwandte Artikel

TutorialsNetzwerkeAnfänger

How to Install Nginx Proxy Manager

Install Nginx Proxy Manager with Docker, add proxy hosts and free Let's Encrypt certificates, use DNS challenges for internal names, add access lists and avoid common mistakes.

5 Min. Lesezeit
TutorialsNetzwerkeFortgeschritten

How to Set Up WireGuard VPN

Self-host a WireGuard VPN to reach your homelab securely from anywhere: wg-easy in Docker, a manual wg-quick setup, split vs full tunnel, DNS, CGNAT and troubleshooting.

6 Min. Lesezeit
TutorialsNetzwerkeAnfänger

Pi-hole installieren

Blockiere Werbung und Tracker im ganzen Netzwerk, indem du Pi-hole Schritt für Schritt auf deinem Heimserver installierst.

5 Min. Lesezeit