HUB
Sign inSign up
caddy-proxy logo

bfren/caddy-proxy

By bfren

•Updated 1 day ago

Image
0

10K+

bfren/caddy-proxy repository overview

⁠Caddy Proxy

GitHub release (latest by date) Docker Pulls Docker Image Size GitHub Workflow Status

Docker Hub⁠ - Quay.io⁠ - bfren ecosystem⁠

⁠Introduction

A reverse proxy for your other containers, built on Caddy⁠ and configured using one simple JSON file. Caddy requests and renews Let's Encrypt certificates automatically, using the HTTP challenge or the DNS challenge (via Hetzner⁠) for wildcards and private servers.

It is built on Alpine + S6⁠, and adds:

  • Caddy, compiled with the Hetzner DNS module
  • bf-proxy, a Nushell module that turns conf.json into Caddy's JSON configuration, with secure defaults
  • helper commands to regenerate configuration, manage basic auth users and check or renew certificates

This is the successor to bfren/nginx-proxy⁠ - see Migrating from nginx-proxy⁠.

⁠Quickstart

docker-compose.yml:

services:
  proxy:
    image: quay.io/bfren/caddy-proxy:2.0
    restart: unless-stopped
    ports:
      - "0.0.0.0:80:80"
      - "0.0.0.0:443:443"
      - "0.0.0.0:443:443/udp"
    environment:
      - BF_PROXY_DOMAIN=proxy.example.com
      - [email protected]
      - BF_PROXY_LETS_ENCRYPT_LIVE=0
    volumes:
      - ./ssl:/ssl
      - ./sites:/sites
      - ./www:/www

  app:
    image: traefik/whoami

This example is also in docker-compose.yml⁠.

ssl/conf.json:

{
    "$schema": "https://schemas.bfren.dev/docker/caddy-proxy/domains/2026-11.json",
    "domains": [
        { "primary": "app.example.com", "aliases": [ "www.app.example.com" ], "upstream": "http://app:80" }
    ]
}
  1. Point the DNS records for proxy.example.com, app.example.com and www.app.example.com at your server.
  2. Run docker compose up -d, then check docker compose logs proxy for errors.
  3. Once the sites load, set BF_PROXY_LETS_ENCRYPT_LIVE=1 and run docker compose up -d again.

Until step 3, certificates come from Let's Encrypt's staging server, so browsers warn that they aren't trusted. Staging is used first because the live server is rate limited, and failed attempts while you set things up count towards the limit.

⁠Features

  • 🔒 Automatic HTTPS - certificates are requested and renewed for every host name, and HTTP is redirected to HTTPS
  • 🌐 HTTP or DNS challenge per domain, with wildcard certificates and CNAME delegation for domains hosted elsewhere
  • 📄 One conf.json file, with a JSON schema for validation and autocomplete
  • ⚖️ Multiple upstreams with load balancing, extra routes (proxy, redirect or static files) per path
  • 🛡️ Secure headers, optional TLS 1.3 only, HTTP basic auth, and AI training crawlers blocked by default
  • 🗜️ zstd / gzip compression, HTTP/3 and WebSockets
  • 🔧 Maintenance page when an upstream is down
  • 🔄 Optional automatic reload when configuration changes, without dropping connections
  • 🧩 Add your own Caddy routes per domain, or take complete control of a domain's configuration
  • 🩺 Health check using a local server that isn't logged
  • 👤 Caddy runs as www (UID 1000), not root

⁠Tags

Images are published to Docker Hub (bfren/caddy-proxy) and Quay.io (quay.io/bfren/caddy-proxy) for linux/amd64, linux/arm/v7 and linux/arm64.

TagExampleUse when
latestYou always want the newest image
MAJOR2You want updates, but not breaking changes
MINOR2.0You want fixes only - recommended
VERSION2.0.5You want to pin this image exactly

A new MAJOR version means a breaking change. Before moving to one, read the changelog⁠, where each breaking change is marked Breaking.

⁠Configuration

