Tutorials 14 min read

HTTPS Reverse Proxy with Caddy and Docker in 2026: Complete Tutorial (Automatic Let's Encrypt)

2026 tutorial: set up Caddy 2 with Docker Compose as an HTTPS reverse proxy. Caddyfile, automatic Let's Encrypt certificates, a shared Docker network, checks, security headers and HSTS you enable yourself, and troubleshooting for ACME and 502 errors.

S By Selfhostr · no sponsored content
HTTPS Reverse Proxy with Caddy and Docker in 2026: Complete Tutorial (Automatic Let's Encrypt)
This article may contain affiliate links (no extra cost to you, it funds the site). As an Amazon Associate, Selfhostr earns from qualifying purchases. See the disclosure.

👍 What we like

  • Automatic HTTPS with HTTP-to-HTTPS redirect, no certbot or cron job
  • Short, readable configuration that reloads without downtime
  • Host and X-Forwarded-* headers passed by default, WebSockets and HTTP/3 supported
  • Free software with a maintained official Docker image

👎 What to watch

  • Ports 80/443 reachable from the Internet and correct DNS are required for Let's Encrypt
  • HSTS and security headers must be added yourself, site by site
  • The DNS challenge (blocked ports, wildcard) needs an image built with an extra module
  • Ports published by Docker bypass ufw, which requires care
📑 Contents ▾

Caddy is a web server written in Go that obtains and renews its own TLS certificates (from Let’s Encrypt, with ZeroSSL as a fallback) as soon as a domain name appears in its configuration, and redirects HTTP to HTTPS. Placed in front of your Docker containers, it publishes each service on its own subdomain over HTTPS with a three-line block per site, without certbot or a cron job. This tutorial installs it with Docker Compose on a Linux server, publishes two demo services, then shows how to check, secure and troubleshoot the setup.

One clarification before you start: what Caddy automates is HTTPS (certificates, renewal, redirect, recent TLS protocols). Security headers such as HSTS are not added by default. You enable them yourself, site by site, in step 8. The whole tutorial uses current Caddy 2 syntax.

Prerequisites

  • A 64-bit Linux server: a VPS, a mini PC or a VM. Docker’s documentation currently supports Ubuntu 22.04 LTS, 24.04 LTS, 26.04 LTS and 26.10; Debian 12 works too. Derivatives such as Linux Mint are not officially supported. On a fresh VPS, start by installing and securing an Ubuntu VPS. Choosing the machine is covered at the end of the page.
  • A domain name whose DNS you manage, with one record per subdomain (see step 2). The examples use exemple.fr.
  • Ports 80/TCP and 443/TCP reachable from the Internet, forwarded to the server if you are behind a home router. Add 443/UDP for HTTP/3. Port 80 is used for the ACME HTTP challenge and the redirect to HTTPS, port 443 for the TLS-ALPN challenge and traffic.
  • An email address for the ACME account. It is optional but recommended.

Step 1: Install Docker Engine and the Compose Plugin

For a server that will stay in production, Docker’s documentation recommends the official apt repository (Ubuntu’s own packages are often behind). The commands below target Ubuntu; on Debian 12, replace ubuntu with debian in both download.docker.com URLs:

sudo apt update
sudo apt install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

For a test machine, the official get.docker.com script is faster. Docker reserves it for testing and quick installs; the --dry-run option shows what it will do without installing anything:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh --dry-run
sudo sh get-docker.sh

Both methods install the docker compose plugin (Compose v2, command with a space), not the old docker-compose binary. Then add your user to the docker group, and log out and back in:

sudo usermod -aG docker $USER

Check: the first two commands print a version, the third prints “Hello from Docker!”.

docker --version
docker compose version
docker run --rm hello-world

Step 2: Declare the Subdomains in DNS

Let’s Encrypt only issues a certificate if the domain points to your server. At your DNS provider, create one A record per subdomain (and AAAA if you have IPv6), before Caddy starts for the first time:

TypeNameValue
Aapp1.exemple.fr203.0.113.10
Aapp2.exemple.fr203.0.113.10

Propagation often takes a few minutes, sometimes up to an hour. Check from your computer:

dig +short app1.exemple.fr A
dig +short app1.exemple.fr AAAA

The first command must return the server’s public IP. A stale AAAA record pointing elsewhere is enough to make validation fail.

Step 3: Create the Directory Layout and the Shared Network

Caddy goes under /opt/caddy and the applications under /opt/apps. The Caddyfile will live in a conf/ subfolder: the image documentation recommends mounting the folder rather than the file alone, because many editors replace the file when saving, and the container would keep seeing the old version.

An external Docker network, proxy, links Caddy to the containers of other Compose projects. Caddy will reach them by name (whoami:80), resolved by Docker’s internal DNS, not by an IP that changes.

sudo mkdir -p /opt/caddy/conf /opt/apps/demo
sudo chown -R $USER:$USER /opt/caddy /opt/apps
docker network create proxy

Check: the proxy network appears in the list.

docker network ls --filter name=proxy

Step 4: Start the Services to Publish

Caddy needs targets that actually exist, otherwise it returns 502 errors. To validate the chain end to end, start two small demo containers, traefik/whoami (which prints the headers it receives) and nginx, attached to the proxy network. Create /opt/apps/demo/compose.yaml:

services:
  whoami:
    image: traefik/whoami
    container_name: whoami
    restart: unless-stopped
    networks:
      - proxy

  nginx-demo:
    image: nginx:alpine
    container_name: nginx-demo
    restart: unless-stopped
    networks:
      - proxy

networks:
  proxy:
    external: true
cd /opt/apps/demo
docker compose up -d
docker compose ps

Both containers must be running. Note that there is no ports: section: that is the whole point of a reverse proxy. These services are never exposed directly to the Internet; only Caddy can reach them over the proxy network.

For a real application, the principle is the same: add networks: [proxy] to the web service, declare the proxy network as external: true, and note the container name and its internal port. That pair is what goes behind reverse_proxy. For Nextcloud, see our tutorial on hosting Nextcloud on a VPS.

Step 5: Write the Caddyfile

Create /opt/caddy/conf/Caddyfile. The unnamed block at the top holds global options (here the ACME email). After that, each site has its own block, starting with the domain name:

{
	email you@exemple.fr
}

app1.exemple.fr {
	reverse_proxy whoami:80
}

app2.exemple.fr {
	reverse_proxy nginx-demo:80
}

That file is enough:

  • Every block that starts with a domain name triggers a Let’s Encrypt certificate and the HTTP-to-HTTPS redirect.
  • reverse_proxy whoami:80 forwards traffic to the whoami container on its internal port 80. The directive keeps the original Host header, adds X-Forwarded-For, X-Forwarded-Proto and X-Forwarded-Host, and handles WebSockets: no headers to set by hand.
  • Do not put http:// in front of a domain name: that turns off automatic HTTPS for that site (no certificate, no redirect). https:// is unnecessary.

While you set things up, if your ports or DNS are not ready yet, repeated failures can get you blocked by Let’s Encrypt for a while because of its rate limits. Add acme_ca https://acme-staging-v02.api.letsencrypt.org/directory to the global block. Browsers don’t trust staging certificates: remove the line once everything works, then reload Caddy.

Step 6: Caddy’s Compose File

Create /opt/caddy/compose.yaml. The official image advises against the latest tag in production, so we pin the 2.11 branch (check the current tag on Docker Hub).

services:
  caddy:
    image: caddy:2.11
    container_name: caddy
    restart: unless-stopped
    cap_add:
      - NET_ADMIN
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./conf:/etc/caddy
      - caddy_data:/data
      - caddy_config:/config
    networks:
      - proxy

networks:
  proxy:
    external: true

volumes:
  caddy_data:
  caddy_config:

What matters here:

  • The caddy_data volume, mounted on /data, holds certificates, keys and the ACME account. It must persist: without it, Caddy would request all its certificates again every time the container is recreated and would quickly hit Let’s Encrypt’s limits (5 duplicate certificates per week).
  • The proxy network is declared external: true because we created it in step 3.
  • Port 443/udp enables HTTP/3. cap_add: NET_ADMIN is optional: it only lets the QUIC stack enlarge its UDP buffers.
  • Caddy’s admin API (port 2019) only listens on localhost inside the container. Never publish that port.

Step 7: Validate the Configuration and Start

cd /opt/caddy
docker run --rm -v "$PWD/conf:/etc/caddy:ro" caddy:2.11 \
  caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile
docker compose up -d
docker compose logs -f caddy

validate must end with “Valid configuration”. The logs should then show certificate obtained successfully for each domain.

Step 8: Add Security Headers (Optional)

Caddy does not send security headers on its own. If you want them, a reusable snippet saves copying them into every block. Here is a cautious baseline to put in the Caddyfile:

(security) {
	header {
		X-Content-Type-Options "nosniff"
		X-Frame-Options "SAMEORIGIN"
		Referrer-Policy "strict-origin-when-cross-origin"
		-Server
	}
}

app1.exemple.fr {
	import security
	# HSTS: only once you are sure you will keep HTTPS on this domain
	header Strict-Transport-Security "max-age=31536000"
	encode zstd gzip
	reverse_proxy whoami:80
}

Three precautions about HSTS:

  • Browsers remember the header for the whole max-age: from then on they refuse any HTTP connection to that domain. Only enable it on a site whose HTTPS is stable.
  • Only add includeSubDomains if all subdomains of the domain are served over HTTPS, including those that do not go through this server.
  • Do not add preload by default. That keyword is used to request inclusion of the domain in the list built into browsers, and getting out of it takes time. It is a deliberate step, not a setting to copy and paste.

Some applications already send their own headers: after enabling these, check that responses carry no duplicates (curl -sI https://app1.exemple.fr). The encode zstd gzip line compresses responses, which is unrelated to security.

Step 9: Reload After a Change

After every Caddyfile change, reload without stopping the service. Existing certificates are kept:

cd /opt/caddy
docker compose exec -w /etc/caddy caddy caddy reload

If the new configuration is invalid, the command prints the error and Caddy keeps running with the previous one.

Check From the Outside

Test from a machine outside your network, for example a phone on 4G/5G or a remote server:

curl -I http://app1.exemple.fr          # expected: 308 Permanent Redirect to https://
curl -I https://app1.exemple.fr         # expected: HTTP/2 200
curl -s https://app1.exemple.fr | grep -i x-forwarded   # whoami prints the headers it received
echo | openssl s_client -connect app1.exemple.fr:443 -servername app1.exemple.fr 2>/dev/null \
  | openssl x509 -noout -issuer -dates

The openssl output must show a Let’s Encrypt (or ZeroSSL) issuer and current validity dates. The whoami page must show X-Forwarded-Proto: https: proof that the request went through Caddy.

For a fuller audit of the TLS configuration, test the domain on SSL Labs. An A+ grade requires the HSTS header from step 8: without it, not getting A+ is expected.

Security: What Must Stay Closed

  • Expose only 80 and 443. No SSH, Samba/SMB, Docker socket or admin interface (DSM, Portainer, Caddy API on 2019) on the Internet. For remote administration, go through a VPN such as Tailscale (see our Tailscale tutorial).
  • Watch the firewall. According to Docker’s documentation, ports published by a container bypass ufw/firewalld rules. So publish no ports on your applications, and never expose a service both directly (via ports:) and behind Caddy: that creates an unencrypted access path. Keep ufw for host services:
sudo ufw allow OpenSSH        # before enabling, so you don't lock yourself out
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 443/udp
sudo ufw enable
sudo ufw status verbose
  • Protect applications that have no authentication with the basic_auth directive (generate the hash with docker compose exec caddy caddy hash-password), an SSO provider such as Authelia, or keep them on the local network.
  • Updates. From time to time, run docker compose pull && docker compose up -d in /opt/caddy after reading the release notes. Add the caddy_data volume to your backups.
  • Expiry alerts. Caddy does not send emails, and Let’s Encrypt stopped its expiry emails in 2025. To be warned about a failed renewal, set up external monitoring (an HTTPS probe that checks the expiry date).

Advanced Options

  • DNS challenge. For a wildcard certificate (*.exemple.fr), or if your server cannot be reached on port 80 or 443, use the DNS challenge. It needs a Caddy image built with your DNS provider’s module (Cloudflare, OVH…) and an API token (see the Caddy documentation).
  • Structured logs. The log directive writes JSON logs that a monitoring stack can use.
  • Local-only services. Names such as nas.home.arpa, *.internal or localhost get a certificate from Caddy’s local authority, not from Let’s Encrypt. You then need to install that authority’s root (stored in /data/caddy/pki/authorities/local/) on your devices. You can also force this behavior with tls internal in a site block.
  • Behind Cloudflare. Choose “Full (strict)” mode so that traffic between Cloudflare and your server stays encrypted with Caddy’s certificate. With the Cloudflare proxy enabled, the HTTP challenge may fail; switch to the Cloudflare DNS challenge in that case.

Troubleshooting Common Errors

  • “timeout during connect” or “connection refused” in the ACME logs. Port 80 or 443 does not reach the server (router, ISP, firewall, CGNAT). Check port forwarding and sudo ufw status, then test from outside. If your ISP blocks these ports, use the DNS challenge.
  • “NXDOMAIN” or wrong IP. DNS does not point to you yet, or a stale AAAA record lingers. Both dig commands from step 2 must return your public addresses.
  • 502 Bad Gateway. The target container is not on the proxy network, its name is different or its port is wrong. docker network inspect proxy must list the container, and the port in reverse_proxy must be its internal port, not a published one.
  • Caddyfile changes are not picked up. A forgotten reload, or the file mounted alone instead of the folder. Mount ./conf:/etc/caddy, then run the step 9 command.
  • “address already in use” at startup. Another web server (Apache, nginx, DSM) already holds 80/443. sudo ss -tlnp | grep -E ':80|:443' identifies it; stop it or change its ports.
  • Let’s Encrypt rate-limit error. Too many failed attempts. Switch to the staging URL (step 5), fix the issue, wait for the block to expire, then go back to production.

FAQ

Why choose Caddy over Nginx or Traefik?

For the simplicity of automatic HTTPS: Caddy obtains, installs and renews certificates with no ACME configuration or cron, whereas Nginx needs certbot and longer configuration blocks. Traefik is very good at discovering containers automatically through Docker labels, at the cost of a more abstract configuration. The details are in our comparison Traefik vs Nginx Proxy Manager vs Caddy.

Do I need to configure X-Forwarded-* or Host headers?

No. reverse_proxy keeps the original Host header and fills in X-Forwarded-For, X-Forwarded-Proto and X-Forwarded-Host. Some applications still need to be told to trust the proxy (for example trusted_proxies in Nextcloud’s configuration): check their documentation.

Are my certificates renewed automatically?

Yes. Caddy renews in the background, well before expiry (for a 90-day certificate, about 30 days before the end date). If renewal fails, it retries at increasing intervals. The only condition is that the caddy_data volume persists. To check, run docker compose logs caddy | grep -i renew and rerun the openssl command above from time to time to read the notAfter date.

How long are certificates valid?

Let’s Encrypt currently issues 90-day certificates by default. It has announced a gradual reduction: 64 days for the default profile from February 2027, then 45 days in February 2028. With Caddy, there is nothing to change; renewal follows automatically.

Can I use certbot to manage these certificates?

No. Caddy manages its own certificates in /data. certbot does not see them and must not touch them.

Does Caddy use a lot of resources?

No. It is a single binary, shipped in a lightweight Alpine image, and it suits a small entry-level VPS for moderate traffic. The applications behind it are what size the machine.

Which Machine to Run It On: VPS, Mini PC or NAS

Caddy needs few resources; the choice of machine depends mostly on where you want your services to run and how easily you can open ports 80 and 443.

  • A VPS is the simplest way to expose services to the Internet: a fixed public IP, open ports, no home router to configure. If you don’t want to manage hardware or maintenance, a managed VPS is ready in minutes: See PlanetHoster plans.
  • A low-power mini PC is enough at home in most cases, provided you forward 80 and 443 from your router (and are not behind CGNAT). Two common picks: the Beelink Mini S12 Pro (Intel N100, 16 GB) and the Beelink EQ14 (Intel N150, dual 2.5 GbE ports). To choose for your needs: our guide to the best mini PCs for a homelab.
  • A Synology NAS such as the Synology DS920+ can run containers with Container Manager, but DSM already uses ports 80 and 443 itself. To put Caddy on it, you have to publish other ports and adjust your router’s forwarding: check the procedure in the documentation for your DSM version.
  • Docker Desktop is fine for practicing locally, but it is not a platform for a proxy exposed 24/7: it won’t get a public certificate without port forwarding to that computer. For learning resources, you can also search for “Docker Desktop” on Amazon.

Once the reverse proxy is in place, the logical next step is to back up this server, caddy_data volume included: see our tutorial on restic and the 3-2-1 rule.

Key facts

80/TCP and 443/TCP (+ 443/UDP for HTTP/3)
Ports to open
Automatic, no certbot or cron
Renewal
caddy:2.11 (official image)
Docker image
€0 (free software)
Software cost

Our picks

Affiliate links · same price for you
Intel N100 mini PC
Beelink Mini S12 Pro

Beelink Mini S12 Pro

View on Amazon

Amazon · price and availability on the listing

Intel N150, dual 2.5 GbE
Beelink EQ14

Beelink EQ14

View on Amazon

Amazon · price and availability on the listing

NAS with Container Manager
Synology DS920+

Synology DS920+

View on Amazon

Amazon · price and availability on the listing

See all our recommended gear in the shop.

Tags: CaddyDockerLet's EncryptReverse ProxyHTTPSDevOps

Related

Tutorials

Deploy Immich with Docker in 2026: Complete Guide (HTTPS, Reverse Proxy, Backups)

Step-by-step 2026 tutorial to self-host Immich, the Google Photos alternative, using Docker Compose. Features include photo/video gallery, facial recognition, automatic HTTPS via Caddy and Let's Encrypt, backups, and hardening. Ready-to-use configs included.

Read
Tutorials

Deploy Jellyfin with Docker in 2026: Complete Guide (Hardware Transcoding, HTTPS, Reverse Proxy)

Step-by-step 2026 tutorial to self-host Jellyfin, the open-source media server, using Docker Compose. Covers film/series libraries, Intel/NVIDIA hardware transcoding, automatic HTTPS via Caddy and Let's Encrypt, remote access, and hardening. Ready-to-use configs included.

Read
Tutorials

Self-Hosting Your Website in 2026: Complete Guide (VPS, Docker, HTTPS)

2026 technical guide to self-hosting on a VPS: choosing plans, Docker setup, Let's Encrypt HTTPS, security, and budget. Compare self-hosting vs. cloud.

Read