Infra: Caddy instead of nginx (plain swap, TLS stays in front)

Caddy 2.11.7 built with xcaddy, with caddy-ratelimit pinned to commit
5625512f (for M3/F7 later; its only tag is years old). Prod bakes
public/ and the config into the image (target caddy); dev uses the
caddy-base stage with docker/caddy and public/ bind-mounted.

It's a 1:1 swap for the host's TLS proxy: plain HTTP on
127.0.0.1:${APP_PORT:-8090}, auto_https off, and
trusted_proxies private_ranges so the proxy's X-Forwarded-* headers
still reach Laravel (checked: https + forwarded host in redirects, as
before). Narrow that range once it's known where TLS ends.

Carried over from nginx:
- php_fastcgi to app:9000, 300s read timeout, try_files to index.php
- /build, /static, /packages and /storage (from the storage volume)
  with Cache-Control "public, max-age=31536000, immutable"
- /sitemap.xml from storage/app, max-age=3600, application/xml
- X-Frame-Options SAMEORIGIN + X-Content-Type-Options nosniff
- 100 MiB upload limit (413 above, as before)
- dotfiles denied except /.well-known/
- /stoic/ proxied to stoic:2727, prefix stripped, cookie paths
  rewritten to /stoic/
- access log to stdout, favicon/robots not logged

Changed on purpose:
- FastCGI page cache dropped (and X-Cache-Status with it). The static
  page cache comes later (P2). It also replayed the first visitor's
  Set-Cookie (session + XSRF) to other guests, since it ignored
  Set-Cookie and keyed on URL only.
- New: zstd/gzip compression for text responses (nginx had none).
- Security headers now on every response; prod nginx dropped them on
  PHP responses and on 4xx/5xx.
- A missing file under the static paths is a plain 404 instead of
  Laravel's page with a 404 status.
- No Via or CGI Status header; one Content-Type on the sitemap (nginx
  sent two).
