I want a long expiring Cache-Control header for JavaScript files. I am not sure if I’ve misconfigured something, or how to figure out only setting this header on files that are found. It seems like I can’t control setting the header only if the file exists, and I am hoping someone can help me figure it out?
When I make a request to a file that doesn’t exist, I still get the “Cache-Control” and “Expires” headers back. Actually I think “Expires” was redundant in modern browsers:
curl -I https://example.com/js/foo.js
HTTP/1.1 404 Not Found
Date: Fri, 04 Aug 2017 22:45:55 GMT
Content-Type: text/plain; charset=utf-8
Connection: keep-alive
Cache-Control: public, max-age=31536000
Expires: Sat, 04 Aug 2018 21:57:35 GMT
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
CF-Cache-Status: HIT
Server: cloudflare-nginx
CF-RAY: 38950cbe2e3139b2-PHX
Here is my Caddyfile (I am running this in Docker and terminating SSL):
Those header entries don’t care whether or not Caddy actually returned a file, it will just set that header for any requests matching /css or /js.
Try without them - I think your expires directive should handle .js and .css files already (although that regex looks strange to me without round braces around the grouping).
Thanks for the info, how can I only conditionally set the Cache-Control headers when a file exists? Browsers will use Cache-Control, I am setting the Expires only in case a proxy uses it for some reason.
Also, you mentioned the regex looks strange, how would you recommend I format the regex differently?
I don’t believe you can currently configure Caddy to validate the existance of a file before setting headers in this manner. If it’s a problem, I’d suggest going to the issues page of the Github repo and submitting a feature request.
As for the regex, I’m used to seeing match groups written in round brackets, like: (.css|.js)$