# 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.

URL: https://levelrail.com/learn/domains-https/automatic-https-with-caddy/
Updated: 2026-11-27

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:

```text
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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Browser says connection refused on 443 | Caddy not running or port blocked | `systemctl status caddy`, then open 443 in firewall |
| Logs show an ACME challenge failure | DNS wrong or port 80 or 443 blocked | Fix the A record, open both ports, reload |
| `listen tcp :80: bind: address already in use` | nginx or Apache holds the port | Stop that service, restart Caddy |
| `502 Bad Gateway` | App is not running on the proxied address | Start the app and check the port. See the 502 lesson |
| Reload fails with a parse error | Typo in the Caddyfile | Run `caddy validate` and fix the line it names |
| Certificate re-requested after every restart in Docker | `/data` is not on a volume | Mount a named volume at `/data` |
| `permission denied` binding to port 80 or 443 | Caddy run by a user that cannot bind low ports | Use the packaged systemd service |
| Browser warns about an untrusted certificate on a test domain | Caddy made its own local certificate or a staging one | Use 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:

```text
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](https://levelrail.com/learn/docker/docker-networks-explained/)). They publish no ports to the host. See [Docker volumes](https://levelrail.com/learn/docker/docker-volumes-and-persistent-data/)) for why the named volume matters, and [deploy an app with Docker Compose](https://levelrail.com/blog/deploy-docker-compose-app-self-hosted/)) 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.

## Related lessons

- [What is a reverse proxy](https://levelrail.com/learn/deploy-operate/what-is-a-reverse-proxy/)): what the reverse_proxy line does.
- [Docker volumes and persistent data](https://levelrail.com/learn/docker/docker-volumes-and-persistent-data/)): keep /data across container recreation.
- [Nginx reverse proxy with certbot](https://levelrail.com/learn/domains-https/nginx-reverse-proxy-with-certbot/)): the same result with two tools.

## Next

You now have HTTPS working. Continue with the deploy track, starting at [What is a reverse proxy](https://levelrail.com/learn/deploy-operate/what-is-a-reverse-proxy/)).

## 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

- [Caddy docs, install](https://caddyserver.com/docs/install)
- [Caddy docs, automatic HTTPS](https://caddyserver.com/docs/automatic-https)
