Skip to content

Caddy Brotli compression — when and how to enable it

Adding Brotli compression to Caddy via the caddy-brotli plugin — when it helps, when it does not, and the operational notes an MSP records about CPU cost and pre-compression.

Brotli is a compression algorithm developed at Google that produces smaller output than gzip for text payloads — typically 15–25% smaller HTML, 20–30% smaller JS, 10–15% smaller CSS — at the cost of higher CPU per byte compressed. Caddy supports gzip and zstd in the stock binary; Brotli requires the caddy-brotli plugin. This article is when we add it for clients, when we don’t, and how to deploy it without paying the CPU cost on every request.

How to verify

Whether your Caddy speaks Brotli, and what it actually serves:

caddy list-modules | grep -i brotli
curl -H 'Accept-Encoding: br' -I https://docs.example.com/
curl -H 'Accept-Encoding: br' -s https://docs.example.com/ | wc -c
curl -H 'Accept-Encoding: gzip' -s https://docs.example.com/ | wc -c
ls -la /var/www/docs.example.com/index.html*

If list-modules shows http.encoders.br, Brotli is compiled in. The two curl comparisons show the actual bytes-on-wire difference. The ls reveals whether precompressed .br files exist alongside the originals.

What’s happening

Caddy’s encode directive is the compression handler. It looks at the client’s Accept-Encoding header, picks the first algorithm the client supports that Caddy can produce, and wraps the response body in that encoding. The supported algorithms from the stock binary are zstd (Zstandard) and gzip. Brotli requires the plugin.

The compression happens per-request unless precompressed is enabled, in which case Caddy serves a .br file from disk if one exists next to the original, falling back to live compression if not. The pre-compressed path is the fast one — no CPU spent at request time, just sendfile of the right artifact.

Brotli’s headline win is on the wire: a 100KB JS bundle compresses to ~28KB with gzip and ~22KB with Brotli at the same effort level. Brotli at maximum quality (level 11) produces even smaller output, but the CPU cost is huge — typically 4-10x gzip at the same input size, which is why almost no one runs Brotli live at maximum quality. The practical pattern is level 6 (default) for live compression and level 11 for pre-compression.

zstd is the dark horse. Caddy ships it in the stock binary. Compression ratios are between gzip and Brotli; CPU cost is roughly equivalent to gzip. Most modern browsers support zstd over HTTPS as of late 2024. For new deployments where you control the stack, zstd is often the right answer.

The procedure

  1. Build with the Brotli plugin. Stock Caddy does not include Brotli:

    xcaddy build \
      --with github.com/ueffel/caddy-brotli \
      --output /usr/local/bin/caddy
    sudo systemctl restart caddy

    See xcaddy custom builds for the build mechanics.

  2. Enable in the encode directive. Order matters — first algorithm the client accepts wins:

    docs.example.com {
        root * /var/www/docs.example.com
        encode zstd br gzip
        file_server
    }

    For modern browsers that send Accept-Encoding: gzip, deflate, br, zstd, Caddy picks zstd (best ratio, low CPU). Older clients without zstd get br. Anything else gets gzip.

  3. Filter by content type. Compressing already-compressed media (JPEG, MP4, ZIP) wastes CPU. The encode directive accepts a match block to filter:

    docs.example.com {
        root * /var/www/docs.example.com
        encode {
            zstd
            br
            gzip
            match {
                header Content-Type text/*
                header Content-Type application/json*
                header Content-Type application/javascript*
                header Content-Type application/xml*
                header Content-Type image/svg+xml*
            }
        }
        file_server
    }

    The match block restricts compression to text-like content types. Caddy applies a similar built-in filter by default; the explicit list is for cases where the default does not match a custom content type.

  4. Pre-compress static assets. Generate .br (and .gz, .zst) files at build time, serve them with precompressed:

    # in your build pipeline
    find /var/www/docs.example.com -type f \( -name '*.html' -o -name '*.css' -o -name '*.js' \) | while read f; do
        brotli -q 11 -k "$f"   # writes f.br
        gzip -9 -k "$f"        # writes f.gz
        zstd -19 -q "$f" -o "$f.zst"
    done
    docs.example.com {
        root * /var/www/docs.example.com
        file_server {
            precompressed br zstd gzip
        }
    }

    At request time Caddy looks for index.html.br first (if the client accepts br), then .zst, then .gz, then the bare index.html. The compressed file ships with the matching Content-Encoding header. No CPU spent on compression at request time — just disk read.

  5. Pick a quality level. The encode directive accepts a per-algorithm config:

    encode {
        zstd 3
        br 4
        gzip 5
        minimum_length 1024
    }

    Quality 4 for Brotli is a sane live-compression default — better than gzip-9 ratio at roughly gzip-6 CPU cost. minimum_length skips compression for tiny responses (smaller than the gzip header overhead).

Operational notes

  • Live compression of large files is a CPU hog. For a 5MB HTML response compressed with Brotli level 11 per request, you can saturate a CPU core with a handful of concurrent requests. Always cap quality at 6 or below for live compression, or pre-compress.
  • The precompressed mode reads file existence on each request. For directories with thousands of files, the filesystem stat overhead is real — keep the precompressed set bounded to the actual hot assets.
  • Brotli requires HTTPS for browser support — Accept-Encoding: br is only sent over secure connections. HTTP traffic falls back to gzip. With Caddy automatic HTTPS, this is rarely a real constraint.
  • A CDN in front of Caddy (CloudFront, Fastly, Cloudflare) may negotiate its own compression with the client and decompress what Caddy sends. Either let the CDN do the compression (set Caddy to not compress) or pre-compress and have the CDN pass through.
  • The caddy-brotli plugin’s quality range is 0-11. Levels 10 and 11 are dramatically slower than 4-9 with diminishing returns. Profile before picking — for many corpora the ratio gain from 9 to 11 is under 5% for 5x the CPU.

Stack Harbor configures compression for client edges as a workload-fitted choice — Brotli pre-compression for large static asset directories, zstd live compression for dynamic JSON APIs, gzip live for legacy clients — with the CPU cost monitored alongside hit rates. If you have an edge whose bandwidth bill is the line item you want trimmed, this is the kind of work covered by our Managed Operations practice.