V2: Cache-Cotrol for static files using RegEx | replace for Caddy 1 expire directive

1. My Caddy version (caddy version):

2.0 RC1

3. The problem I’m having:

Is it possible with Caddy 2 to assign cache control headers based on file extension?
With Caddy 1 it was possible usign the expire plugin.

expires {
        match (jpg|jpeg|png|gif|ico)$ 30d
}

but it is normal functionality eg. for .htaccess

<filesMatch ".(jpg|jpeg|png|gif|ico)$">
   Header set Cache-Control "max-age=31536000, public"
</filesMatch>

I am looking for sollution using the Caddyfile, as the simplicity of it is the main reason why I (and I suppose many others) are interested in caddy.

5. What I already tried:

I discovered it is possible to specify system path…

header  /assets/* {
        Cache-Control "public, max-age=604800, must-revalidate"
    }

…hovewer lot of my images are linked to individual pages, so this option is not viable.

Yep, you’re looking for matchers!

This might look like this:

@cachedFiles {
    path *.jpg *.jpeg *.png *.gif *.ico
}
header @cachedFiles Cache-Control "public, max-age=604800, must-revalidate"

Or you can use the path_regexp matcher if you prefer as well

5 Likes

Thanks. Exactly what i was looking for.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.