↓Skip to main content

Serving Apps with Automatic HTTPS on a Small Server

The server has one job: run apps and serve them over HTTPS. The shape I ended up with is simple. Caddy is the only container with published ports. Every app lives on a private Docker network, listens on an internal port, and is reachable only through Caddy. Caddy gets the certificates, renews them, redirects HTTP to HTTPS, and proxies the requests.

Internet :80/:443
        │
        ▼
   caddy container
   TLS + reverse proxy
        │  web network
        ├── go-app:8080
        └── another-app:3000

There is no certbot, no cron job, and no per-app TLS configuration. Adding an app means adding a service, adding a site block to the Caddyfile, and pointing DNS at the server.

The examples use example.com and a generic Go app called go-app; replace those with your own domain and service name.

Before the stack #

The server is a Debian/Ubuntu box with a public IPv4 address. You need:

  • A domain you control, with DNS records pointing at the server.
  • Docker Engine and the Compose plugin. Verify with docker compose version.
  • A user in the docker group, or sudo for every command.
  • ufw if you want the host firewall in front of the published ports.

Install Docker from the official Engine instructions rather than from a distribution package that may be years behind. Then add your user to the docker group and log in again:

sudo usermod -aG docker "$USER"

DNS first #

Caddy can only get a certificate for a name that resolves to the server. Point an A record at the IPv4 address for every hostname in the Caddyfile:

example.com          A    203.0.113.10
www.example.com      A    203.0.113.10
app.example.com      A    203.0.113.10

If the server has IPv6, add matching AAAA records and make sure the firewall allows 80 and 443 over IPv6 too. Check propagation from the server:

dig +short example.com
dig +short app.example.com

If dig is not installed, it is in the dnsutils package:

sudo apt install -y dnsutils

Do this before starting Caddy. Caddy will retry failed certificate requests, but it is much easier to debug DNS first than to read ACME errors in the logs.

Firewall #

The host firewall allows SSH on a non-default port, plus the two ports Caddy needs. The order matters: allow the new SSH port before denying the old one, and keep the current session open while you test.

sudo ufw default deny incoming
sudo ufw default allow outgoing

# SSH on 2233; substitute your own port
sudo ufw allow 2233/tcp

# Caddy
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 443/udp

# only after confirming 2233 works
sudo ufw deny 22/tcp

sudo ufw enable
sudo ufw status verbose

The resulting rules should look like this:

To                         Action      From
--                         ------      ----
2233/tcp                   ALLOW IN    Anywhere
80/tcp                     ALLOW IN    Anywhere
443/tcp                    ALLOW IN    Anywhere
443/udp                    ALLOW IN    Anywhere
22/tcp                     DENY IN     Anywhere

80 is needed for the ACME HTTP challenge and for the HTTP-to-HTTPS redirect. 443 is the actual TLS listener. 443/udp enables HTTP/3; if you do not want it, drop that rule and the corresponding Compose port.

The SSH change is not security by itself. It cuts log noise, but the real protection is key-only authentication, no root login, and keeping the host patched. Changing the firewall rule does not move sshd: set Port 2233 in /etc/ssh/sshd_config (or a drop-in), restart SSH, and test a second session on 2233 before closing the one you have. If you previously allowed port 22, remove that rule before adding the deny:

sudo ufw delete allow 22/tcp
sudo ufw delete allow OpenSSH
sudo ufw deny 22/tcp

Docker and UFW: Docker publishes container ports by writing its own iptables rules, and those can bypass UFW. That is one reason the app containers here use expose instead of ports. If you publish an app port, do not assume UFW is protecting it. Only Caddy gets a host port.

If the server is at a cloud provider, open 80 and 443 in the provider’s firewall or security group as well. The host firewall is only one layer.

Repository layout #

I keep the infrastructure repository at /opt/myserver, with the app next to the Compose file:

/opt/myserver/
├── caddy/
│   └── Caddyfile
├── docker-compose.yml
├── README.md
└── go-app/
    ├── Dockerfile
    ├── env.sample
    ├── go.mod
    ├── main.go
    └── static/

The infra repo owns the public surface: the Caddyfile, the Compose file, and the network. The app repo owns the application and its Dockerfile. Clone the infra repo into place and give your user ownership:

sudo mkdir -p /opt/myserver
sudo chown "$USER":"$USER" /opt/myserver
git clone git@github.com:you/infra.git /opt/myserver
cd /opt/myserver

