Chapter 3 · Lesson 12 of 22
Domains and HTTPS

Automatic HTTPS with Caddy

Caddy is a web server that requests and renews HTTPS certificates automatically, so a reverse proxy needs only the domain and the app address.

By the end you will

Serve your app over HTTPS with a certificate Caddy gets and renews for you.

Time
14 min
Level
Beginner
Check
4 questions
You need
  • A domain whose A record points at your server
  • Ports 80 and 443 open and free
  • An app listening on 127.0.0.1:3000

You want HTTPS without learning certificate tooling. With Caddy, you write the domain name in the config and the certificate appears. This works well for a small server with one or a few apps, as long as you know what Caddy needs from the network to do it.

How it works#

When Caddy sees a site address that is a public domain name, such as example.com, it assumes you want HTTPS. At startup it contacts a certificate authority (Let’s Encrypt or ZeroSSL), proves you control the domain, stores the certificate and serves the site. It also renews the certificate before it expires, without a cron job or timer you manage.

The proof uses the same challenges you may know from certbot. By default Caddy tries the HTTP challenge on port 80 and the TLS-ALPN challenge on port 443, so both ports must be reachable from the internet. Caddy also redirects plain HTTP to HTTPS by itself.

Certificates and account keys are kept in Caddy’s data directory. Treat it like state: if it disappears, Caddy has to ask for everything again. The reverse_proxy line forwards each request to your app and sets the usual forwarding headers.

Your domain’s A record must point at the server, and ports 80 and 443 must be open, before Caddy can get a certificate. Nothing else (such as nginx) should be using those ports.

Step 1: Install Caddy#

Follow the Debian, Ubuntu and Raspbian instructions on the official install page at https://caddyserver.com/docs/install. They add Caddy’s apt repository and install the caddy package, which also sets up a systemd service.

What you should see: caddy version prints a version, and systemctl status caddy shows the service running.

Step 2: Write the Caddyfile#

Open /etc/caddy/Caddyfile with sudo and replace its contents:

output
example.com {
    reverse_proxy 127.0.0.1:3000
}

The first line is the site address. Writing a real domain there is what turns on automatic HTTPS. The block says to forward everything to the app.

Step 3: Check the file and reload Caddy#

bash
caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy

On first load Caddy asks for a certificate for example.com. This takes a few seconds. A reload applies the new config without dropping connections.

What you should see: validate reports the configuration is valid, and the reload prints nothing on success.

Step 4: Watch the issuance#

bash
sudo journalctl -u caddy --no-pager -n 50

What you should see: lines that mention obtaining a certificate for your domain and no repeating errors.

Check it worked#

bash
curl -I https://example.com

You should get a response from your app over HTTPS with no certificate error. Plain http:// requests are redirected to HTTPS by default, which you can test with curl -I http://example.com and look for a redirect status and a Location header.

Edge cases and gotchas#

  • Caddy and nginx fight over ports. Only one process can listen on 80 and 443. Stop and disable nginx first (sudo systemctl disable --now nginx).
  • A site address that is not a public name gets no public certificate. An IP address or localhost does not trigger Let’s Encrypt. Use a real domain.
  • DNS must point here first. If the A record still points at an old server, validation reaches that server and fails. Caddy retries, but fix DNS rather than waiting.
  • An AAAA record that points nowhere breaks validation. If you publish IPv6, the server must really answer on it. Otherwise remove the AAAA record.
  • Behind a CDN or proxy, the challenge may not reach Caddy. Point DNS straight at the server while issuing, or use a different challenge type that your setup supports.
  • In Docker, mount /data. Without a volume the certificates vanish with the container, and repeated requests can hit the authority’s rate limits. Mount /config as well to keep autosaved config.
  • Running Caddy as a non-root user. The packaged service has permission to bind low ports. A hand-run binary may not, and fails with a permission error on port 80 or 443.

Troubleshooting#

SymptomLikely causeFix
Browser says connection refused on 443Caddy not running or port blockedsystemctl status caddy, then open 443 in firewall
Logs show an ACME challenge failureDNS wrong or port 80 or 443 blockedFix the A record, open both ports, reload
listen tcp :80: bind: address already in usenginx or Apache holds the portStop that service, restart Caddy
502 Bad GatewayApp is not running on the proxied addressStart the app and check the port. See the 502 lesson
Reload fails with a parse errorTypo in the CaddyfileRun caddy validate and fix the line it names
Certificate re-requested after every restart in Docker/data is not on a volumeMount a named volume at /data
permission denied binding to port 80 or 443Caddy run by a user that cannot bind low portsUse the packaged systemd service
Browser warns about an untrusted certificate on a test domainCaddy made its own local certificate or a staging oneUse a real public domain, and see Caddy’s docs for trusting the local root

Real-world scenario#

You run a Compose project with two apps and want app.example.com and api.example.com on one server. You add a caddy service that publishes ports 80 and 443 and mounts a named volume at /data, with this Caddyfile:

output
app.example.com {
    reverse_proxy app:3000
}

api.example.com {
    reverse_proxy api:4000
}

Both A records point at the server. On docker compose up -d, Caddy’s logs show certificates obtained for both names within seconds. A week later you change the Compose file and run docker compose up -d again. Caddy is recreated, but it finds its certificates in /data and does not request new ones. If you had forgotten the volume, each recreate would be a new request, and a handful of those in a short time can hit the authority’s rate limits.

The app containers use the Compose service names app and api because Caddy reaches them over the shared Docker network. They publish no ports to the host. See Docker volumes for why the named volume matters, and deploy an app with Docker Compose for a full project.

Quick reference#

bash
caddy validate --config /etc/caddy/Caddyfile   # check syntax
sudo systemctl reload caddy                    # apply config
sudo journalctl -u caddy --no-pager -n 50      # issuance logs
sudo ss -tlnp | grep -E ':80|:443'             # who holds the ports
curl -I http://example.com                     # redirect to https?
curl -I https://example.com                    # served over https?

Security and safety notes#

  • Do not delete Caddy’s data directory casually. It holds your certificate keys and account.
  • Do not publish the app’s own port. Keep it bound to 127.0.0.1 so all traffic goes through Caddy.

Next#

You now have HTTPS working. Continue with the deploy track, starting at What is a reverse proxy.

Check what you learned4 questions
1Do you need certbot when Caddy serves the domain?
2What does the Caddyfile block example.com { reverse_proxy 127.0.0.1:3000 } do?
3Caddy returns 502. What does that suggest?
4You run Caddy in Docker and recreate the container often. What should you mount?

Frequently asked questions

Do I need certbot with Caddy?

No. Caddy obtains and renews certificates by itself as long as the domain points at the server and ports 80 and 443 are reachable.

Can Caddy run next to nginx?

Not on the same ports. Only one program can listen on 80 and 443, so stop nginx first or use different ports.

Will Caddy get a certificate for localhost or an IP address?

Not a public one. For localhost Caddy makes its own local certificate, which browsers do not trust until you install Caddy's local root. Public certificates need a public domain name.

Where does Caddy store its certificates?

In its data directory, which is /data inside the official Docker image. Keep that on a volume, or Caddy has to request new certificates every time the container is recreated.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios