1

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.

1
  • Have you tried adding the expires directive directly to the server block? If you have no non-cacheable pages, then this should be fine. And what exactly are the headers for your pages with these directives? May 30, 2015 at 15:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.