清除nginx靜態資源緩存

以前寫過一篇如何配置nginx緩存及手動清除緩存的文章:html

http://www.cnblogs.com/Eivll0m/p/4921829.htmlpython

但若是有大量緩存須要清理,手動一條條清理就比較慢了,因此寫了個小腳本進行清理,腳本以下:nginx

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# data:2015-12-08
# author:eivll0m
# 腳本用途:清除nginx靜態資源緩存
# 使用方法:將要清除緩存的url粘貼到/app/admin/sbin/url.txt文件中,一行放一個url,而後執行此腳本。

import urllib2

def purge(FILE):
    with open(FILE) as f:
        for line in f:
            L = line.strip().split('/')
            L.insert(3,'purge')
            req = urllib2.Request('/'.join(L))
            try:
                response = urllib2.urlopen(req)
                page = response.read()
                print page
            except urllib2.HTTPError, e:
                print line + '緩存已清除或沒有緩存!'
                #print 'Error code: ', e.code

if __name__ == '__main__':
    F = "/app/admin/sbin/url.txt"
    purge(F)
相關文章
相關標籤/搜索