批量清除nginx缓存简单脚本

简单清除前端缓存脚本

环境一:nginx作为CDN前端缓存,清除各地区缓存机上所有域名下的图片文件缓存

脚本:clear.sh

#!/bin/sh 
#---CDN cache host list--- 
cat >>/tmp/iplist.txt <<EOF
61.x.x.x 
45.x.x.x 
122.x.x.x 
EOF 
#---purge cache script send to all CDN server--- 
for ip in `cat /tmp/iplist.txt` 
do 
  ping -w 1 -c 1 $ip >/dev/null 
 if [ $? -eq 0 ] 
  then 
    scp /tmp/nginx-clear.sh root@$ip:/tmp 
    ssh $ip "sh  /tmp/nginx-clear.sh &&rm -f /tmp/nginx-clear.sh" 
  else 
     echo "$ip host is not exist or network is no connect,continue next host...." 
  fi 
done


引用的脚本nginx-clear.sh

 
 
#!/bin/sh 
#-----clear jpg type cache---- 
cache_path="/data/proxy_cache_dir"
grep -a -r jpg $cache_path |strings |awk -F: '{print $3}' |sed 's/^ //g'| awk 'BEGIN{FS=OFS="/"}$1="http://"$1OFS"purge"' >>/tmp/cachelist.txt 
localip= `ifconfig eth0|awk -F'[ |:]+' '/inet addr/{print $4}'` 
cat >>/etc/hosts <<EOF
$localip imgtu.aa.net 
$localip imgtu.bb.com 
$localip imgtu.cc.com 
$localip imgtu.dd.com 
EOF 
for url in `cat  /tmp/cachelist.txt` 
do 
  curl $url 
  if [ $? -eq 0 ] 
  then 
    echo "$url is not clean,failure,please check......" 
  fi 
done

环境二:清除指定域名和目录路径下的缓存

 
 
#!/bin/bash 
#---source host take out the directory path 
for each in /var/wwwroot/imgtu.xxx.net/web/uploads/content/201303/*/* 
do  
  echo "$each" >>/tmp/test0417 
done 
#!/bin/bash 
#----------clean cache script---------- 
cat >>/tmp/iplist.txt <<EOF
61.x.x.x 
45.x.x.x 
122.x.x.x 
EOF 
echo "please input you need clean domainname:" 
read domainname                                                                  
URL=`cat /tmp/test0417 |sed 's:/var/wwwroot/:http\://:g' |sed 's:/web:\:81/purge:g'` 
for host in `cat /tmp/iplist.txt` 
do 
 echo "$host $domainname" >>/etc/hosts 
 for i in $URL 
 curl $i 
 done 
done


你可能感兴趣的:(nginx缓存,清除nginx缓存)