首页

使用Shell脚本批量清除Nginx缓存

提高网站速度体验除了本身网站程序优化外,对于LinuxSA来说还有大量的工作要做,优化系统内核、调整Web服务器的参数、优化数据库、增加网站架构缓存等等一系列的工作。

对于网站缓存,目前主流的HTTP加速器主要有Varnish、Nginx_proxy、Squid等,随着Nginx Web 高速反向代理被各大中型网站使用,其集成缓存的功能(Nginx_proxy)也日益强大,目前企业中也在大量使用。今天我们来研究一下 Nginx_Proxy缓存如何有效的来清理。如下使用Shell脚本来自动化清理,直接上脚本如下:

#! /bin/sh

#Auto Clean Nginx Cache Shell Scripts

#2013-06-12 wugk

#Define Path

CACHE_DIR=/data/www/proxy_cache_dir/

FILE="$*"

#To determine whether the input script,If not, then exit 判断脚本是否有输入,没有输入然后退出

if

[ "$#" -eq "0" ];then

echo "Please Insertclean Nginx cache File, Example: $0 index.html index.js"

sleep 2 && exit

fi

echo "The file : $FILEto be clean nginx Cache ,please waiting ....."

#Wrap processing for the input file, for grep lookup,对输入的文件进行换行处理,利于grep查找匹配相关内容

for i in `echo $FILE |sed 's//n/g'`

do

grep -ra $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 DeletedSuccess !"

done

done

#The Scripts exec success and exit 0

如下为执行清除脚本后的截图:

使用Shell脚本批量清除Nginx缓存