Setting Static File Cache with Caddy

Using Google PageSpeed Insights When testing this site, the diagnostic results indicate that static file caching is not configured. In particular, downloading the woff2 font is taking a long time, impacting loading speed. Since this site uses the Caddy2 server, static file caching needs to be enabled. The configuration file syntax differs slightly from that of Nginx.

Example of setting the static file cache duration in Caddy2:

mephisto.cc { encode gzip zstd root * /data/mephisto.cc/public file_server

@static { file path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.woff2 *.webp } header @static Cache-Control max-age=604800

reverse_proxy /xray 127.0.0.1:2001 { transport http { versions h2c } }

handle_errors { rewrite * /{http.error.status_code}.html file_server }

log { output file /var/log/caddy/mephisto.cc.log { roll_size 100mb roll_keep 10 roll_keep_for 7d } }

@static is officially called a named matcher in the documentation. The following section states that if the request path matches the following static files, the cache is considered expired by controlling the return header. If the cache exceeds 3600 x 24 x 7 seconds (604800 seconds), the cache is considered expired. Unlike Expires, this time is relative to the request time.

@static { file path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.woff2 *.webp } header @static Cache-Control max-age=604800

Other header control options can be added as needed.

Lastmod: Friday, August 8, 2025

Translations: