Mobile app version of vmapp.org
Login or Join
Bryan171

: Nginx Rewrite causing problems I wrote a little php script that reads and compresses css and javascript files. I used a rewrite to automatically redirect all resources to that script. The rule

@Bryan171

Posted in: #Nginx

I wrote a little php script that reads and compresses css and javascript files. I used a rewrite to automatically redirect all resources to that script. The rule looks like this:

rewrite ^(/.*.(css|js))$ /compress.php?file=&type=;


However this somehow disables these settings:

location ~* .(css|js|gif|jpe?g|png|ico)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}


How do I get Nginx to still use these settings?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Bryan171

1 Comments

Sorted by latest first Latest Oldest Best

 

@Dunderdale272

You can try to create a separate block for the css / js files, and include the rewrite directive inside that directive, like this:

location ~* (?<filename>.+.(?<type>css|js))$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
rewrite ^ /compress.php?file=$filename&type=$type;
}


Here I use regex captures on the location directive, and capture to named variables. It is just a matter of taste how you do the captures though.

And then reduce the other block to:

location ~* .(?:gif|jpe?g|png|ico)$ {


I use ?: prefix here to indicate that we don't need to make any regex captures.

If that doesn't work, then you have to set the cache headers inside your compress.php script with header() function.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme