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

你可能感兴趣的:(web)