- Dropped as unneeded: open_file_cache, fastcgi buffers/keepalive,
  `charset utf-8` (Caddy's MIME types carry it where it matters).

Stoic's dev config called the app at http://nginx/validate; it's now
http://caddy/validate. bootstrap/app.php and .env.example comments
updated. docker/nginx/ is removed.

Not in this repo, not changed: boboko-test and the boboko starter
still use nginx.

Deploy note: the service is renamed nginx -> caddy; Envoy's
`up -d --remove-orphans` removes the old nginx container. Expect a few
seconds without a listener on 8090 while it swaps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
elviraandClaude Opus 5.5 committed 2026-11-03 01:01:13 +02:00
1 parent f0a1b928dd
commit 6d88d53882
12 files changed
+187 -181

No files matched your search

+1 -1
View File
@@ -76,7 +76,7 @@ VITE_APP_NAME="${APP_NAME}"
SESSION_SECURE_COOKIE=true
# Stoic is the headless CMS, proxied at /stoic/ behind the app in prod (see docker/nginx/prod.conf)
# Stoic is the headless CMS, proxied at /stoic/ behind the app (see docker/caddy/common.caddy)
STOIC_SECRET=dev-secret-change-me
STOIC_SSO_SECRET=dev-sso-secret-change-me
STOIC_HOST=http://localhost:2727
+15 -3
View File
@@ -103,10 +103,22 @@ EXPOSE 9000
ENTRYPOINT ["/entrypoint.sh"]
CMD ["php-fpm8.5", "-F"]
# ── Stage 5: Nginx (public assets baked in from production) ──────────────────
FROM nginx:alpine AS nginx
# ── Stage 5a: Caddy binary (xcaddy, pinned versions) ─────────────────────────
# caddy-ratelimit isn't used yet: it's for the search subdomain (M3) and the
# PHP-route rate limits (F7). Its only tag (v0.1.0) is years old, so it's
# pinned to a commit.
FROM caddy:2.11.7-builder-alpine AS caddy-build
RUN xcaddy build v2.11.7 \
--with github.com/mholt/caddy-ratelimit@5625512f24f6f59d6f64fb3aafe5eecff0b286db
COPY docker/nginx/prod.conf /etc/nginx/conf.d/default.conf
# Development uses this stage as is, with docker/caddy and public/ bind-mounted
# (docker-compose.dev.yml).
FROM caddy:2.11.7-alpine AS caddy-base
COPY --from=caddy-build /usr/bin/caddy /usr/bin/caddy
# ── Stage 5b: Caddy for production (config + public assets baked in) ─────────
FROM caddy-base AS caddy
COPY docker/caddy/global.caddy docker/caddy/common.caddy docker/caddy/Caddyfile /etc/caddy/
COPY --from=production /var/www/html/public /var/www/html/public
# ── Stage 6: Development ──────────────────────────────────────────────────────
+4 -2
View File
@@ -11,8 +11,10 @@
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
// nginx (docker/nginx/prod.conf) only listens on plain HTTP:80.
// Without trusting that layer's X-Forwarded-Proto header, Laravel sees every
// Caddy (docker/caddy) only listens on plain HTTP:80, behind the
// host's TLS proxy, and passes that proxy's X-Forwarded-* headers on
// (trusted_proxies in docker/caddy/global.caddy).
// Without trusting the X-Forwarded-Proto header, Laravel sees every
// request as http://, so url()/signed-route generation and
// verification (URL::hasValidSignature()) both use the wrong
// scheme — breaks any signed URL whose recipient hits it over
+7 -3
View File
@@ -31,12 +31,16 @@ services:
valkey:
condition: service_started
nginx:
image: nginx:alpine
caddy:
build:
context: .
target: caddy-base
image: boboko-caddy
command: caddy run --config /etc/caddy/Caddyfile.dev --adapter caddyfile
ports:
- "${APP_PORT:-8080}:80"
volumes:
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
- ./docker/caddy:/etc/caddy:ro
- ./public:/var/www/html/public:ro
- ./storage/app/public:/var/www/html/storage/app/public:ro
depends_on:
+4 -2
View File
@@ -32,10 +32,12 @@ services:
retries: 20
start_period: 60s
nginx:
# Caddy (docker/caddy/Caddyfile), plain HTTP on 127.0.0.1 behind the
# host's TLS proxy, same port nginx used.
caddy:
build:
context: .
target: nginx
target: caddy
restart: unless-stopped
ports:
- "127.0.0.1:${APP_PORT:-8090}:80"
+44
View File
@@ -0,0 +1,44 @@
# Production. public/ is baked into the image (Dockerfile, target caddy);
# storage/app is the shared `storage` volume, read-only.
# No page cache here: nginx's FastCGI cache is dropped on purpose, the
# static page cache comes later (P2).
{
import global.caddy
}
import common.caddy
:80 {
import site
route {
import deny_dotfiles
import stoic
# Content-hashed or versioned files: cache forever.
@immutable path /build/* /static/* /packages/*
handle @immutable {
header Cache-Control "public, max-age=31536000, immutable"
file_server
}
# User media, straight from the storage volume (no symlink needed).
handle_path /storage/* {
root * /var/www/html/storage/app/public
header Cache-Control "public, max-age=31536000, immutable"
file_server
}
# Generated by the app into storage/app.
handle /sitemap.xml {
root * /var/www/html/storage/app
header Cache-Control "public, max-age=3600"
header Content-Type "application/xml; charset=utf-8"
file_server
}
import laravel
}
import static_404
}
+19
View File
@@ -0,0 +1,19 @@
# Development. public/ and storage/app/public are bind-mounted
# (docker-compose.dev.yml); /storage/ goes through the public/storage symlink.
# No long cache headers in dev. Vite is reached directly on its own port
# (public/hot), not through Caddy.
{
import global.caddy
}
import common.caddy
:80 {
import site
route {
import deny_dotfiles
import stoic
import laravel
}
}
+76
View File
@@ -0,0 +1,76 @@
# Snippets shared by Caddyfile (prod) and Caddyfile.dev, imported right after
# their global options block (global.caddy).
# Site-level settings shared by both environments.
(site) {
root * /var/www/html/public
# New with Caddy: nginx compressed nothing. Caddy only compresses text
# types (HTML, CSS, JS, JSON, SVG…), never images or fonts.
encode zstd gzip
# Same upload limit as nginx's client_max_body_size (and php.ini).
request_body {
max_size 100MiB
}
# On every response. nginx only added these to static files and 2xx/3xx
# (prod's PHP location dropped them).
header {
X-Frame-Options "SAMEORIGIN"
X-Content-Type-Options "nosniff"
# Caddy's proxy adds Via; nginx sent none. A deletion is applied as
# the response goes out, so it also catches headers set later.
-Via
}
# Access log to stdout, as nginx did; favicon/robots skipped like nginx.
log
@quiet path /favicon.ico /robots.txt
log_skip @quiet
}
# Dotfiles (.env, .git…) are never served; /.well-known/ is allowed.
# Use inside a route block, first.
(deny_dotfiles) {
@dotfiles {
path */.*
not path /.well-known/*
}
respond @dotfiles 403
}
# Stoic (the headless CMS) under /stoic/, prefix stripped, cookies scoped to
# /stoic/ (nginx: proxy_cookie_path / /stoic/).
(stoic) {
handle_path /stoic/* {
reverse_proxy stoic:2727 {
header_up X-Real-IP {client_ip}
header_down Set-Cookie "(?i)(;\s*path=)/" "$1/stoic/"
}
}
}
# Laravel. php_fastcgi does the try_files (file, dir/index.php, index.php);
# file_server serves the files that exist in public/.
(laravel) {
php_fastcgi app:9000 {
read_timeout 300s
# PHP-FPM's CGI status line; nginx didn't pass it on either.
header_down -Status
}
file_server
}
# A missing file under one of prod's static paths (/build/, /storage/…): a
# plain 404, without the Cache-Control/Content-Type that path's handle already
# set.
# (nginx sent these to Laravel via `error_page 404 /index.php`, kept the 404
# status and showed Laravel's page; nobody sees that page for an asset.)
(static_404) {
handle_errors 404 {
header -Cache-Control
header Content-Type "text/plain; charset=utf-8"
respond "Not Found" 404
}
}
+16
View File
@@ -0,0 +1,16 @@
# Global options shared by Caddyfile (prod) and Caddyfile.dev, imported inside
# their global options block.
# TLS ends in front of this container (a proxy on the host talks to it on
# 127.0.0.1:8090), so Caddy serves plain HTTP and never tries to get
# certificates. Revisit once it's settled where TLS should end.
auto_https off
servers {
# Keep the X-Forwarded-For/-Proto/-Host the host proxy sends: it
# reaches the container from a private (docker) address. Without this
# Caddy replaces them with its own, so Laravel (trustProxies '*')
# would see every request as plain http. Narrow this to the proxy's
# actual address once it's known.
trusted_proxies static private_ranges
}
-48
View File
@@ -1,48 +0,0 @@
server {
listen 80;
server_name _;
root /var/www/html/public;
index index.php;
client_max_body_size 100M;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
resolver 127.0.0.11 valid=5s;
set $upstream app:9000;
fastcgi_pass $upstream;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_read_timeout 300;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
location /stoic/ {
proxy_pass http://stoic:2727/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cookie_path / /stoic/;
}
location ~ /\.(?!well-known).* {
deny all;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
}
-121
View File
@@ -1,121 +0,0 @@
fastcgi_cache_path /tmp/fcgi_cache levels=1:2 keys_zone=app_cache:10m max_size=256m inactive=10m use_temp_path=off;
upstream php_fpm {
server app:9000;
keepalive 16;
}
server {
listen 80;
server_name _;
root /var/www/html/public;
index index.php;
client_max_body_size 100M;
charset utf-8;
open_file_cache max=5000 inactive=30s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# --- FastCGI cache bypass rules ---
set $skip_cache 0;
if ($request_method = POST) { set $skip_cache 1; }
if ($request_uri ~* "^/(boboko|up)") { set $skip_cache 1; }
if ($query_string) { set $skip_cache 1; }
# Any logged-in request must never be served a cached response —
# the cache key is URL-only ($scheme$request_method$host$request_uri,
# see fastcgi_cache_key below), with no per-session variation. Without
# this, the first cached hit on an authenticated GET page (e.g.
# /account) gets replayed verbatim to every other visitor of that same
# URL for up to fastcgi_cache_valid's 10m window — a real account/
# session data leak, not just a staleness bug. Laravel's session
# cookie name is env('SESSION_COOKIE', Str::slug(APP_NAME).'-session')
# (config/session.php) — APP_NAME varies per environment, so this
# matches the fixed '-session' suffix rather than a hardcoded name.
if ($http_cookie ~* "-session") { set $skip_cache 1; }
# Vite build assets — content-hashed filenames, safe to cache forever
location /build/ {
add_header Cache-Control "public, max-age=31536000, immutable";
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
try_files $uri =404;
}
location /static/ {
add_header Cache-Control "public, max-age=31536000, immutable";
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
try_files $uri =404;
}
location /packages/ {
add_header Cache-Control "public, max-age=31536000, immutable";
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
try_files $uri =404;
}
# User media — served directly from the storage volume (no symlink needed)
location /storage/ {
alias /var/www/html/storage/app/public/;
add_header Cache-Control "public, max-age=31536000, immutable";
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
try_files $uri =404;
}
location / {
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass php_fpm;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_read_timeout 300;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_keep_conn on;
fastcgi_cache app_cache;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_valid 200 10m;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
add_header X-Cache-Status $upstream_cache_status;
}
location /stoic/ {
proxy_pass http://stoic:2727/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cookie_path / /stoic/;
}
location ~ /\.(?!well-known).* {
deny all;
}
location = /sitemap.xml {
root /var/www/html/storage/app;
try_files /sitemap.xml =404;
add_header Cache-Control "public, max-age=3600";
add_header Content-Type "application/xml; charset=utf-8";
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
}
+1 -1
View File
@@ -2,7 +2,7 @@ base_path: /stoic
auth:
mode: session
validate_url: http://nginx/validate
validate_url: http://caddy/validate
cookie: laravel_session
content_path: /content