go-app/ is either a Git submodule of the infra repo or a separate checkout. If it is a submodule:

git submodule update --init --recursive

If it is a separate repository, clone it into place:

git clone git@github.com:you/go-app.git /opt/myserver/go-app

Create the app’s environment file from the sample and keep it out of Git:

cp go-app/env.sample go-app/.env
chmod 600 go-app/.env
$EDITOR go-app/.env

Add the usual ignores to the infra repository:

go-app/.env
**/.env

The Compose file #

services:
  caddy:
    image: caddy:2
    container_name: caddy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./caddy/Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    networks:
      - web

  go-app:
    build:
      context: ./go-app
    container_name: go-app
    env_file:
      - ./go-app/.env
    restart: unless-stopped
    expose:
      - "8080"
    networks:
      - web

volumes:
  caddy_data:
  caddy_config:

networks:
  web:
    driver: bridge

The important details:

  • Caddy publishes 80, 443, and 443/udp. It is the only public listener.
  • caddy_data stores Caddy’s certificates, account keys, and ACME state. Do not delete it unless you want Caddy to start over.
  • caddy_config stores Caddy’s autosaved config. Keep it with the data volume.
  • The Caddyfile is mounted read-only. Edit it on the host, then reload Caddy.
  • go-app uses expose, not ports. It is reachable as go-app:8080 on the web network, but not from the host or the internet.
  • Caddy reaches the app by its Compose service name. Docker’s DNS resolves go-app inside the web network.
  • The app must listen on 0.0.0.0:8080 inside its container. If it binds 127.0.0.1:8080, Caddy will get a connection refused and return 502.

container_name is convenient for logs and docker compose exec, but it makes the service harder to scale. For one server with one copy of each app, it is fine.

The Caddyfile #

example.com app.example.com {
    reverse_proxy go-app:8080
}

www.example.com {
    redir https://example.com{uri} permanent
}

The first block serves the same app on both example.com and app.example.com. Caddy provisions a certificate for every hostname in the block, so both names must resolve to the server.

reverse_proxy go-app:8080 sends requests to the app container over the web network. Caddy adds the usual forwarding headers and handles WebSocket upgrades, so an app that uses WebSockets or streaming responses does not need extra proxy configuration.

The www block is a canonical redirect. Caddy’s automatic HTTPS already redirects http:// to https://; this block handles the hostname change. If you do not want www, remove the block and the DNS record.

If you want to give the ACME account an email address, put a global options block at the top:

{
    email you@example.com
}

It is optional, but it gives the certificate authority a contact address and makes expiry notices possible.

Caddy stores the certificates under /data. The named volume is what keeps them across container restarts and rebuilds. Without it, every recreate would request new certificates, which is a good way to hit rate limits.

First deploy #

cd /opt/myserver
docker compose up -d --build
docker compose ps

Watch Caddy’s logs while it obtains certificates:

docker compose logs -f caddy

Look for a certificate obtained or serving message. The first request can take a few seconds while ACME completes. Then check the app logs:

docker compose logs -f go-app

From outside the server, verify the three paths:

curl -I http://example.com
curl -I https://example.com
curl -I https://www.example.com
curl -I https://app.example.com

The HTTP request should redirect to HTTPS (308 is Caddy’s automatic redirect). The www request should redirect to the apex domain (301 from the Caddyfile). The HTTPS requests should return whatever the app returns, usually 200 or a redirect to a login page.

If https:// works but http:// times out, port 80 is blocked somewhere. If the certificate request never completes, check DNS and ports 80/443 before changing the Caddyfile.

How the TLS part works #

Caddy’s automatic HTTPS does the certificate work:

  • It obtains a certificate for each site address from an ACME CA, usually Let’s Encrypt or ZeroSSL.
  • It completes the HTTP-01 or TLS-ALPN-01 challenge on ports 80 and 443.
  • It renews certificates before they expire and reloads them without a restart.
  • It redirects HTTP to HTTPS automatically.

There is no certbot timer to maintain and no certificate files to copy into the app. The app only ever speaks plain HTTP on the private Docker network; Caddy is the TLS endpoint.

Caddy will not issue a public certificate for localhost, a bare IP address, or a name that does not resolve publicly. If you are testing locally, use a real domain or Caddy’s internal CA, not the public ACME flow.

