FRiCKLE Labs & yo.se pres. ngx_cache_purge

Hello,
ngx_cache_purge is a module which adds ability to purge content from
FastCGI
and proxy caches. This is rewrite of well-tested cache_purge patch
(kindly
funded by yo.se), so hopefully this short introduction is enough :wink:

You can download it at usual place:
http://labs.frickle.com/nginx_ngx_cache_purge/

GitHub repository is available at:

Best regards,
Piotr S. < [email protected] >

Version 1.1 is now available at:
http://labs.frickle.com/nginx_ngx_cache_purge/

GitHub repository is available at:

Changes:
2010-06-08 VERSION 1.1
* Fix compatibility with nginx-0.8.40+.

* Add ability to purge content from uWSGI's cache.

Best regards,
Piotr S. < [email protected] >

Thanks.

My English is not good and please forgive my bad grammer.

I want only .jpg and .gif be cached, so I wrote:

location ~ .*.(jpg|gif)$
{
proxy_cache cache1;
proxy_cache_valid 200 304 15m;
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://backend;
}

location /
{
proxy_pass http://backend;
}

and here comes a problem: you canā€™t purge any more, browser show: 404

Can you help me?

Posted at Nginx Forum:

Hi,
You donā€™t have any purge location, so this canā€™t work. Please add:

location ~ /purge(/.*) {
proxy_cache_purge cache1 $host$1$is_args$args;
}

before ā€œlocation ~ .*.(jpg|gif)$ā€.

Best regards,
Piotr S. < [email protected] >

And, how can I donā€™t cache a path-like url ? i.e., http://www.test.com/
or http://www.test.com/path/

Posted at Nginx Forum:

Hi,
like I said before, you need to put ā€œlocation ~ /purge(/.)" before
"location ~ .
.(jpg|gif)$ā€.

In your current configuration ā€œhttp://www.test.com/purge/1.gifā€ is
served by
ā€œlocation ~ .*.(jpg|gif)$ā€. This is because both regexp locations match
your query and in such case nginx picks the first one from the top.

Best regards,
Piotr S. < [email protected] >

THANK YOU VERY MUCH!

What an simple solution it is.

Posted at Nginx Forum:

Thank you for your reply.

I mean everything is ok before I limited .jpg and .gif to be cached.
So I didnā€™t write all configurations here.
Then, I give the full version before and current for you to help me.

Version Ok

I visited http://www.test.com/1.gif,

and purge it by visiting, http://www.test.com/purge/1.gif

Itā€™s ok, show: Successful purge ā€¦

##########################################################
server {
server_name www.test.com;
listen 80;

    location / {
            proxy_cache cache1;
            proxy_cache_valid 200 304 15m;
            proxy_cache_key $host$uri$is_args$args;
            proxy_pass http://backend;
    }

    location ~ .*\.(php)?$
    {
            proxy_pass http://backend;
    }

    location ~ /purge(/.*)
    {
            proxy_cache_purge    cache1   $host$1$is_args$args;
    }
}

Version not ok

This canā€™t be purged, show: 404

and Iā€™m sure I visited http://www.test.com/1.gif again before I

purge.

Itā€™s in cache indeed, because I can see the picture in my browser

(with F5, and Ctrl+F5) even after I delete 1.gif from that server.
########################################################
server {
server_name www.test.com;
listen 80;

    access_log logs/proxy.log;

    location ~ .*\.(jpg|gif)$ { # I want only jpg/gif to be cached
            proxy_cache cache1;
            proxy_cache_valid 200 304 15m;
            proxy_cache_key $host$uri$is_args$args;
            proxy_pass http://backend;
    }

    location / # All others not cached
    {
            proxy_pass http://backend;
    }

    location ~ /purge(/.*)
    {
            proxy_cache_purge    cache1   $host$1$is_args$args;
    }
}

Thank you, I thought you can repeat this problem easily.

Posted at Nginx Forum:

Version 1.2 is now available at:
http://labs.frickle.com/nginx_ngx_cache_purge/

GitHub repository is available at:

