Skip to main content

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

The root 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 for index.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 the Accept header includes application/json, the browse endpoint returns JSON:

Hiding Files

Prevent certain files from being served:
Patterns without a path separator match any file or directory with that name. Use paths like ./hidden to hide only a specific file.

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

Caddy sanitizes paths using Go’s path.Clean() to prevent directory traversal, but files within the root should be trusted. The file server does not implement access control beyond hiding files.

Windows-Specific Protection

On Windows, Caddy automatically rejects:
  • Alternate Data Streams (ADS) paths containing :
  • 8.3 short name paths containing ~
These protections prevent potential file hiding bypasses.

Complete Example

  • templates: Process HTML templates before serving
  • encode: Compress responses on-the-fly
  • rewrite: Modify request paths before serving files