nginx系列(二十)nginx的缓存清理模块ngx_cache_purge

前言
ngx_cache_purge是第三方模块,用于清理nginx内置模块(proxy_cache)缓存。就像CDN的清理缓存的url命令一样。
模块下载地址
https://github.com/FRiCKLE/ngx_cache_purge

安装
./configure --prefix=/opt/nginx/nginx-1.9.6 --add-module=/opt/soft/ngx_cache_purge-2.3
make
make install

配置
    # Cache_proxy Purge,这个url中,必须以波浪号开头,不要加"^"
    location ~ /purge(/.*) {
        proxy_cache_purge content $host:$server_port$1$is_args$args;
        access_log  logs/cache.log cache_log;
    }  

    # 如果请求头里有信息:Cache-Control:no-cache,则缓存规则失效
    location /cache_ehcache-2.10.0_web {
        proxy_pass http://192.168.56.1:8080/cache_ehcache-2.10.0_web;
        proxy_cache content; #根keys_zone后的内容对应 
        proxy_cache_valid  200 304 301 302 1h;   #哪些状态缓存多长时间 
        proxy_cache_methods GET;  # 默认是get和head
        proxy_cache_valid  any 3s;    #其他的缓存多长时间 
        proxy_cache_key $host:$server_port$uri$is_args$args;   #通过key来hash,定义KEY的值
        #缓存的具体key值是:   [host]192.168.56.2 [server_port]8080 [uri]/cache_ehcache-2.10.0_web/expire [is_args] [args]-
        proxy_cache_min_uses 3; #只要统一个url,在磁盘文件删除之前,总次数访问到达3次,就开始缓存。
        proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment; # 如果任何一个参数值不为空,或者不等于0,nginx就不会>查找缓存,直接进行代理转发
        #增加输出日志
        access_log  logs/cache.log cache_log;
    }

测试效果
输入URL:http://192.168.56.2:8080/cache_ehcache-2.10.0_web/expire?a=222


清理缓存:http://192.168.56.2:8080/purge/cache_ehcache-2.10.0_web/expire?a=222


之后再次访问原来的url,缓存就会失效了。

高级用法
1.结合nginx的白名单/黑名单功能:http://phl.iteye.com/blog/2251767
2.结合nginx的auth使用:http://phl.iteye.com/blog/2251771
3.nginx的代理缓存设置:http://phl.iteye.com/blog/2253442
这样,安全控制得到了保障。如果是nginx集群,可以使用python进行集群的清理。

参考文章
nginx之location配置
http://blog.csdn.net/hellochenlian/article/details/44655547

Nginx Purge清除缓存配置
http://www.myhack58.com/Article/sort099/sort0100/2013/38043.htm

nginx第三方插件
https://www.nginx.com/resources/wiki/modules/?highlight=purge

猜你喜欢

转载自phl.iteye.com/blog/2256356