nginx内置缓存Proxy_cache之清除

nginx的第三方模块

可以在原有基础上直接增加模块,只能一个一个访问,比较麻烦

#wget http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz
#tar -xzf ngx_cache_purge-2.0.tar.gz
在原有编译基础上加上(–add-module=../ngx_cache_purge-2.0),注意解压路径
#cd nginx-1.6.2
#./configure –user=nginx –group=nginx –prefix=/usr –sbin-path=/usr/sbin/nginx –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx/nginx.pid –lock-path=/var/lock/nginx.lock –with-http_ssl_module –with-http_flv_module –with-http_stub_status_module –with-http_gzip_static_module –http-client-body-temp-path=/var/tmp/nginx/client/ –http-proxy-temp-path=/var/tmp/nginx/proxy/ –http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ –http-uwsgi-temp-path=/var/tmp/nginx/uwsgi –http-scgi-temp-path=/var/tmp/nginx/scgi –with-pcre –with-http_image_filter_module –with-file-aio –add-module=../ngx_cache_purge-2.0> /dev/null

修改nginx配置如下:

  server {
        listen       80;
        server_name  www.mytest.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
#清除缓存,务必放在前面(要清除http://www.mytest.com/hello/index.html,则访问http://www.mytest.com/purge/hello/index.html)
         location ~ /purge(/.*) {
            allow 127.0.0.1;
            allow 192.168.1.0/24;
            deny all;
            proxy_cache_purge one_cache $host$1$is_args$args;
        }

        location ~ .*\.(html|htm|css|js|ico|jpeg|git|jpg|png|bmp|swf)$ {
            root /home/test/apache-tomcat-7.0.64/webapps/hello;
            proxy_pass http://mytest;
            proxy_set_header Host  $host;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP  $remote_addr;

            proxy_cache one_cache;
            proxy_cache_valid  200 304 301 302 1d;
            proxy_cache_valid  any 6h;
            proxy_cache_key $host$uri$is_args$args;
            add_header X-Cache '$upstream_cache_status from $host';
        }


        location ~ .*$ {
            proxy_set_header Host  $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://mytest;
            add_header X-Cache '$upstream_cache_status from $host';
        }

shell脚本清除

可以批量清除,这里的只是最简单的脚本,还有许多不完善之处

#!/bin/bash
mfile="$*"
cache_dir=/etc/nginx/proxy_cache
echo $mfile
if [ "$#" -eq 0 ]
then
    echo "please input scripts, if not, it will exit"
    sleep 2 && exit
fi
echo "what you put $mfile will delete, please wait..."
for i in `echo $mfile | sed 's/ /\n/g'`
do
    grep -ira $i $cache_dir | awk -F ':' '{print $1}' > /tmp/cache_list.txt
    for j in `cat /tmp/cache_list.txt`
    do
        rm -rf $j
        echo "$i $j is delete Success!"
    done
done
rm -rf /tmp/cache_list.txt

操作如:
这里写图片描述
若有1.jpg和11.jpg会同时删除,所以要精确到删除某张图片还是用第三方库ngx_cache_purge。若想删除所有.jpg格式的图片,可以直接./1.sh jpg

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值