⁠Ports
PortPurpose
80HTTP challenge, and redirecting HTTP to HTTPS
443HTTPS
443/udpHTTP/3 (optional)

Caddy runs as www and needs no extra capabilities: Docker lets unprivileged users bind to ports below 1024 inside a container. With network_mode: host the host's setting applies instead, so set net.ipv4.ip_unprivileged_port_start=80 (or lower) on the host.

⁠Volumes
VolumePurpose
/sslconf.json, users.json (basic auth), and Caddy's certificates (/ssl/caddy).
/sitesPer-domain Caddy configuration - see below⁠.
/www/www/public is served for BF_PROXY_DOMAIN, with the maintenance page.

Everything in the volumes is owned by www (UID / GID 1000), and /ssl is readable only by its owner. On the host, edit the files as UID 1000 (usually the first user account) or with sudo.

⁠Environment Variables
VariableValuesDescriptionDefault
BF_PROXY_DOMAINdomainRequired. The proxy's domain - unknown hosts redirect here.
BF_PROXY_LETS_ENCRYPT_EMAILemailRequired. Let's Encrypt account email.
BF_PROXY_LETS_ENCRYPT_LIVE0, 1Use the live server - 0 uses staging.0
BF_PROXY_USE_INTERNAL_CA0, 1Use Caddy's internal (untrusted) CA - for testing.0
BF_PROXY_ACME_CHALLENGEhttp, dnsDefault challenge - can be set per domain.http
BF_PROXY_DNS_PROVIDERhetznerDNS provider for the DNS challenge.hetzner
BF_PROXY_HETZNER_TOKENtokenHetzner API token for the DNS challenge.
BF_PROXY_DNS_TTLdurationTTL of DNS challenge records, e.g. 60s.provider's
BF_PROXY_DNS_PROPAGATION_DELAYdurationWait before a DNS challenge record is checked.60s
BF_PROXY_DNS_PROPAGATION_TIMEOUTdurationHow long to wait for DNS propagation, e.g. 5m.
BF_PROXY_DNS_RESOLVERSIPsSpace-separated resolvers to check propagation.
BF_PROXY_DNS_OVERRIDE_ZONEdomainDelegate DNS challenges to <primary>.<zone>.
BF_PROXY_SSL_REDIRECT_TO_CANONICAL0, 1Default for redirectToPrimary.0
BF_PROXY_UPSTREAM_RETRYdurationDefault for retry (0s to disable).5s
BF_PROXY_AUTO_RELOAD0, 1Regenerate and reload when configuration changes.0
BF_PROXY_HARDEN0, 1Allow only TLS 1.3 (older devices may fail).0
BF_PROXY_BLOCK_AI_BOTS0, 1Return 403 to AI training crawlers.1
BF_PROXY_ACCESS_LOG0, 1Write access logs to the container output.0
BF_PROXY_CLEAN_INSTALL0, 1Delete all configuration and certificates on start.0
BF_PROXY_AUTO_PRIMARYdomainWith BF_PROXY_AUTO_UPSTREAM, generate a missing conf.json.
BF_PROXY_AUTO_UPSTREAMURLUpstream for the generated domain.
BF_PROXY_AUTO_ALIASESnamesAliases for the generated domain.
BF_PROXY_AUTO_CUSTOM0, 1Mark the generated domain as custom.0
BF_PROXY_MAINTENANCE_REFRESH_SECONDSintegerMaintenance page refresh interval.6

See Alpine + S6⁠ for BF_DEBUG, BF_TZ and the other base image variables.

⁠Build Arguments
ArgumentDescriptionDefault
CADDY_VERSIONCaddy release to build.2.11.9
CADDY_HETZNER_VERSIONcaddy-dns/hetzner release.2.0.1
CADDY_BUILDERxcaddy build image.quay.io/bfren-bin/caddy:2.11-builder-alpine
AI_ROBOTS_VERSIONai.robots.txt release.v2.0

⁠How It Works

On start-up, conf.json is read (or converted from nginx-proxy format, or generated from the BF_PROXY_AUTO_* variables), checked, and turned into one Caddy configuration file. Each domain's part is kept in /sites/<primary>.json. Caddy then runs under S6. The health check uses a server on 127.0.0.1:2020 with no access log, so it doesn't fill the container log.

⁠conf.json

Domains are defined in /ssl/conf.json - see proxy-conf-sample.json⁠ for an example and the schema⁠ for the full definition. Add $schema (as in the Quickstart) for validation and autocomplete.

PropertyDescription
primaryRequired. The primary domain name.
aliasesOther host names - wildcards need the DNS challenge.
upstreamRequired (unless custom). e.g. http://app:5000 - a list load balances.
lbLoad balancing policy (default random) - see below.
retryHow long to retry an unreachable upstream, e.g. 10s.
challengehttp or dns - defaults to BF_PROXY_ACME_CHALLENGE.
dnsOverrideDomainWhere to create DNS challenge records ("" to disable).
redirectToPrimaryRedirect aliases to the primary domain.
authBasic auth: false (default), true for all users, or a list of names.
headersResponse headers to add or override - null stops the proxy adding one.
routesExtra routes, before the default upstream - see below.
compressfalse to turn off compression.
clacksfalse to remove the X-Clacks-Overhead header.
tlsSkipVerifytrue to accept self-signed https upstreams.
customSee below⁠.

lb can be random, round_robin, least_conn, first, ip_hash, uri_hash or client_ip_hash.

