You put Caddy in front of two app servers because the README promises “automatic HTTPS in one line.” It does. TLS works on day one. Six months later a third site is added the same way, the Caddyfile becomes a wall of repeated blocks, the systemd unit still ships the stock LimitNOFILE=1048576 but the kernel nofile ceiling never moved, the access log is going to journald where you can’t grep it, and when a renewal finally fails because port 80 was firewalled “for security,” nobody finds out until a customer reports the browser warning.
What’s happening
A production-ready edge proxy needs more than ACME automation. TLS renewal has to keep working when sites are added — which means port 80 stays reachable for the HTTP-01 challenge, or the operator switches to DNS-01 deliberately. The access log has to be a structured file you can grep and ship to a log pipeline, not buried in journald. HSTS needs preload only after you have committed to the policy. Timeouts (read_timeout, write_timeout, idle_timeout) need values that match upstream behaviour, not Caddy defaults. The OS-level file descriptor limit has to actually take effect on the Caddy process — LimitNOFILE= in the unit file is ignored if the kernel-wide ceiling is lower. Request IDs need to be propagated to upstream so a trace from the app log can be tied back to an edge log line. None of these is hard in isolation; they compound, and the first time you find out is during an incident.
How to verify
After install, the listeners and the cert chain should look like this:
ss -lntp | grep -E ':(80|443|2019)\b'
# 0.0.0.0:80 caddy
# 0.0.0.0:443 caddy
# 127.0.0.1:2019 caddy (admin API, localhost only)
The Caddyfile must parse cleanly before reload:
caddy validate --config /etc/caddy/Caddyfile
A curl -I against a configured vhost should show HSTS and a request-id passthrough:
curl -sI https://example.com/ | grep -iE 'strict-transport|x-request-id|server'
And the live cert chain, verifying it’s the renewed one and not a stale staging cert:
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null \
| openssl x509 -noout -issuer -dates
If Server: Caddy is missing, issuer says (STAGING), or the admin API is bound to 0.0.0.0:2019, the install is not done yet.
The fix
Save the script below as /root/setup-caddy-ubuntu22.sh, then run it. It performs the full installer flow on a fresh Ubuntu 22.04 VM: base update, installs Caddy from the official Cloudsmith APT repo, writes a multi-site Caddyfile skeleton with HSTS + access log + sane timeouts + request-id passthrough, drops the admin API on 127.0.0.1:2019, hardens SSH while keeping the root key, installs and configures ufw to allow only 22/80/443, configures fail2ban to consume the JSON access log, applies sysctl tuning relevant to high-concurrency edge proxies, raises LimitNOFILE to 200000 in a systemd override and matches it with /etc/security/limits.d, validates the Caddyfile, starts the service, and waits for both listeners before continuing.
#!/usr/bin/env bash
set -euo pipefail
### Ubuntu 22.04 Caddy 2.x reverse proxy installer
### TLS via Let's Encrypt HTTP-01 (port 80 stays open)
### Admin API bound to 127.0.0.1:2019
### No local backups; assumes VM-level backup
### Reboot remains manual
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
echo
echo "========================================"
echo "Caddy 2.x Ubuntu 22.04 Setup"
echo "========================================"
echo
echo "==> Updating base OS"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get -y upgrade
echo "==> Installing base packages"
apt-get -y install \
curl \
ca-certificates \
gnupg \
debian-keyring \
debian-archive-keyring \
apt-transport-https \
ufw \
fail2ban \
unattended-upgrades \
chrony \
lsof \
net-tools \
jq
echo "==> Enabling time sync"
systemctl enable --now chrony
timedatectl set-ntp true
echo "==> Applying sysctl tuning for edge proxy"
cat > /etc/sysctl.d/99-caddy.conf <<'SYSCTL'
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 250000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240 65535
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
fs.file-max = 2097152
SYSCTL
modprobe tcp_bbr || true
echo tcp_bbr > /etc/modules-load.d/bbr.conf
sysctl --system
echo "==> Setting file limits"
cat > /etc/security/limits.d/99-caddy.conf <<'LIMITS'
* soft nofile 200000
* hard nofile 200000
root soft nofile 200000
root hard nofile 200000
caddy soft nofile 200000
caddy hard nofile 200000
LIMITS
echo "==> Hardening SSH while keeping root SSH key login enabled"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%H%M%S)
set_sshd_option() {
local key="$1"
local value="$2"
if grep -qE "^[#[:space:]]*${key}[[:space:]]+" /etc/ssh/sshd_config; then
sed -i "s|^[#[:space:]]*${key}[[:space:]].*|${key} ${value}|" /etc/ssh/sshd_config
else
echo "${key} ${value}" >> /etc/ssh/sshd_config
fi
}
set_sshd_option PermitRootLogin prohibit-password
set_sshd_option PasswordAuthentication no
set_sshd_option PubkeyAuthentication yes
set_sshd_option KbdInteractiveAuthentication no
set_sshd_option ChallengeResponseAuthentication no
set_sshd_option X11Forwarding no
set_sshd_option ClientAliveInterval 300
set_sshd_option ClientAliveCountMax 2
set_sshd_option MaxAuthTries 4
sshd -t
systemctl restart ssh
echo "==> Configuring UFW (22, 80, 443 only)"
ufw --force reset
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable
echo "==> Adding Caddy APT repository"
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \
| gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
| tee /etc/apt/sources.list.d/caddy-stable.list
apt-get update
apt-get -y install caddy
echo "==> Writing Caddyfile skeleton (multi-site + HSTS + access log + request id)"
mkdir -p /etc/caddy /var/log/caddy
chown caddy:caddy /var/log/caddy
cat > /etc/caddy/Caddyfile <<'CADDYFILE'
{
email ops@example.com
admin 127.0.0.1:2019
servers {
timeouts {
read_body 10s
read_header 10s
write 30s
idle 120s
}
trusted_proxies static private_ranges
}
log default {
output file /var/log/caddy/access.log {
roll_size 100MiB
roll_keep 14
roll_keep_for 14d
}
format json
}
}
(common_headers) {
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains"
X-Content-Type-Options "nosniff"
Referrer-Policy "strict-origin-when-cross-origin"
-Server
}
}
(propagate_request_id) {
request_header X-Request-Id {http.request.uuid}
header X-Request-Id {http.request.uuid}
}
app1.example.com {
import common_headers
import propagate_request_id
reverse_proxy 10.0.0.11:8080 {
header_up X-Real-IP {http.request.remote.host}
header_up X-Forwarded-For {http.request.remote.host}
header_up X-Forwarded-Host {http.request.host}
health_uri /healthz
health_interval 10s
health_timeout 3s
transport http {
dial_timeout 5s
response_header_timeout 30s
}
}
}
app2.example.com {
import common_headers
import propagate_request_id
reverse_proxy 10.0.0.12:3000 {
header_up X-Real-IP {http.request.remote.host}
header_up X-Forwarded-For {http.request.remote.host}
header_up X-Forwarded-Host {http.request.host}
health_uri /healthz
health_interval 10s
health_timeout 3s
}
}
CADDYFILE
chown caddy:caddy /etc/caddy/Caddyfile
chmod 644 /etc/caddy/Caddyfile
echo "==> Validating Caddyfile"
caddy validate --config /etc/caddy/Caddyfile
echo "==> Setting systemd limits for Caddy"
mkdir -p /etc/systemd/system/caddy.service.d
cat > /etc/systemd/system/caddy.service.d/override.conf <<'CADDY_OVERRIDE'
[Service]
LimitNOFILE=200000
LimitNPROC=16384
CADDY_OVERRIDE
systemctl daemon-reload
echo "==> Configuring fail2ban for Caddy JSON access log"
mkdir -p /etc/fail2ban/filter.d /etc/fail2ban/jail.d
cat > /etc/fail2ban/filter.d/caddy-status.conf <<'F2B_FILTER'
[Definition]
failregex = ^.*"remote_ip":"<HOST>".*"status":(401|403|404|429).*$
ignoreregex =
F2B_FILTER
cat > /etc/fail2ban/jail.d/caddy.local <<'F2B_JAIL'
[caddy-status]
enabled = true
filter = caddy-status
logpath = /var/log/caddy/access.log
maxretry = 30
findtime = 5m
bantime = 1h
F2B_JAIL
systemctl enable --now fail2ban
echo "==> Configuring unattended security upgrades"
dpkg-reconfigure -f noninteractive unattended-upgrades
echo "==> Enabling and starting Caddy"
systemctl enable --now caddy
systemctl restart caddy
echo "==> Waiting for Caddy listeners on :80 and :443"
for i in {1..30}; do
if ss -lntp | grep -q ':443' && ss -lntp | grep -q ':80'; then
break
fi
sleep 1
done
if ! ss -lntp | grep -q ':443'; then
echo "Caddy did not bind to :443"
systemctl status caddy --no-pager -l || true
journalctl -u caddy -n 120 --no-pager || true
exit 1
fi
echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Kernel:"
uname -r
echo
echo "Listeners:"
ss -lntp | grep -E ':(80|443|2019)\b' || true
echo
echo "Services:"
systemctl is-active caddy fail2ban chrony ufw || true
echo
echo "Caddy version:"
caddy version
echo
echo "Caddyfile path: /etc/caddy/Caddyfile"
echo "Access log: /var/log/caddy/access.log"
echo "Admin API: 127.0.0.1:2019 (localhost only)"
echo
echo "First test request:"
echo " curl -I https://app1.example.com/"
echo
echo "Reload after Caddyfile edits:"
echo " caddy validate --config /etc/caddy/Caddyfile && systemctl reload caddy"
Make it executable and run it:
chmod 700 /root/setup-caddy-ubuntu22.sh
/root/setup-caddy-ubuntu22.sh
Once DNS for app1.example.com and app2.example.com points at the VM and port 80 is reachable from the public internet, Caddy will request real certificates on first hit. Confirm the issuer is not the staging environment, then enable HSTS preload on a separate change only after you have committed to HTTPS-only for every subdomain. The script does not configure host-level backups — the assumption is the VM has snapshot/backup at the cloud layer.
Run the edge proxy on a Stack Harbor-managed Cloud Compute environment with monitoring, patching cadence, and on-call escalation in place — reach out.