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

本文介绍了NGINX内置缓存模块proxy_cache的清理方式,通过第三方模块ngx_cache_purge实现精准缓存清理。包括安装、配置、高级用法及与NGINX其他功能的结合,提供了全面的缓存管理解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

[size=xx-large]安装[/size]
./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
[img]http://dl2.iteye.com/upload/attachment/0113/0237/495ade35-88f3-3eb9-a095-ed44a39c1691.png[/img]

清理缓存:http://192.168.56.2:8080/purge/cache_ehcache-2.10.0_web/expire?a=222
[img]http://dl2.iteye.com/upload/attachment/0113/0235/cf8337b6-edc9-353a-9aab-957b84296ad5.png[/img]

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

[size=xx-large]高级用法[/size]
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进行集群的清理。

[size=xx-large]参考文章[/size]
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值