Struggling a bit with the following.
Adding expires for JS and CSS, plus other static files is easy:
# Directives to send expires headers.
location ~* \.(?:css|js)$ {
expires 28d;
access_log off;
add_header Cache-Control "public";
}
But for my fancy URLs I'm not getting it right. Rewritten URLs look like this:
www.domain.tld/post-name
So no extension like .html or .php, and the homepage is just a / Note my homepage is static, no latest news/blog posts etc. so I also wish for this to be cached in the browser.
Adding the expires to this location block doesn't add browser cache to the homepage:
location / {
try_files $uri $uri/ /index.php?$args;
expires 1d;
}
Adding expires on the php file location (before the rewrite) doesn't work either, Nginx seems to want the exact requested URI.
I've seen this suggested, but am wary of using too many if statements:
location / {
if (-f $request_filename) {
expires 30d;
break;
}
}
What is the best approach to ensure a simple 5 page site like this gets all its pages browser cached? Ideally without hard-coding the URLs.
www.domain.tld/ www.domain.tld/our-products www.domain.tld/our-services www.domain.tld/about-us www.domain.tld/contact
Is it perhaps possible to add expires by mime type instead of extension like we do with gzip?
mime_type text/html
expires 1d;
Any tips in the right direction would be welcome, thanks.
0 Answers