Changes:
2010-08-11 VERSION 1.2
* Fix various build scenarios with disabled upstream modules.
Reported by Johan Bergstroem.

* Add ability to purge content from SCGI's cache.
  Requested by Johan Bergstroem.

Best regards,
Piotr S. < [email protected] >

here is my conf:
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
keys_zone=DIGG:10m inactive=2m;

location /
{
fastcgi_cache DIGG;
fastcgi_cache_key $host$uri$is_args$args;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 2;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ /purge(/.*)
{
allow 127.0.0.1;
deny all;
fastcgi_cache_purge DIGG $host$1$is_args$args;
}
nginx -t donā€™t returns any error.
but when i visit http://mydomain/purge/xxx.jpg, it returns me a 404
page.
i checked the error log and found this:

2010/10/11 05:04:25 [emerg] 8915#0: unknown directive
ā€œfastcgi_cache_purgeā€ in /usr/local/nginx/conf/nginx.conf:27

is there anybody help me? thanks a lot.

Posted at Nginx Forum:

hi Piotr,
i used nginx 0.75 before, and compile ngx_cache_purge with nginx 0.85.2,
after ./configure --add-module=ā€¦/ngx_cache_purge-1.2 ā€¦(my own
parameters) and make, i didnā€™t run a make install and just cp objs/nginx
covering the old one. itā€™s general method when i upgrade the nginx. i
found there is a file named ngx_cache_purge_module.o in the directory
objs/addon/ngx_cache_purge-1.2, but i donā€™t know where should i cp it
to. could you please tell me how to install your mod with a running
nginx?
thanks a lot
regards
solo

Posted at Nginx Forum:

Hi,
did you restart nginx after that? Reload isnā€™t sufficient.

Best regards,
Piotr S. < [email protected] >

hi piotr,
sure, i have restarted nginx for many times. but it donā€™t work well now
stillā€¦
need i copy ngx_cache_purge_module.o to main dir of nginx? or is there
any way to check whether your mod has been compiled with nginx well?
regards
solo

Posted at Nginx Forum:

Hi,

2010/10/11 05:04:25 [emerg] 8915#0: unknown directive
ā€œfastcgi_cache_purgeā€ in /usr/local/nginx/conf/nginx.conf:27

You are not using nginx compiled with ngx_cache_purge module.

Best regards,
Piotr S. < [email protected] >

Hi,

need i copy ngx_cache_purge_module.o to main dir of nginx?

No, this is compile-time object file. You donā€™t need to worry about it.

or is there
any way to check whether your mod has been compiled with nginx well?

nginx -V

Best regards,
Piotr S. < [email protected] >

hi piotr,
i know this line, and it returns me:

nginx version: nginx/0.8.52
built by gcc 4.1.2 20080704 (Red Hat 4.1.2-48)
TLS SNI support disabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx
ā€“with-http_stub_status_module --with-http_ssl_module
ā€“add-module=ā€¦/ngx_cache_purge-1.2

i think i canā€™t determine whatā€™s wrong with it stillā€¦
regards
solo

Posted at Nginx Forum:

hi piotr,
iā€™m very sure iā€™m running the certain nginx, because iā€™m very puzzled so
that i delete all old versionā€¦
regards
solo

Posted at Nginx Forum:

Hi,
both of your outputs: ā€œnginx -Vā€ and ā€œnginx -tā€ suggest that nginx is
compiled with ā€œfastcgi_cache_purgeā€ support.

Are you sure that youā€™re running the same nginx binary as your init.d
script? What happens when you execute nginx binary yourself (without
using
init.d or other scripts)?

Best regards,
Piotr S. < [email protected] >

hi piotr,
i have no idea with this case, and have to reinstall whole nginx, it
works well now, thanks a lot for your help.
i donā€™t know why, but it works nowā€¦
regards
solo

Posted at Nginx Forum:

hi piotr,
could you please help me one more question? is it useful to cache a php
script in fastcgi_cache? in other words, need i disable fastcgi cache
for all php script? thanks a lot
regards
solo

Posted at Nginx Forum: