Static sites under Caddy are the easiest possible Caddy deployment — a Caddyfile of three lines and you have HTTPS, compression, and proper MIME types. But “the easiest” still has production decisions: which files are indexed, which are hidden, what cache headers go where, what happens on a missing path. This article is the configuration we deploy at clients for documentation sites, SPA frontends, and asset CDNs running on Caddy.
How to verify
A static site is well-configured when these all return the right thing:
curl -I https://docs.example.com/
curl -I https://docs.example.com/missing-page
curl -I https://docs.example.com/assets/style.css
curl -H 'Accept-Encoding: gzip, zstd, br' -I https://docs.example.com/
ls -la /var/www/docs.example.com/
The root returns 200 with Content-Type: text/html. A missing page returns 404 (not 200 with the index file, unless it is an SPA). The CSS asset returns 200 with Content-Type: text/css and a Cache-Control header. The compressed request shows Content-Encoding: zstd or gzip for compressible types.
What’s happening
The file_server directive is the static handler. By default it serves files from the configured root directory, returns 404 for missing files, returns directory listings only if explicitly enabled, and applies MIME types from Go’s mime package (which reads /etc/mime.types plus a built-in default set).
The handler chain matters. A file_server block alone serves the file if it exists or returns 404. An SPA wants the 404 to fall back to index.html instead — that is what the try_files directive does. Compression is a separate encode directive that wraps the response handler. Cache headers are set by a header directive that runs before file_server writes the response.
Putting all of these together is the difference between “the site works” and “the site is well-served.” Caddy’s defaults are sane but not assertive — for example, no Cache-Control is set on static assets unless you ask for it.
The procedure
-
The minimal static site. Single document root, no extras:
docs.example.com { root * /var/www/docs.example.com file_server }That serves files from
/var/www/docs.example.com/, returningindex.htmlfor/requests if it exists, otherwise a 404. HTTPS is provisioned automatically. -
Add compression.
encoderuns beforefile_server, compressing the response when the client accepts it:docs.example.com { root * /var/www/docs.example.com encode zstd gzip file_server }Order matters: zstd first for clients that support it (modern browsers, Caddy 2.7+), gzip as a fallback. Add
br(Brotli) if you compiled Caddy with thecaddy-brotliplugin — see caddy brotli compression. -
Cache headers per file type. Static asset routes should advertise long cache lifetimes; HTML should not:
docs.example.com { root * /var/www/docs.example.com @assets { path *.css *.js *.woff2 *.png *.jpg *.svg *.webp } header @assets Cache-Control "public, max-age=31536000, immutable" @html { path *.html } header @html Cache-Control "public, max-age=60, must-revalidate" encode zstd gzip file_server }The
@namematchers are reusable filters — Caddy applies the matchingheaderonly to requests for paths matching the pattern. -
SPA mode — fall back to index.html on miss. For a Vue/React/Svelte SPA where the client-side router owns paths that do not exist on disk:
app.example.com { root * /var/www/app.example.com encode zstd gzip try_files {path} /index.html file_server }try_filesrewrites the request internally. If/some/routedoes not exist on disk, the internal URI becomes/index.html, which exists, andfile_serverserves it. The browser still sees/some/routein the URL bar. -
Hidden files and directory traversal protection. Caddy refuses path traversal (
../) and refuses to follow symlinks out of the root by default. Hide dotfiles and source files explicitly:docs.example.com { root * /var/www/docs.example.com file_server { hide .git .env .htaccess *.bak } }The
hidedirective treats listed paths as if they do not exist — 404 instead of a 403 with a discoverable filename. -
Directory listings, intentionally. When you genuinely want browsable directories (asset mirrors, package repos):
files.example.com { root * /srv/files file_server browse { index off } }browseenables the HTML index.index offdisables the automaticindex.htmllookup so every directory shows the listing — useful for asset mirrors. -
Per-host roots from one server block. When you serve multiple sites from the same Caddy instance with one config:
*.example.com { @docs host docs.example.com handle @docs { root * /var/www/docs.example.com file_server } @blog host blog.example.com handle @blog { root * /var/www/blog.example.com file_server } }The single TLS certificate covers the wildcard if you have DNS-01 wired — see caddy ACME DNS challenge.
Common pitfalls
- Forgetting
encodemeans assets ship uncompressed, which is fine for a 5KB CSS file and a 50% bandwidth bill for a 500KB JS bundle. Always includeencode zstd gzipfor any site with non-trivial assets. - The default MIME types from Go’s package may be wrong for newer formats (e.g.,
application/wasmwas missing in older Caddy releases). If a WASM module won’t execute, check theContent-TypeCaddy sends and override with@wasm+header Content-Type application/wasm. try_files {path} /index.htmlfor SPA mode is a 200 — Google Search Console will eventually flag this as “soft 404.” Add anerror 404for paths that truly should not match (/api/*,/static/*that miss).file_serverfollows symlinks by default within the root. A symlink at/var/www/docs/secret -> /etc/passwdis served. Usefile_server { follow_symlinks }to opt in andhideto block sensitive names, or move sensitive files entirely off the docroot.- Compression has CPU cost. For a 1MB file requested 10,000 times per minute, the compression overhead is real — enable
precompressedto serve pre-compressed.gzor.zstfiles from disk instead of compressing per request.
Stack Harbor runs static-site fleets on Caddy for clients who got tired of CDN configuration as the cost of “uploading some HTML” — origin under config management, cache headers and compression tuned to the asset mix, and HTTPS state monitored. If you have a static site or a documentation portal you want hosted with operational rigor without the CDN tax, this is the kind of work covered by our Managed Operations practice.