Caddy as it fronts our services, so the same configuration is not copied into every repository. The
image is the official caddy:2-alpine with the rate limit module and a configuration set by
environment variables:
- the PROXY protocol is always on and trusted only from
PROXY_ALLOW, LISTENER_MODEpicks how Caddy faces the world: on its own, behind an SNI proxy, or on an internal network with its own CA,- the site is a reverse proxy to
UPSTREAM,ACCESSdecides who gets in andRATE_LIMIThow often.
An image built on this one can replace the site with its own (see below), everything else stays.
services:
caddy:
image: ghcr.io/mapiiik/caddy:2
restart: unless-stopped
labels:
com.centurylinklabs.watchtower.enable: "true"
environment:
SERVER_NAME: ${SERVER_NAME}
UPSTREAM: app:8080
LISTENER_MODE: direct
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./data/caddy/data:/data
- ./data/caddy/config:/configKeep /data (certificates and the keys of the internal CA), /config only holds what Caddy last
loaded.
| Variable | Default | Meaning |
|---|---|---|
SERVER_NAME |
localhost |
The hostname the certificate is issued for, or an IP address in the internal mode. |
UPSTREAM |
Where requests go, such as app:8080. Required by the default site. |
|
LISTENER_MODE |
direct |
direct, behind-proxy or internal, see below. |
PROXY_ALLOW |
127.0.0.1/32 ::1/128 |
Addresses the PROXY protocol header is trusted from, separated by spaces. Behind a proxy, its network. |
ACCESS |
public |
public, admin-paths or admins-only, see below. |
ADMIN_IPS |
Addresses or CIDR ranges of administrators, separated by spaces. Required by admin-paths and admins-only. |
|
ADMIN_PATHS |
Paths only administrators may open, such as /admin*. Required by admin-paths. |
|
RATE_LIMIT |
off |
on limits POST to RATE_LIMIT_PATHS, see below. |
RATE_LIMIT_PATHS |
Paths whose POST requests are limited, such as /login /users/login. Required with RATE_LIMIT=on. |
|
RATE_LIMIT_EVENTS |
10 |
How many such requests one client address may make in the window. |
RATE_LIMIT_WINDOW |
1m |
The window, such as 30s or 5m. |
CADDY_GLOBAL_OPTIONS |
More global options, such as email [email protected]. |
|
CADDY_SERVER_EXTRA_DIRECTIVES |
More directives for the site, before the proxy. | |
CADDY_EXTRA_CONFIG |
More site blocks. |
direct: Caddy faces the internet on ports 80 and 443 and gets a certificate from Let's Encrypt. Ports 80 and 443 must be open and DNS must point at the server.behind-proxy: behind a proxy that routes TLS by SNI without terminating it, such as HAProxy withsend-proxy-v2. The proxy owns port 80 and redirects it to HTTPS, Caddy gets the certificate with the TLS-ALPN challenge. Put Caddy into the proxy's network under an alias the proxy knows and setPROXY_ALLOWto that network, so the client addresses come from the PROXY header.internal: only on an internal network, Caddy issues the certificate from its own CA.SERVER_NAMEcan be the server's IP address. Browsers warn until they trust the root certificate,/data/caddy/pki/authorities/local/root.crt. On Windows,certutil -addstore -user Root root.crt.
Without a proxy only this machine is trusted with the PROXY header, so nobody can claim another
address. Everything that depends on the client address (access, rate limit, X-Real-IP for the
upstream) uses the one from the header.
public: everyone.admin-paths: the paths inADMIN_PATHSonly fromADMIN_IPS(403 for others), the rest for everyone. For an admin page, such as Vaultwarden's/admin*.admins-only: the whole site only fromADMIN_IPS.
With RATE_LIMIT=on, one client address may send at most RATE_LIMIT_EVENTS POST requests to the
paths in RATE_LIMIT_PATHS within RATE_LIMIT_WINDOW, then it gets 429 until older ones fall out of
the window. The paths share one budget. Other requests are not limited. Off, the module is in the
image but not on the request path, so it costs nothing.
It is a second layer: a service that has a login should limit attempts itself.
The default site is /etc/caddy/site.caddy. An image built on this one replaces it and keeps the
modes, the PROXY protocol and the global options:
FROM ghcr.io/mapiiik/caddy:2
COPY site.caddy /etc/caddy/site.caddyThe site can import access/{$ACCESS:public}.caddy and rate-limit/{$RATE_LIMIT:off}.caddy like the
default one does.
2, 2.11, 2.11.7 and latest follow the Caddy release, sha-… the commit and build-… what the
image was built from. A nightly build at 2:00 UTC makes a new image only when Caddy, Alpine or the Go
toolchain in the base images changed, an hour before the images built on this one look for a new
base. Watchtower then picks it up on the servers.
Check the configuration in every mode with:
docker run --rm -v "$PWD/tests:/tests:ro" ghcr.io/mapiiik/caddy:2 sh /tests/validate.sh