Serving Static Files
Caddy’s file server is a production-ready, high-performance module for serving static files. It includes automatic MIME type detection, directory browsing, precompressed file support, and more.Basic File Server
The simplest way to serve files from a directory:Configuration Options
Site Root
Theroot directive specifies where your files are located. By default, it uses {http.vars.root} if set, or the current working directory.
The site root is not a sandbox. Files and symlinks within the root can be accessed directly based on the request path.
Custom Index Files
By default, Caddy looks forindex.html and index.txt. You can customize this:
Directory Browsing
Enable directory browsing to show file listings when no index file is present:Browse Sorting Options
- sort_by:
name(default),namedirfirst,size,time - order:
asc(default),desc
JSON API
When theAccept header includes application/json, the browse endpoint returns JSON:
Hiding Files
Prevent certain files from being served:Precompressed Files
Serve precompressed.br, .zstd, or .gz files automatically:
Advanced Features
Pass-Through Mode
If a file is not found, invoke the next handler instead of returning 404:Custom Status Codes
Override the response status code (useful for custom error pages):ETag Headers
Caddy automatically generates ETags using file modification time and size. You can also read ETags from sidecar files:Canonical URIs
By default, Caddy enforces trailing slashes for directories. Disable this:Performance Considerations
The file server is zero-allocation and highly optimized. It properly handles:
- Range requests (for video streaming)
- If-Modified-Since / If-None-Match (304 responses)
- Content-Type based on file extension
- Automatic ETag generation
Security
Windows-Specific Protection
On Windows, Caddy automatically rejects:- Alternate Data Streams (ADS) paths containing
: - 8.3 short name paths containing
~
Complete Example
Related Modules
- templates: Process HTML templates before serving
- encode: Compress responses on-the-fly
- rewrite: Modify request paths before serving files