Cache Busting and Include Files for Nginx

Posted by Vince Kronlein on Server Fault See other posts from Server Fault or by Vince Kronlein
Published on 2013-11-03T02:20:28Z Indexed on 2013/11/03 3:56 UTC
Read the original article Hit count: 484

Filed under:
|
|

In Apache you can use the following to cache bust css and js files and server them as a single file with Apache's Include mod:

<FilesMatch "\.combined\.js$">
  Options +Includes
  AddOutputFilterByType INCLUDES application/javascript application/json
  SetOutputFilter INCLUDES
</FilesMatch>
<FilesMatch "\.combined\.css$">
  Options +Includes
  AddOutputFilterByType INCLUDES text/css
  SetOutputFilter INCLUDES
</FilesMatch>
<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
</IfModule>

I know this is possible with nginx but I can't seem to get the syntax correct.

-- EDIT -- Adding some code

The only piece I have thus far is:

location ~* (.+)\.(?:\d+)\.(js|css)$ {
    ssi on;
    try_files $uri $1.$2;
}

What I'm looking for is to be able to combine all js and css files into single files using the combined keyword with a number for cache busting:

style.combined.100.css
javascript.combined.100.js

© Server Fault or respective owner

Related posts about apache2

Related posts about nginx