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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章