Varnish purges cache cleanup tutorial

  
 

Varnish's cache cleanup is very complicated. Whether it is Varnish's cleanup or grammar rules used when it is cleaned up, it's more complicated. In order to understand him, I spent a lot of time, and now I am very happy that I know how to explain it to everyone. Varnish has two ways to clear the cache. One way is by hitting a single variant of the object, so he can't clear an already compressed object when he hits an uncompressed object. This method is also forced expiry. It forces the expiration by setting the TTL of the object you want to clear to 0. The VCL settings are as follows:

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, another way is to use purge_url, VCL settings are as follows :

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);}

Through the above settings in the VCL file, we execute PURGE via HTTP. This means that you have now sent one:

PURGE /HTTP/1.0Host: www.example.com

Varnish was given via port 80. However, this way of implementing PURGE does not support regularity. If you want to support it, you can set VCL like this:

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, for the way of purge In addition to setting VCL to allow PURGE as in point 2 above, we can also clear the cache by sending flexible PURGE commands through Varnish's management port. 3.1 Let's first take a look at the help port for the management port (Varnish version 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 There are three commands related to purge in help, where purge.list is a list of purges, and purge is fine.url And purge two commands. 3.2.1 The purge.url command only supports the url of the purge, such as clearing 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

In addition to using the CLI interface, you can also use:

/usr/local/varnish-2.1/bin/varnishadm -T 192.168.1.185:3500 purge.url ^test.html$

3.2.2 The purge command is very flexible, see the list: Clear http: //izhoufeng.com/somedirectory/and all pages in the directory.

purge req.http.host == izhoufeng.com && req.url ~ ^/somedirectory/.*$orpurge req.url ~ ^/somedirectory/&& req.http.host == izhoufeng.com

Clear all objects with "Cache-Control: max-age=3600”.

purge obj.http.Cache-Control ~ max-age=3600orpurge obj.http.Cache-Control ~ max-age ?= ?3600[^0-9]

4, for A lot of cleanup requires a program interface to do it. 4.1 PURGE interface through HTTP. <span style="color: rgb(0, 0, 0); font-weight: bold;"><?php</span><span style="color: rgb(102, 102 , 102); font-style: italic;">//Refresh the varnish cache function, $ip is the varnish server IP address, $host is the website domain name to be refreshed, $url is the URL to be refreshed without domain name Address </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>

Copyright © Windows knowledge All Rights Reserved