Welcome! Log In Create A New Profile

Advanced

Stop caching images on a rewrite with NGINX

Posted by Callumpy 
Stop caching images on a rewrite with NGINX
October 26, 2013 05:32PM
Hey, I posted for help on stackoverflow quite a while back and got no reply, so i'm hoping some of you guys can help me out. You can see my post here: http://stackoverflow.com/questions/19434656/stop-caching-images-on-a-rewrite-with-nginx

On my site I am using PHP to generate images with different options for styles.

The images are requested like this: "/img/image.php?user=NAME&style=STYLE"

So I have a rewrite to get the images, this is written as follows:
rewrite ^/image/([a-zA-Z0-9_+]+)/([a-zA-Z0-9+]+).png$ /img/image.php?style=$1&user=$2;

This works fine, except when my caching is uncommented in the NGINX config, it stops me from accessing the images from the rewrite. I am still able to access them without the rewrite though. Its a necessity that I have the '.png' on the end of the rewrite also.

Here is my cache setup:
location ~* \.(jpg|jpeg|png|gif|ico|css|xml|js|woff)$ {
expires 720h;
root /home/site/public;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

Any information on how to fix this would be highly appreciated.
Re: Stop caching images on a rewrite with NGINX
October 26, 2013 07:25PM
I randomly found a solution.

I used the following:

location ^~ /image {
rewrite ^/image/([a-zA-Z0-9_+]+)/([a-zA-Z0-9+]+)\.png$ /img/image.php?style=$1&user=$2 last;
expires epoch;
}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 142
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready