Skip to content

Caddy as a static file server, the production setup

Serving static sites and assets with Caddy — file_server directive, indexes, MIME, cache headers, hidden files, and the operational details an MSP locks down before launch.

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

  1. 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/, returning index.html for / requests if it exists, otherwise a 404. HTTPS is provisioned automatically.

  2. Add compression. encode runs before file_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 the caddy-brotli plugin — see caddy brotli compression.

  3. 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 @name matchers are reusable filters — Caddy applies the matching header only to requests for paths matching the pattern.

  4. 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_files rewrites the request internally. If /some/route does not exist on disk, the internal URI becomes /index.html, which exists, and file_server serves it. The browser still sees /some/route in the URL bar.

  5. 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 hide directive treats listed paths as if they do not exist — 404 instead of a 403 with a discoverable filename.

  6. Directory listings, intentionally. When you genuinely want browsable directories (asset mirrors, package repos):

    files.example.com {
        root * /srv/files
        file_server browse {
            index off
        }
    }

    browse enables the HTML index. index off disables the automatic index.html lookup so every directory shows the listing — useful for asset mirrors.

  7. 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 encode means assets ship uncompressed, which is fine for a 5KB CSS file and a 50% bandwidth bill for a 500KB JS bundle. Always include encode zstd gzip for any site with non-trivial assets.
  • The default MIME types from Go’s package may be wrong for newer formats (e.g., application/wasm was missing in older Caddy releases). If a WASM module won’t execute, check the Content-Type Caddy sends and override with @wasm + header Content-Type application/wasm.
  • try_files {path} /index.html for SPA mode is a 200 — Google Search Console will eventually flag this as “soft 404.” Add an error 404 for paths that truly should not match (/api/*, /static/* that miss).
  • file_server follows symlinks by default within the root. A symlink at /var/www/docs/secret -> /etc/passwd is served. Use file_server { follow_symlinks } to opt in and hide to 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 precompressed to serve pre-compressed .gz or .zst files 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.