Correct way to set Expires on Caddy 2?

What will be the correct way to set files expires on Caddy 2 file. As for example on NGINX I used to keep:
location ~* .(?:ico|css|js|gif|jpg|jpeg|png|svg|woff)$ {
expires 1w;
access_log off;
add_header Cache-Control “public, max-age=604800”;
}
In the same way, what will be that for Caddy 2? Will something like:
header *.js Cache-Control “max-age=2592000”
will work?
I need to set expires for all fonts, js, css, and images.
Please help me.
Thank you!!

Hey! You need to read up on named matchers and in particular on path_regexp. After that yeah, you just do header @your_named_matcher Cache-Control max-age= 2592000.

I am not that expert on Caddy. Please give me example code that is working on yours. Thanks.

Seems like these one’s worked on my side:
header *.js Cache-Control max-age=5184000
header *.css Cache-Control max-age=5184000
header *.jpg Cache-Control max-age=5184000
header *.png Cache-Control max-age=5184000
header *.ico Cache-Control max-age=5184000
header *.svg Cache-Control max-age=5184000
header *.gif Cache-Control max-age=5184000
header *.ttf Cache-Control max-age=5184000
header *.eot Cache-Control max-age=5184000
header *.woff Cache-Control max-age=5184000
header *.woff2 Cache-Control max-age=5184000

They are valid as I checked. But, I am looking for one line code.

I think its still not resolved. I will be waiting for somebody expert to help resolve this please.

if you have a chance to put everything in a subdirectory, (for example a directory called assets)

you can give header /assets/* Cache-Control ...

or you can use header_regexp i guess.

@tweeniev2 for example?

i don’t know much about regular expressions :smiley:

maybe @matt answers your question.

@static {
  path_regexp \.(ico|css|js|gif|jpg|jpeg|png|svg|woff)$
}
header @static Cache-Control max-age=5184000

As I said, you need to read up on matchers.

4 Likes

@piranha thank you very much. U r awesome!!

noted for future.

A more optimal variant (avoiding regex in favour of substring checks) would be as follows:

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

Note especially that I have added the file matcher to @static - this ensures the file exists on disk before Caddy will send the Cache-Control header. No point caching a 404!

6 Likes

That’s beautiful.

Yes, yesterday I tried a lot with the previous one but it didn’t worked for my WordPress website. I have now changed that to this new one. Hope it works.

it didn’t worked

I think the regex in the first example might have been slightly off, too (cc @piranha):

/\.(ico|css|js|gif|jpg|jpeg|png|svg|woff)$/

This would strictly match /.css but not /foo.css. (I threw it on https://regexr.com/ to double check)

My example will match /foo.css, or /.css, or /foo/bar/test.css. It just has to end in .css (or any of the other extensions).

1 Like

Argh, that’s my nginx habits speaking up. Those slashes are for indicating a refular expression to nginx. They need to be removed. :slight_smile:

I like path matching more though, it’s just when you know regular expressions, everything looks like a target for them.

1 Like

@Whitestrake your one worked on mine! This problem is fully resolved now. Thank you!!

1 Like

You could create a Wiki entry for this, I think it would be useful ! :+1:

I have already created a snippet with it, to use it on all websites : voiretmanger.fr/Caddyfile at main · nicolinuxfr/voiretmanger.fr · GitHub

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