2

I want to easily clear the browser's cache when js/css files are updated.

Real file location:

  • /js/some-files.js
  • /css/some-files.css

File location set in the app to be called by browser:

  • /js/v1/some-files.js
  • /css/v1/some-files.css

I tried the following but going to /js/v1/some-files.js gives me 404s.

location ~ ^/(js|css)/v[0-9]+/(.*)$ {
    access_log off;
    expires 7d;
    add_header Cache-Control public;
    try_files $uri $uri/ $1/$2;
}

Can you spot what I'm doing wrong?

3
  • 1
    It's probably the try_files directive that should probably read like... try_files $uri $uri/ /$1/$2; Sep 14, 2013 at 6:37
  • Well that was easy! It was that missing slash!
    – iDev247
    Sep 14, 2013 at 7:03
  • Glad to know it works. Posting it as answer, if you wish to upvote or select it as the best answer. ;-) Sep 14, 2013 at 7:12

1 Answer 1

2

It was due to missing slash, as mentioned in the comments! Additionally, your location block can be simplified like the following...

location ~ /(js|css)/v[0-9]+/(.*) {
    access_log off;
    expires 7d;
    add_header Cache-Control public;
    try_files $uri $uri/ /$1/$2;
}
3
  • Thanks for the hint (the missing slash)! Looks like the ~ is however still required to trigger regular expression.
    – iDev247
    Sep 14, 2013 at 7:24
  • This issue also deviated to another issue… if you wish please check out my other quesiton
    – iDev247
    Sep 14, 2013 at 7:37
  • You're right. I've fixed it. Will look into the other issue now. Sep 14, 2013 at 8:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .