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.
Copyright statement:
- All content that is not sourced is original., please do not reprint without authorization (because the typesetting is often disordered after reprinting, the content is uncontrollable, and cannot be continuously updated, etc.);
- For non-profit purposes, to deduce any content of this blog, please give the relevant webpage address of this site in the form of 'source of original text' or 'reference link' (for the convenience of readers).