From 9696c0e441fb0e7cddcc2f6ea77ed46909cb69de Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 27 Nov 2026 13:13:16 -0700 Subject: [PATCH] caddy: reload instead of restart, validate before deploy, HTTP catch-all, header timeout, body limits; dns: drop stale records - Caddyfile moves to config/ (directory mount) so 'caddy reload' sees the new file; the handler reloads gracefully instead of restarting - template validate: runs 'caddy validate' with the image Caddy uses - http:// catch-all aborts plain-HTTP requests for unknown hosts / bare IP; known hosts keep the automatic HTTPS redirect (tested) - read_header timeout 10s (default 1 min) - body_limit snippet: 1MB static sites, tinyauth, repaircafe; 10MB Pocket ID, Changedetection, ByteStash, Gotify, iGotify; 20MB Baikal - dns: remove grist, habits, office, podcasts, pdftools (nothing serves them) Co-Authored-By: Claude Opus 5.5 --- dns.yml | 10 ++++++ roles/docker/README.md | 2 +- roles/docker/handlers/main.yml | 10 +++--- roles/docker/tasks/infrastructure/caddy.yml | 19 ++++++++-- roles/docker/templates/Caddyfile.j2 | 40 +++++++++++++++++++-- roles/docker/templates/caddy-compose.yml.j2 | 2 +- 6 files changed, 70 insertions(+), 13 deletions(-) diff --git a/dns.yml b/dns.yml index b5be972..c8381c6 100644 --- a/dns.yml +++ b/dns.yml @@ -92,6 +92,16 @@ ip: "{{ network.netcup_ip }}" - name: "kutt" # old AWS IP, no longer ours (takeover risk) ip: "44.202.81.119" + - name: "grist" # not served (cleanup 2026-11-27) + ip: "{{ network.netcup_ip }}" + - name: "habits" + ip: "{{ network.netcup_ip }}" + - name: "office" + ip: "{{ network.netcup_ip }}" + - name: "podcasts" + ip: "{{ network.netcup_ip }}" + - name: "pdftools" # pointed at the Tailscale IP; nothing listens for it + ip: "{{ network.docker_host_ip }}" - name: "home" ip: "{{ network.netcup_ip }}" - name: "pin" diff --git a/roles/docker/README.md b/roles/docker/README.md index 146c04a..8a31a0e 100644 --- a/roles/docker/README.md +++ b/roles/docker/README.md @@ -128,7 +128,7 @@ roles/docker/ │ └── monitoring/ # changedetection, appriseapi, gotify, cronmaster ├── templates/ # -compose.yml.j2, env and config templates, Caddyfile.j2 ├── files/ufw-docker.sh # UFW integration for Docker -└── handlers/main.yml # restart caddy, restart obsidian-livesync +└── handlers/main.yml # reload caddy, restart crowdsec, restart obsidian-livesync ``` ## Troubleshooting diff --git a/roles/docker/handlers/main.yml b/roles/docker/handlers/main.yml index 5f420f8..b74c458 100644 --- a/roles/docker/handlers/main.yml +++ b/roles/docker/handlers/main.yml @@ -1,10 +1,8 @@ # roles/docker/handlers/main.yml -- name: restart caddy - community.docker.docker_compose_v2: - project_src: /opt/stacks/caddy - files: - - compose.yml - state: restarted +# Graceful: new config without dropping connections. If the new config +# fails to load, Caddy keeps running the old one and the deploy fails. +- name: reload caddy + ansible.builtin.command: docker exec caddy-caddy-1 caddy reload --config /etc/caddy/Caddyfile --adapter caddyfile - name: restart crowdsec community.docker.docker_compose_v2: diff --git a/roles/docker/tasks/infrastructure/caddy.yml b/roles/docker/tasks/infrastructure/caddy.yml index bc8701d..379ff1c 100644 --- a/roles/docker/tasks/infrastructure/caddy.yml +++ b/roles/docker/tasks/infrastructure/caddy.yml @@ -4,15 +4,30 @@ state: directory loop: - /opt/stacks/caddy + - /opt/stacks/caddy/config +# The Caddyfile lives in config/, which is mounted as a directory: a +# single-file bind mount would keep showing Caddy the old file after +# ansible replaces it, and "caddy reload" would reload stale config. +# validate: the new file must pass "caddy validate" with the image Caddy +# runs before it replaces the live one, so a broken Caddyfile never lands. - name: template Caddyfile ansible.builtin.template: src: Caddyfile.j2 - dest: /opt/stacks/caddy/Caddyfile + dest: /opt/stacks/caddy/config/Caddyfile owner: root mode: '0640' group: docker - notify: restart caddy + validate: >- + docker run --rm --network none -e CROWDSEC_API_KEY=validate + -v %s:/etc/caddy/Caddyfile:ro {{ caddy_image }} + caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile + notify: reload caddy + +- name: remove the old Caddyfile location + ansible.builtin.file: + path: /opt/stacks/caddy/Caddyfile + state: absent - name: template caddy .env (CrowdSec bouncer key) ansible.builtin.template: diff --git a/roles/docker/templates/Caddyfile.j2 b/roles/docker/templates/Caddyfile.j2 index f9a5f6b..f19ca6d 100644 --- a/roles/docker/templates/Caddyfile.j2 +++ b/roles/docker/templates/Caddyfile.j2 @@ -1,6 +1,14 @@ -{% if caddy_custom %} -# Plugins come from the custom image (repo phil/caddy-custom). { + servers { + timeouts { + # Clients get 10 s to send request headers (default 1 min), so + # slow-header connections can't pile up. Bodies (uploads) and + # responses are not limited. + read_header 10s + } + } +{% if caddy_custom %} + # Plugins come from the custom image (repo phil/caddy-custom). order crowdsec first order appsec after crowdsec crowdsec { @@ -13,9 +21,18 @@ # CrowdSec being down must not take every site down. appsec_fail_open } +{% endif %} +} + +# Plain-HTTP requests for hostnames we don't serve, or for the bare IP: +# close the connection instead of redirecting. Known hosts still get +# Caddy's automatic HTTPS redirect (inserted before this catch-all), and +# ACME HTTP challenges are answered before routing. Unknown names over +# HTTPS already fail the TLS handshake (no certificate). +http:// { + abort } -{% endif %} # Tell browsers to always use HTTPS for each site for a year. No # includeSubDomains, so hosts not served here are unaffected. # defer: replace any HSTS header an app sends itself, so there's only one. @@ -75,6 +92,7 @@ import hsts import security_headers import protect + import body_limit 1MB header { Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'self'; form-action 'self'" X-Frame-Options SAMEORIGIN @@ -125,6 +143,14 @@ {% endif %} } +# Largest request body a site accepts: import body_limit . Only for +# sites that never take big uploads; larger requests get 413. +(body_limit) { + request_body { + max_size {args[0]} + } +} + # Every proxied site: log, HSTS and the shared headers. (site) { import logging @@ -136,6 +162,7 @@ {{ subdomains.pocketid }} { import site import no_framing + import body_limit 10MB import ratelimit_login pocketid_login /api/webauthn/login/* /api/one-time-access-token* /api/one-time-access-email* reverse_proxy pocket-id:1411 } @@ -143,6 +170,7 @@ {{ subdomains.tinyauth }} { import site import no_framing + import body_limit 1MB import ratelimit_login tinyauth_login /api/oauth/* reverse_proxy tinyauth:3000 } @@ -157,6 +185,7 @@ paper.thesatelliteoflove.com { cal.thesatelliteoflove.com { import site import no_framing + import body_limit 20MB redir /.well-known/caldav /dav.php 302 redir /.well-known/carddav /dav.php 302 # Web admin needs Pocket ID; CalDAV/CardDAV (/dav.php) stays as is. @@ -189,6 +218,7 @@ loclog.thesatelliteoflove.com { watcher.thesatelliteoflove.com { import site import no_framing + import body_limit 10MB import ratelimit_login cd_login /login # UI needs Pocket ID; the API (own key) stays reachable for scripts. @ui not path /api/* @@ -199,6 +229,7 @@ watcher.thesatelliteoflove.com { snippets.thesatelliteoflove.com { import site import no_framing + import body_limit 10MB import ratelimit_login bytestash_login /api/auth/login /api/auth/register reverse_proxy bytestash:5000 } @@ -255,6 +286,7 @@ models.thesatelliteoflove.com { gotify.thesatelliteoflove.com { import site import no_framing + import body_limit 10MB import ratelimit_login gotify_login /client /user reverse_proxy gotify:80 } @@ -262,6 +294,7 @@ gotify.thesatelliteoflove.com { gotify-assistant.thesatelliteoflove.com { import site import no_framing + import body_limit 10MB reverse_proxy igotify-assistant:8080 } @@ -320,6 +353,7 @@ listmonk.nerder.land { repaircafe.nerder.land { import site + import body_limit 1MB redir /volunteer-signup https://listmonk.nerder.land/subscription/form 301 } diff --git a/roles/docker/templates/caddy-compose.yml.j2 b/roles/docker/templates/caddy-compose.yml.j2 index eeca07f..5439c8b 100644 --- a/roles/docker/templates/caddy-compose.yml.j2 +++ b/roles/docker/templates/caddy-compose.yml.j2 @@ -10,7 +10,7 @@ services: env_file: .env # CROWDSEC_API_KEY {% endif %} volumes: - - ./Caddyfile:/etc/caddy/Caddyfile + - ./config:/etc/caddy:ro - ./site:/srv - caddy_data:/data - caddy_config:/config