lab.awk.im

Caddy: snippets, bind, and living without :443

· caddy

Sometimes Caddy is not the thing listening on port 443: an L4 proxy, sslh, or some other service owns it and hands connections to Caddy on a loopback port. Caddy handles this fine, but a few defaults need adjusting.

Move HTTPS off 443

https_port changes where Caddy serves TLS. The side effect is that automatic HTTP→HTTPS redirects now point at that port, e.g. https://example.com:8443/, which is wrong when the public port is still 443. Disable the automatic redirects and write the one you actually want:

{
	https_port 8443
	auto_https disable_redirects
}

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

https://example.com:8443 {
	bind 127.0.0.1
	import site
}

Certificates still work: with port 80 reachable, the HTTP-01 challenge is answered by the http:// site.

bind keeps it private

Without bind, Caddy listens on all interfaces. bind 127.0.0.1 on a site block makes that listener loopback-only, which is exactly what you want behind another proxy. Check with ss -tlpn | grep caddy.

Snippets for shared config

When the same content is served on two listeners (say a TLS one and a plain one for an internal consumer), a snippet keeps them identical. I keep snippets at the top of the file, right after the global options, before any site block that imports them.

(site) {
	root * /srv/site
	encode zstd gzip
	file_server
	header Strict-Transport-Security "max-age=31536000"
	handle_errors 404 {
		rewrite * /404.html
		file_server
	}
}

Recent Caddy versions accept status codes as arguments to handle_errors, which is much nicer than the old expression {err.status_code} == 404 matcher.

Validate before reload

caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile
caddy reload   --config /etc/caddy/Caddyfile