shell批量清理nginx缓存脚本 【ok】

这两天在研究下perl清理缓存的方法

先查看下nginx的缓存使用的情况 

  
  
  
  
  1. [root@localhost ~]# grep -a -r jpg /tmp 
  2. grep: /tmp/.font-unix/fs7100: No such device or address 
  3. /tmp/proxy_cache_dir/7/bd/fa7598dff0b629c144632008620dfbd7:KEY: 10.10.10.22/tupian/61050_1338704916meeb.jpg.mid.jpg 
  4. /tmp/proxy_cache_dir/7/d5/8dd69d3d271f2f3a2ddb90d48a4f5d57:KEY: 10.10.10.22/123/sdfasdfa.jpg 
  5. /tmp/proxy_cache_dir/a/c5/d92a17c8c8eb4666cbbf11ca973d4c5a:KEY: 10.10.10.22/123/012.jpg 
  6. /tmp/proxy_cache_dir/a/68/21e64184982e8e31146b2ab7c6cf068a:KEY: 10.10.10.22/123/posters28.jpg 
  7. /tmp/proxy_cache_dir/a/68/2bb08d9b46fcb330b1338d57abdad68a:KEY: 10.10.10.22/tupian/61050_13387049050o4Y.jpg.mid.jpg 
  8. /tmp/proxy_cache_dir/c/8e/03796363bb6b40f8d1c46ab7ce1bf8ec:KEY: 10.10.10.22/tupian/61043_1338704919migW.jpg.mid.jpg 
  9. /tmp/proxy_cache_dir/4/fa/38e438e2b2c1376f2a4267441348bfa4:KEY: 10.10.10.22/123/001.jpg 
  10. /tmp/proxy_cache_dir/4/dc/1092567d9c88a423d8a73e0f9ab4fdc4:KEY: 10.10.10.22/tupian/45_avatar_small.jpg 
  11. /tmp/proxy_cache_dir/f/07/4b0b1d5163f0a5f0a5ceb9b51996b07f:KEY: 10.10.10.22/123/jienslps52.jpg 
  12. /tmp/proxy_cache_dir/d/43/991cde969a0bae8e748244399cb0743d:KEY: 10.10.10.22/123/415808.jpg 
  13. /tmp/proxy_cache_dir/5/c5/edb48f38b3a568079d08ee87e56d1c55:KEY: 10.10.10.22/tupian/46_avatar_small.jpg 
  14. /tmp/proxy_cache_dir/b/34/c7a1bf4c0a12894eb9e09a131462534b:KEY: 10.10.10.22/tupian/44_avatar_small.jpg 
  15. /tmp/proxy_cache_dir/1/e4/c984f2640ff7ee17754627de298fbe41:KEY: 10.10.10.22/tupian/61048_1338704895a570.jpg.mid.jpg 
  16. /tmp/proxy_cache_dir/1/dc/bb403bb5dea1739bd6b98a3d9bbc8dc1:KEY: 10.10.10.22/tupian/61050_1338704895313p.jpg.mid.jpg 
  17. /tmp/proxy_cache_dir/3/03/08f1512aaae9c4d3d374a6ae9f941033:KEY: 10.10.10.22/tupian/61050_1338704928Ihc9.jpg.mid.jpg 
  18. /tmp/proxy_cache_dir/3/13/42f6cb8e81f03ce5390978da30143133:KEY: 10.10.10.22/tupian/61050_133870491466qQ.jpg.mid.jpg 
  19. /tmp/proxy_cache_dir/8/e6/8aaf0d9d323f0ed1d19a3a1452348e68:KEY: 10.10.10.22/123/011.jpg 
  20. /tmp/proxy_cache_dir/6/5e/7f0080fcc6a1898a033d1a9876a365e6:KEY: 10.10.10.22/123/webshots007.jpg 
  21. grep: /tmp/mapping-root: No such device or address 
  22. [root@localhost ~]#  

shell清理缓存~~~~~~~

这里的ip   10.10.10.22,可以替换成你的域名的,比如www.92hezu.com     ~~~~~~

  
  
  
  
  1. #!/bin/sh 
  2. nginxcache_path="/tmp/proxy_cache_dir" 
  3. grep -a -r $2 $nginxcache_path | strings |grep "KEY:" | awk -F'KEY: ' '{print "http://"$2;}' | grep [a-zA-Z0-9]$ > cache_list.txt 
  4. #清除cat cache_list.txt文本中url所对应的nginx 
  5. sed -i "s#10.10.10.22/#10.10.10.22/purge/#" cache_list.txt 
  6. for url in `cat cache_list.txt`; do 
  7. curl $url 
  8. done 

 

 [root@localhost ~]# ./ng 10.10.10.22 jpg

<html>

<head><title>Successful purge</title></head>

<body bgcolor="white">

<center><h1>Successful purge</h1>

<br>Key : 10.10.10.22/tupian/61050_1338704916meeb.jpg.mid.jpg

<br>Path: /tmp/proxy_cache_dir/7/bd/fa7598dff0b629c144632008620dfbd7

</center>

<hr><center>nginx/1.2.0</center>

</body>

</html>

<html>

<head><title>Successful purge</title></head>

<body bgcolor="white">

<center><h1>Successful purge</h1>

<br>Key : 10.10.10.22/123/sdfasdfa.jpg

<br>Path: /tmp/proxy_cache_dir/7/d5/8dd69d3d271f2f3a2ddb90d48a4f5d57

</center>

<hr><center>nginx/1.2.0</center>

</body>

</html>

<html>

<head><title>Successful purge</title></head>

<body bgcolor="white">

<center><h1>Successful purge</h1>

<br>Key : 10.10.10.22/123/012.jpg

<br>Path: /tmp/proxy_cache_dir/a/c5/d92a17c8c8eb4666cbbf11ca973d4c5a

</center>

<hr><center>nginx/1.2.0</center>

</body>

</html>

 

 

浏览要清理的图片~~~~~~~~~~~~~~~~~~!

清理图片~~~~~~~~~! 

 

你可能感兴趣的:(nginx)