To do text compression using caddy it as easy as adding encode zstd gzip
to your caddy file.
This website's encoding configuration
encode { zstd gzip }
Out of the box caddy offers zstandard (zstd) and GNU Gzip on the fly compressions.
To use brotli you could either use the caddy-brotli plugin for on-the-fly compressions. To use this plugin with NixOS, take a look at mdleom’s article Installing Caddy plugins in NixOS.
WARNING
Brotli is slower in compression, so you might actually get a performance hit.
Pre compressed files
The encode
option will compress files on-demand, this is resource intensive. Static files might be better served with pre compressed files. Use the precrompressed
option to enable this. It will make caddy search for already compressed files, and when no valid files are found caddy will either serve the file as is or encode it according to your caddy-file.
Brotli is supported natively by caddy for pre compressed files, use the .br
extension to signify caddy that it’s encoded with brotli.