Varnish的緩存清除非常復雜。無論是Varnish的清除方式還是清除時候使用的語法規(guī)則等,都是比較復雜。為了理解他,我花費了不少時間,現(xiàn)在我很高興我知道怎么來解釋給大家聽了。
1、Varnish有兩種方式來清除緩存,其中一種方式是通過命中對象的單一變體,所以在他命中一個沒有壓縮的對象的時候他不能清除一個已經(jīng)壓縮的對象。這個方式也就是強制過期(forced expiry),他是通過設置你想清除的對象的TTL為0去強制它過期。VCL設置如下:
acl purge {"localhost";"192.0.2.14";}sub vcl_recv {if (req.request == "PURGE") {if (!client.ip ~ purge) {error 405 "Not allowed.";}lookup;}}sub vcl_hit {if (req.request == "PURGE") {set obj.ttl = 0s;error 200 "Purged.";}}sub vcl_miss {if (req.request == "PURGE") {error 404 "Not in cache.";}}
2、另外一種方式是使用purge_url,VCL設置如下:
acl purge {"localhost";"192.0.2.14";}sub vcl_recv {if (req.request == "PURGE") {if (!client.ip ~ purge) {error 405 "Not allowed.";}purge("req.url == " req.url);}
通過以上在VCL文件的設置,我們通過HTTP來執(zhí)行PURGE。這意味著你現(xiàn)在發(fā)送了一個:
PURGE / HTTP/1.0Host: www.example.com
通過80端口給了Varnish。但是,這種執(zhí)行PURGE的方式不支持正則。如果你想支持,可以按照這樣來設置VCL:
acl purge {"localhost";"192.0.2.14";}sub vcl_recv {if (req.request == "PURGE") {if (!client.ip ~ purge) {error 405 "Not allowed.";}purge("req.url ~ " req.url);}
3、對于purge的方式,除了像上邊第2點那樣設置VCL來允許PURGE外,其實我們還可以通過Varnish的管理端口發(fā)送靈活的PURGE命令來清除緩存。3.1 首先讓我們來看看管理端口的help(Varnish版本2.1)
[root@varnish4 varnish]# telnet 192.168.1.185 3500Trying 192.168.1.185...Connected to 192.168.1.185 (192.168.1.185).Escape character is ^].200 154 -----------------------------Varnish HTTP accelerator CLI.-----------------------------Type help for command list.Type quit to close CLI session.help200 377 help [command]ping [timestamp]auth responsequitbannerstatusstartstopstatsvcl.load <configname> <filename>vcl.inline <configname> <quoted_VCLstring>vcl.use <configname>vcl.discard <configname>vcl.listvcl.show <configname>param.show [-l] [<param>]param.set <param> <value>purge.url <regexp>purge <field> <operator> <arg> [&& <field> <oper> <arg>]...purge.list
3.2 help中和purge有關的命令有三個,其中purge.list是查看purge的列表,能執(zhí)行purge的是purge.url和purge兩個命令。3.2.1 purge.url命令它只支持url的purge,如清除http://blog.izhoufeng.com/test.html。
[root@varnish2 varnish]# telnet 192.168.1.185 3500Trying 192.168.1.185...Connected to varnish1 (192.168.1.185).Escape character is ^].200 154 -----------------------------Varnish HTTP accelerator CLI.-----------------------------Type help for command list.Type quit to close CLI session.purge.url test.html200 0
除用CLI接口外也可以用:
/usr/local/varnish-2.1/bin/varnishadm -T 192.168.1.185:3500 purge.url ^test.html$
3.2.2 purge命令則很靈活,請看列子:清除http://izhoufeng.com/somedirectory/和目錄下的所有頁面。
purge req.http.host == izhoufeng.com && req.url ~ ^/somedirectory/.*$orpurge req.url ~ ^/somedirectory/ && req.http.host == izhoufeng.com
清除所有帶“Cache-Control: max-age=3600”的對象。
purge obj.http.Cache-Control ~ max-age=3600orpurge obj.http.Cache-Control ~ max-age ?= ?3600[^0-9]
4、對于大量清除,需要程序接口來做。4.1 通過HTTP的PURGE的接口。<span style="color: rgb(0, 0, 0); font-weight: bold;"><?php</span><span style="color: rgb(102, 102, 102); font-style: italic;">//刷新varnish緩存的函數(shù),$ip為varnish服務器IP地址, $host為要刷新的網(wǎng)站域名,$url為要刷新的不含域名的URL地址</span><span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> varnish_purge<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(0, 0, 136);">$ip</span><span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 0, 136);">$host</span><span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 0, 136);">$url</span><span style="color: rgb(0, 153, 0);">)</span>
新聞熱點
疑難解答
圖片精選