Each route can have a path (e.g. /api/* - without one it matches everything) and exactly one action:

ActionDescription
upstreamProxy here (or a list, with lb). stripPrefix removes a prefix.
redirectRedirect here (301, or set status). Caddy placeholders work.
rootServe static files from this (mounted) directory.
"routes": [
    { "path": "/api/*", "upstream": [ "http://api1:8080", "http://api2:8080" ], "lb": "round_robin", "stripPrefix": "/api" },
    { "path": "/old/*", "redirect": "https://example.com/new{http.request.uri.path}" },
    { "path": "/files/*", "root": "/www/files" }
]
⁠Defaults

For every domain:

  • HTTP is redirected to HTTPS, and certificates are requested and renewed automatically.
  • Responses are compressed with zstd or gzip. Images and other compressed formats, responses under 512 bytes and responses the upstream has already compressed are left alone.
  • Strict-Transport-Security, X-Frame-Options, X-Content-Type-Options and Referrer-Policy are added, and X-Clacks-Overhead: GNU Terry Pratchett - because he was a legend⁠. To stop one being added, set it to null in the domain's headers - e.g. "X-Frame-Options": null for Vaultwarden, which sets it itself.
  • X-Real-IP and Caddy's X-Forwarded-* headers are sent upstream. WebSockets work automatically.
  • If the upstream can't be reached (after retry, so a quick restart isn't noticed), or returns 502, 503 or 504, an auto-refreshing maintenance page is shown. It is regenerated on every start, so don't edit /www/public/maintenance.html. index.html is only created when /www/public doesn't exist, so your own is kept.
  • Requests for unknown hosts are redirected to BF_PROXY_DOMAIN, which serves /www/public (static files cached for a year, /favicon.ico returning 204 unless you add one).
⁠AI crawlers

With BF_PROXY_BLOCK_AI_BOTS=1, crawlers that collect AI training data get 403 Forbidden. The list comes from ai.robots.txt⁠ when the image is built, keeping only crawlers whose function is training data collection - AI search crawlers, assistants and user-triggered agents (e.g. OAI-SearchBot, ChatGPT-User) are not blocked. It is saved to /usr/share/caddy-proxy/ai-bots.txt.

⁠Custom domain configuration

Each domain's Caddy configuration is generated into /sites/<primary>.json: its route, its errors route (the maintenance page) and its tls policy (including the ACME challenge). The file is regenerated on every start and by proxy-regenerate, so don't edit it. Instead, either add your own routes to it, or take it over with custom.

⁠Adding your own routes
  1. Create the folder /sites/<primary>.d, e.g. /sites/app.example.com.d.
  2. Add *.json files to it, each holding a Caddy route⁠ or an array of routes.
  3. Run proxy-regenerate.

Your routes go after the security headers and basic auth, so they are protected like the rest of the site, and before routes and the default upstream.

A file ending .before.json goes first instead - before alias redirects, AI bot blocking, security headers, compression and basic auth. Use it only for a route that must not be protected, e.g. a public /health endpoint on a site that otherwise needs a login. A warning is logged when one is loaded.

Files load in natural sort order, so use lower case names with a two-digit prefix, e.g. 10-health.before.json, 20-api.json. Nginx *.conf files from nginx-proxy are ignored, with a warning.

⁠Taking complete control

Set "custom": true on the domain. Its file is generated once if it doesn't exist, then left alone, so you can edit it. To regenerate it from conf.json, run proxy-regenerate -d <primary> -f.

⁠DNS challenge

Use the DNS challenge for wildcard aliases, and for servers that are not reachable from the internet (e.g. only over a VPN).

  1. In the Hetzner Console project that holds your zones, create an API token with read / write permission (the module uses the Cloud DNS API).

  2. Set BF_PROXY_HETZNER_TOKEN to the token - see keeping the token out of your compose file⁠. It is never written to the generated configuration: Caddy reads it from the environment.

  3. Set BF_PROXY_ACME_CHALLENGE=dns, or "challenge": "dns" on each domain.

  4. Optionally, set BF_PROXY_DNS_TTL=60s (Hetzner's lowest), so old challenge values aren't cached for long.

After creating each record, Caddy waits BF_PROXY_DNS_PROPAGATION_DELAY (60 seconds) before asking Let's Encrypt to check it. Without the wait, Let's Encrypt may ask a Hetzner nameserver that doesn't have the record yet, and the "not found" answer is cached, so retries fail too. (Caddy's own check passes as soon as one nameserver has the record, which isn't enough.) The wait only happens when a certificate is issued or renewed, and certificates are requested in parallel, so the waits overlap.

⁠Domains hosted with another DNS provider

The Hetzner token can only create records in zones Hetzner hosts. For a domain hosted anywhere else, point its _acme-challenge records at a zone Hetzner does host, and tell Caddy to create the records there:

  1. In the token's Hetzner project, create a zone for the challenges, e.g. acme.example.net. It can be a separate domain using Hetzner's nameservers, or a subdomain of yours delegated to Hetzner with NS records.
  2. Set BF_PROXY_DNS_OVERRIDE_ZONE=acme.example.net.
  3. At the other DNS provider, add a CNAME record for every host name, as below.

Each domain in conf.json gets one name in the zone - its primary, e.g. send.example.com.acme.example.net - and every host name in the domain points to it, aliases included. With BF_PROXY_ACME_CHALLENGE=dns the proxy domain needs one too.

For example, with BF_PROXY_DOMAIN=proxy.example.com and this domain:

{ "primary": "send.example.com", "aliases": ["www.send.example.com", "*.files.example.com"], "upstream": "http://app:80" }
CNAME recordValue
_acme-challenge.proxy.example.comproxy.example.com.acme.example.net.
_acme-challenge.send.example.comsend.example.com.acme.example.net.
_acme-challenge.www.send.example.comsend.example.com.acme.example.net.
_acme-challenge.files.example.comsend.example.com.acme.example.net.
  • Record: _acme-challenge. then the host name, without the *. for a wildcard.
  • Value: the domain's primary, then the zone - the same for every host name in the domain.

Per domain, "dnsOverrideDomain" changes this:

  • "" - for a domain Hetzner hosts directly. It needs no CNAME records. (The proxy domain always uses the zone while it is set.)
  • A full name, e.g. send.acme.example.net - use that name instead of <primary>.<zone>.

A wildcard and the same name without *. (e.g. *.files.example.com and files.example.com) share one CNAME record, so they can't point to different names - put them in the same domain.

Check the records before requesting certificates:

dig +short CNAME _acme-challenge.send.example.com   # send.example.com.acme.example.net.
dig +short NS acme.example.net                      # Hetzner's nameservers

Or run proxy-dns, which looks up public DNS (using Google's DNS-over-HTTPS, as Let's Encrypt sees it) for every host name, including the proxy domain, and checks:

  • http challenge - the host has A or AAAA records. It warns if any address is not one of the proxy domain's, as Let's Encrypt may use any of them (it prefers IPv6).
  • dns challenge - _acme-challenge.<host> is a CNAME to the override domain when one is set, and is not a CNAME when it isn't.
  • CAA - if the host or a parent has CAA records, they allow letsencrypt.org (issuewild for wildcards).

It does nothing when BF_PROXY_USE_INTERNAL_CA=1, and exits 1 if anything would stop a certificate being issued.

⁠Automatic reload

Run proxy-regenerate after changing conf.json, users.json or files in /sites, or set BF_PROXY_AUTO_RELOAD=1 to do it automatically. Files are checked every 5 seconds, and once they have been unchanged for 5 seconds configuration is regenerated and Caddy reloads without dropping connections.

If the new configuration is not valid, the errors are written to the container log and Caddy keeps running with the current configuration.

⁠Helper commands

Run these with docker exec <container> <command>, e.g. docker exec proxy proxy-certs.

CommandDescription
proxy-regenerateRegenerate configuration from conf.json and reload Caddy.
proxy-regenerate -d <primary>Only regenerate that domain's file.
proxy-regenerate -d <primary> -fRegenerate that domain's file, even with "custom": true.
proxy-reloadValidate the current configuration and reload Caddy.
proxy-validateValidate conf.json and the current Caddy configuration.
proxy-certsList certificates and days left (flagged at 14 or fewer).
proxy-renew -aDelete all certificates and restart Caddy, so it requests new ones.
proxy-renew -d <primary>The same, for one domain (its primary and aliases).
proxy-pemWrite the PEM files⁠ now.
proxy-cleanupList domains no longer in conf.json - deletes nothing.
proxy-cleanup -lDelete those domains' configuration and certificates.
proxy-tokenCheck the Hetzner token works and can manage your zones.
proxy-dnsCheck the public DNS records each host name needs (see above).
proxy-adduser <username> <password>Add a basic auth user, or change their password.
proxy-deluser <username>Remove a basic auth user.

Passwords are stored as bcrypt hashes, but proxy-adduser takes the password as an argument, so it may be saved in your shell history.

⁠Migrating from nginx-proxy

  1. Copy /ssl/conf.json to the new /ssl volume - it is converted on start-up. The original is kept as /ssl/conf.json.nginx-proxy, and anything that can't be converted is reported in the log.
  2. Change the image to quay.io/bfren/caddy-proxy and rename variables from PROXY_* to BF_PROXY_* (old names are ignored). These are no longer used: PROXY_ENABLE_AUTO_UPDATE (Caddy renews automatically), PROXY_ENABLE_NAXSI, PROXY_SSL_KEY_BITS, PROXY_SSL_DHPARAM_BITS, PROXY_GETSSL_SKIP_HTTP_TOKEN_CHECK and PROXY_UPSTREAM_DNS_RESOLVER.
  3. Custom Nginx configuration in /sites can't be converted - rewrite it as Caddy JSON in /sites/<primary>.d.
  4. Existing certificates are not reused. Caddy requests one certificate per host name (getssl used one per primary domain), so check Let's Encrypt rate limits⁠ if you have many, and keep BF_PROXY_LETS_ENCRYPT_LIVE=0 until everything works.
  5. nginx-adduser is replaced by proxy-adduser -

Tag summary

Content type

Image

Digest

sha256:485765903…

Size

43.4 MB

Last updated

1 day ago

docker pull bfren/caddy-proxy