Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

-- SOLUTION --

location / {

    ssi on;
    ssi_types *;

    location ~* \.combined\.js$ {
        ssi_types application/javascript application/json;
    }

    location ~* \.combined\.css$ {
        ssi_types text/css;
    }

    location ~* ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ {
        try_files $uri $1.$3 =404;
    }
}

Not sure how safe having ssi_types *; is ...

share|improve this question
    
If someone's answer helped to arrive at the solution, remember to upvote and / or select it as the best answer! –  Pothi Nov 4 '13 at 1:15

1 Answer 1

up vote 2 down vote accepted

I don't have much experience with the ssi module, but could you give the following a try (more or less a direct rewrite of the Apache configuration).

location / {

    location ~* \.combined\.js$ {
        ssi on;
        ssi_types application/javascript application/json;
    }

    location ~* \.combined\.css$ {
        ssi on;
        ssi_types text/css;
    }

    location ~* ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ {
        try_files $uri $1.$3 =404;
    }

}
share|improve this answer
    
Well the cache busting part works. I'm calling my stylesheet as style.combined.100.css and it is linking to style.combined.css but sadly no ssi working. Still just printing out the includes:<!--# include file="stylesheet.css" --> <!--# include file="megamenu.css" --> etc. –  Vince Kronlein Nov 3 '13 at 17:39
    
Have you checked that your nginx is actually compiled with the SSI module. What's the output of nginx -V? –  Fleshgrinder Nov 3 '13 at 18:13
    
It is. I created a test file and it is enabled. My version is latest stable nginx version: nginx/1.4.3 I wonder does this have to do the mime type of php that's delivered by fastcgi? I put the same ssi block <!--#config timefmt="%A" --> <!--#echo var="DATE_LOCAL" --> into a php file and it doesn't render. I'm thinking that even though the requested link is a .css file, it's being called by a php file so maybe it's not executing because ssi isn't active for the php mime type? Not sure what it is for php on fastcgi. –  Vince Kronlein Nov 3 '13 at 18:21
    
See updated question for solution. –  Vince Kronlein Nov 3 '13 at 18:33
    
This does work, I'm sure there must be a way to get it working without ssi_types * but I can't seem to find the right types to make it work. –  Vince Kronlein Nov 3 '13 at 18:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.