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):
0.0.0.0
root /srv/app/public
browse
gzip
fastcgi / 127.0.0.1:9000 php
rewrite {
if {path} not_starts_with /api/v1
if {path} not_starts_with /api/latest
ext /
to /index.php?{query}
}
proxy /api/latest {$API_BASE_URL} {
transparent
}
proxy /api/v1 {$API_BASE_URL} {
transparent
}
expires {
match .css|.js$ 1y
}
# Remove the `Server: Caddy` header
header / -Server
# Long JavaScript expiry (1 year)
header /js {
Cache-Control "public, max-age=31536000"
}
# Long CSS expiry (1 year)
header /css {
Cache-Control "public, max-age=31536000"
}
log stdout
errors stdout
startup php-fpm &