Featured image of post How to set "Cache-Control" Header for static files in Caddy

How to set "Cache-Control" Header for static files in Caddy

If you are seeing the “Serve static assets with an efficient cache policy” notice in PageSpeed, you need to add the Cache-Control header to your website.

Here is how to do it if you are using Caddy.

Cache-Control

To set a Cache-Control header only for your static files like, .js, .jpg, and others, just add this to the relevant part of your Caddyfile(under the site block if the Caddyfile includes multiple websites)

1
2
3
4
5
@static {
  file
  path *.ico *.css *.js *.gif *.webp *.avif *.jpg *.jpeg *.png *.svg *.woff *.woff2
}
header @static Cache-Control max-age=5184000

The files are going to be cached for 5184000 seconds, so 60 days, you can change it if you want.

Source

https://caddy.community/t/correct-way-to-set-expires-on-caddy-2/7914/13