Subscribe

Automatic HTTPS and www redirects for a Ghost blog using Caddy

My old Ghost server had Nginx, three acme.sh certificates and a root cron job. The new one has a Caddyfile of about ten lines that gets the certificates, renews them and redirects www and blog to the apex. Here is the file and how I checked it.

Automatic HTTPS and www redirects for a Ghost blog using Caddy

On the old server this blog had Nginx in front of Ghost and three separate acme.sh certificates, one each for fenixara.com, www.fenixara.com and blog.fenixara.com. A root cron job renewed them. It worked for years and I never once looked at it. When I checked this month the earliest one expired on 2026-12-10, which was one more reason to finish the move to the new server quickly.

The new server runs Caddy instead (the bigger picture is in the post on migrating a Ghost-CLI blog to Docker Compose). Caddy gets and renews certificates by itself. In this blog we will look at the Caddyfile, the Compose bits it needs and how I checked the certificates and redirects after the cutover.

The Caddyfile

This is the whole thing, minus one ads.txt block that has its own post.

# Caddy obtains and renews Let's Encrypt certificates automatically.
fenixara.com {
    encode zstd gzip
    reverse_proxy ghost:2368
}

# Canonicalise alternate hostnames onto the apex (path + query preserved).
www.fenixara.com, blog.fenixara.com {
    redir https://fenixara.com{uri} permanent
}

That replaced an Nginx site config, acme.sh and a cron entry.

The first block serves the apex. Because the site address is a real domain name, Caddy turns on automatic HTTPS for it: it gets a certificate from Let's Encrypt, renews it before expiry and redirects plain HTTP to HTTPS. reverse_proxy ghost:2368 sends everything to the Ghost container by its Compose service name. Caddy also sets X-Forwarded-For and X-Forwarded-Proto on the way, so Ghost knows the request came in over HTTPS and doesn't get into a redirect loop.

The second block covers the other two names. They get their own certificates too, they have to, otherwise a browser going to https://www.fenixara.com would see a certificate error before it ever saw the redirect. Then redir sends a 301 to the apex. {uri} is the path plus the query string, so an old link like https://blog.fenixara.com/some-post/?ref=x lands on the same post on the apex.

I picked the apex as the one real hostname because Ghost's url was already https://fenixara.com. One canonical URL is better for search too, the alternate names were only ever there because of old links.

The Compose side

Three things in the Caddy service matter.

services:
  caddy:
    image: caddy:2.10-alpine
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - ./data/caddy/data:/data
      - ./data/caddy/config:/config
    depends_on:
      - ghost

Port 80 has to be open to the internet, both in Compose and in the security group. Let's Encrypt uses it for the HTTP challenge, and it's where the HTTP to HTTPS redirect happens. A common mistake is closing 80 because it's "the insecure port", and then renewals start failing a few months later. 443/udp is for HTTP/3, which Caddy serves by default.

The /data mount is where the certificates and the ACME account live. Keep it on disk and back it up. If you lose it, every rebuild asks Let's Encrypt for new certificates, and Let's Encrypt only lets you get five certificates for the exact same set of names per week. Five rebuilds in an evening while debugging something else, and you are locked out for days. My data/caddy sits under the same folder as Ghost content and MySQL. The nightly S3 backup only takes the database and Ghost content, but the daily EBS snapshots cover the whole disk, Caddy data included.

Ghost itself has no published ports. Only Caddy can reach it.

Why not an ALB with ACM

I did look at it. A load balancer with an ACM certificate is managed by AWS and renews itself too. But it's about $18 a month extra for a blog that runs on one small instance. Also the DNS for this domain is at GoDaddy, not Route 53, so ACM's DNS validation means adding records by hand there. For one instance, Caddy on the box was the obvious choice. If the blog ever needs two instances, I'll look again.

Cutover and the first certificates

Caddy can only pass the HTTP challenge once the domain points at it. My cutover was moving the Elastic IP from the old instance to the new one, so DNS never changed. The moment the IP moved, requests started arriving at Caddy, and it had all three certificates in about a minute. Much less moving parts than the old acme.sh setup.

You can watch it happen in the logs.

docker compose logs -f caddy | grep -i -E "certificate obtained|error"

And the files show up under the data mount.

ls data/caddy/data/caddy/certificates/acme-v02.api.letsencrypt.org-directory/
# blog.fenixara.com  fenixara.com  www.fenixara.com

The plan had a rehearsal step with test certificates first. I ended up not doing it and just went live, and it worked, but I wouldn't rely on that next time. For a rehearsal you can tell Caddy to use its own local CA, and test from a laptop with an /etc/hosts entry pointing the domain at the new server.

{
    local_certs
}

Put that global block at the top of the Caddyfile, and remove it before the real cutover. Your browser will complain about the certificate, which is expected. The other option is Let's Encrypt's staging CA through the acme_ca global option, which has much higher limits.

Checking the redirects

After cutover I checked every hostname with curl. Plain HTTP on the apex first.

curl -sI http://fenixara.com/ | grep -i -E "^HTTP|^location"
# HTTP/1.1 308 Permanent Redirect
# Location: https://fenixara.com/

Caddy's automatic HTTP to HTTPS redirect is a 308, not a 301. That's fine, it's also permanent and it keeps the method.

Then the alternate names, with a path and a query string to make sure {uri} carries both.

for h in www.fenixara.com blog.fenixara.com; do
  curl -sI "https://$h/about/?ref=test" | grep -i -E "^HTTP|^location"
done
# HTTP/2 301
# location: https://fenixara.com/about/?ref=test
# HTTP/2 301
# location: https://fenixara.com/about/?ref=test

And the certificate dates.

for h in fenixara.com www.fenixara.com blog.fenixara.com; do
  echo | openssl s_client -connect "$h:443" -servername "$h" 2>/dev/null \
    | openssl x509 -noout -subject -enddate
done

All three said February 2027. Caddy will renew them well before that, without a cron job and without me remembering anything.