当前位置 主页 > 服务器问题 > nginx问题汇总 >

    使用Nginx作缓存服务器以及删除其缓存文件的方法(2)

    栏目:nginx问题汇总 时间:2018-10-02 16:22


    说明:其实按这个配置location /这个匹配是多余的,因为过来一个后缀名为apk的软件包location ~* .*\.(apk)$已经给匹配上了,不会再到location /了,不过由于我们还会缓存些其他后缀名的文件,所以location /就是必须的。

    c-->/etc/rc.local#!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff.touch /var/lock/subsys/localulimit -HSn 65535/usr/local/nginx/sbin/nginx 

    删除nginx缓存文件
    一:脚本

    a shell版

    #!/bin/bash#Date: 2013-06-27#Auther: budong########################################################说明:#  1.本脚本用于清除nginx缓存文件#  2.查看你的nginx是根据什么作为key来hash的,我的设置是 proxy_cache_key $uri$is_args$args;#  因此nginx会根据$uri$is_args$args作为key进行hash,因此可以模拟nginx对一个key进行再#  hash找到相应的文件路径,删除(具体可随意找个缓存文件 more 一下看看)#  3.缓存设置 proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g;#  根据相应的配置,请做相应修改测试#  4.uri格式请按照同级目录下rm_apk_list.txt中填写#####################################################while read -r linedo  md5uri=`echo -n $line | md5sum | awk '{ print $1 }'`  filepath=`echo "$md5uri" | awk '{print "/data/mumayi/cache/"substr($0,length($0),1)"/"substr($0,length($0)-2,2)"/"$0}'`  rm -rf $filepathdone < /root/sbin/rm_apk_list.txtb python版#!/usr/local/python2.7/bin/python2.7# -*- coding:utf8 -*-#Date: 2013-06-26#Name: budong'''说明:  1.本脚本用于清除nginx缓存文件  2.查看你的nginx是根据什么作为key来hash的,我的设置是 proxy_cache_key $uri$is_args$args;  因此nginx会根据$uri$is_args$args作为key进行hash,因此可以模拟nginx对一个key进行再  hash找到相应的文件路径,删除(具体可随意找个缓存文件 more 一下看看)  3.缓存设置 proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g;  根据相应的配置,请做相应修改测试  4.uri格式请按照同级目录下rm_apk_list.txt中填写'''import osimport systry:  from hashlib import md5except:  from md5 import md5reload( sys )sys.setdefaultencoding('utf-8')PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))URI_FILE = ''.join([PROJECT_ROOT,'/rm_apk_list.txt'])def nginx_purge(uri):  m = md5()  m.update("%s" % uri)  md5uri=m.hexdigest()  md5uri_len=len(md5uri)  dir1=md5uri[md5uri_len-1:]  dir2=md5uri[md5uri_len-3:md5uri_len-1]  file_path=("/data/mumayi/cache/%s/%s/%s" % (dir1, dir2, md5uri))  if os.path.exists(file_path):    os.remove(file_path)with open("%s" % URI_FILE,'r') as uri_file:  for line in uri_file:    line = line.rstrip()    nginx_purge(line)

    c ngx_cache_purge不做考虑,据说已经停止开发了

    说明:

    1 我的 /root/sbin/rm_apk_list.txt 文件

    [root@budong ~]# cat /root/sbin/rm_apk_list.txt /2013/08/15/38/382272/shuazanzhushou_V1.8.16_mumayi_95a91.apk

    2 查看一个缓存对象,应该有些明白了吧

    [root@budong ~]# more /data/mumayi/cache/0/00/db9327b60a6b3c164516117f90d9d000