If you put Cloudflare or another proxy in front of the server, either set the records to DNS-only so the ACME challenge reaches the server, or configure Caddy’s DNS challenge. An orange-cloud proxy that hides the origin can break HTTP-01.

Adding another app #

Add a service to the Compose file:

  another-app:
    build:
      context: ./another-app
    container_name: another-app
    restart: unless-stopped
    expose:
      - "3000"
    networks:
      - web

Add a site block to the Caddyfile:

another.example.com {
    reverse_proxy another-app:3000
}

Point another.example.com at the server, rebuild, and reload Caddy. The app does not get a host port; Caddy is still the only public listener.

The contract is the same every time: the app listens on 0.0.0.0:<port>, the service name and port match the reverse_proxy target, and both containers are on the web network.

Applying Caddyfile changes #

A Caddyfile edit is a bind-mount content change, so docker compose up -d will not recreate the Caddy container by itself. Validate and reload it:

docker compose exec caddy caddy validate --config /etc/caddy/Caddyfile
docker compose exec caddy caddy reload --config /etc/caddy/Caddyfile

caddy reload is graceful and keeps the old config if the new one is invalid. If you prefer a bigger hammer, docker compose restart caddy also re-reads the file, at the cost of a brief interruption.

Check the logs after a reload. A syntax error or an unresolvable hostname will show up there immediately.

Updating an app #

The update flow is intentionally short:

cd /opt/myserver/go-app
git pull

cd ..
docker compose up -d --build

docker compose up -d --build rebuilds the app image and recreates the app container if the image changed. Caddy is not rebuilt, its certificates are untouched, and the public listener stays up. The app will be unavailable for the moment it takes to recreate the container; Caddy returns 502 during that window.

For an infra change, pull the infra repo first:

cd /opt/myserver
git pull
docker compose up -d --build

If the Caddyfile changed, run the validate/reload commands above.

Private GitHub repositories #

For a private app repo, use a read-only SSH deploy key on the server rather than a personal access token in the clone URL. Generate a key that is specific to this server and repository:

ssh-keygen -t ed25519 -C "server-deploy" -f ~/.ssh/go-app-deploy -N ""
cat ~/.ssh/go-app-deploy.pub

Add the public key to the repository under Settings → Deploy keys, with write access disabled. Then point the remote at SSH. If the server has multiple GitHub keys, use a host alias:

Host github.com-go-app
    HostName github.com
    User git
    IdentityFile ~/.ssh/go-app-deploy
    IdentitiesOnly yes
git remote set-url origin git@github.com-go-app:owner/go-app.git
ssh -T git@github.com-go-app
git pull

If the key is lost, revoke it in the repository settings and generate a new one. Do not copy the private key into the image, the Compose file, or the repository.

Troubleshooting #

SymptomCheck
Caddy cannot obtain a certificateDNS for every hostname in the site block, ports 80/443 from outside, IPv6 rules, and the Caddy logs. A proxied CDN record can also block HTTP-01.
HTTPS returns 502 Bad GatewayThe app is not listening on 0.0.0.0:<port>, the service name/port in reverse_proxy is wrong, or the app is not on the web network. Check docker compose logs go-app.
http:// does not redirectPort 80 is blocked, or Caddy is not running. Check sudo ufw status and the provider firewall.
Caddyfile changes do nothingdocker compose up -d does not recreate Caddy for a bind-mount content change. Run caddy reload or restart the container.
www redirects in a loopDo not both reverse-proxy and redirect the same hostname. Keep www only in the redirect block.
Certificate rate limitRecreating Caddy repeatedly with an empty /data volume requests new certificates. Keep caddy_data and use the ACME staging CA while testing.
.env changes are ignoredThe app reads its environment at container start. Recreate the app container with docker compose up -d after editing the file.
Docker published an app port anywayCheck for a ports: entry on the app service. Use expose and let Caddy proxy it. UFW may not filter Docker-published ports.

Wrapping up #

The setup is one public listener, one private network, and one place where TLS lives. DNS and the firewall are the only host-level pieces; after that, Caddy and Compose do the repetitive work.

Adding an app is a service block, a Caddyfile block, and a DNS record. Updating an app is git pull and docker compose up -d --build. Certificate renewal is not a task at all — it is part of Caddy’s normal operation.