Caddy as Reverse Proxy with HTTPS and a Canonical Host

Caddy in front of a container on a VPS: automatic TLS from Let's Encrypt, www redirected to the bare domain, the app port closed to the outside.

Article Created Updated Also in German: Caddy als Reverse Proxy mit HTTPS und kanonischem Host

Reviewed by adrian on

Once the website container (then an Astro app in Podman) answered on 127.0.0.1:4321, I still needed HTTPS and a single canonical hostname. I picked Caddy over nginx for one reason: its built-in ACME client means I never touch certbot timers or renewal cron jobs.

Install Caddy

Caddy isn't in the default Debian repositories, so I added Cloudsmith's stable channel:

sudo apt update
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list >/dev/null
sudo apt update
sudo apt install -y caddy

Reverse proxy and canonical host

Two DNS names pointed at the VPS, adrian-altner.de and www.adrian-altner.de. I wanted exactly one of them to be public, with the other answering with a 301. Caddy's site blocks do both jobs at once, TLS and the redirect, with no extra configuration:

# /etc/caddy/Caddyfile
www.adrian-altner.de {
  redir https://adrian-altner.de{uri} permanent
}

adrian-altner.de {
  encode zstd gzip
  reverse_proxy 127.0.0.1:4321
}

On first start, Caddy fetches Let's Encrypt certificates for both hostnames, serves adrian-altner.de through the reverse proxy and answers www.adrian-altner.de with a permanent redirect.

Validate and reload

sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl restart caddy
sudo systemctl enable caddy
sudo systemctl status caddy --no-pager

A formatting warning from validate is harmless. caddy fmt cleans it up in place:

sudo caddy fmt --overwrite /etc/caddy/Caddyfile

Verify the result

curl -I http://adrian-altner.de
curl -I https://adrian-altner.de
curl -I https://www.adrian-altner.de

What I was looking for:

  • HTTP redirects to HTTPS (Caddy does this on its own once TLS is in place).
  • www returns a 301 to the bare domain.
  • The certificate is trusted and issued by Let's Encrypt.

Close the app port

With Caddy as the only public entry point, there's no reason for port 4321 to be reachable from outside. Binding it to localhost in the compose file closes that door:

ports:
  - '127.0.0.1:4321:4321'

Now the app only listens on the loopback interface, and Caddy alone handles 80 and 443.

Takeaways

  • Bind the app container to 127.0.0.1 as soon as a proxy sits in front of it. Exposing it publicly gains nothing.
  • One canonical hostname, with the other redirecting, avoids duplicate content in search engines later.

Based on

  • Caddy for Astro + Podman (HTTPS and Canonical Host) (raw/2026/2026-03-03/caddy-for-astro-podman-https-and-canonical-host.md) [post]

More on this

Topic

  • Self-Hosting Running services on a server you control instead of using a provider's hosted version, with the upkeep that comes with it.

Basis for

  • Security Headers in Caddy A strict Content Security Policy, HSTS and no-referrer in a Caddy site block, checked with Webbkoll, securityheaders.com and the Mozilla Observatory.

Uses

  • Caddy Web server and reverse proxy that obtains and renews TLS certificates on its own, configured in a short Caddyfile.
  • Podman Container engine without a background daemon that runs rootless by default on Linux and uses the same commands on macOS and on a server.

Mentioned in

Mentioned

  • Astro Tool for building websites from content such as Markdown. Ships plain HTML by default and only loads JavaScript where it is needed.

Show in the graph view