Varnish Purges Cache Cleanup Tutorial

Source: Internet
Author: User

Varnish cache cleanup is very complex. Whether the varnish or the removal of the use of the grammar rules, etc., are more complex. In order to understand him, I spent a lot of time, now I'm glad I know how to explain to everyone.

1. Varnish has two ways to clear the cache, one of which is by hitting a single variant of the object, so that he cannot clear a compressed object when he hits an object that is not compressed. This is the forced expiration (forced expiry), which forces it to expire by setting the TTL of the object you want to erase 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 "purged."
}
}

Sub Vcl_miss {
if (req.request = = "PURGE") {
Error 404 "not in cache.";
}
}

2, another way is to use the PURGE_URL,VCL settings 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 the purge via HTTP. This means that you are now sending a:


purge/http/1.0
Host:www.example.com

Varnish was given through port 80. However, this way of performing purge does not support regular. If you want to support, you can set the VCL 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);
}

3, for the purge way, in addition to the 2nd like the above set VCL to allow purge, in fact, we can also through the varnish management port to send flexible purge command to clear the cache.
3.1 First let's take a look at the admin port Help (Varnish version 2.1)


[root@varnish4 varnish]# telnet 192.168.1.185 3500
Trying 192.168.1.185 ...
Connected to 192.168.1.185 (192.168.1.185).
Escape character is ' ^] '.
154
-----------------------------
Varnish HTTP accelerator CLI.
-----------------------------
Type ' help ' for command list. The
Type ' quit ' to close CLI session.

Help
377
Help [command]
Ping [timestamp]
Auth response
Quit
Banner
status
S Tart
Stop
Stats
vcl.load <configname> <filename>
Vcl.inline <configname> <quoted_ Vclstring>
Vcl.use <configname>
Vcl.discard <configname>
Vcl.list
Vcl.show < Configname>
Param.show [-l] [<param>]
Param.set <param> <value>
Purge.url <regexp
Purge <field> <operator> <arg> [&& <field> <oper> <arg>]
Purge.list

There are three commands for 3.2 help neutralization Purge, where purge.list is a list of purge that can execute purge purge.url and purge two commands.
3.2.1 Purge.url command It only supports purge of URLs, such as clearing http://blog.izhoufeng.com/test.html.


[Root@varnish2 varnish]# telnet 192.168.1.185 3500
Trying 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.html
200 0

In addition to using CLI interfaces, you can use:


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

3.2.2 Purge command is very flexible, please see the case:
Clears all pages in the http://izhoufeng.com/somedirectory/and directories.


Purge Req.http.host = = izhoufeng.com && req.url ~ ^/somedirectory/.*$
Or
Purge Req.url ~ ^/somedirectory/&& req.http.host = = izhoufeng.com

Clears all objects with "cache-control:max-age=3600".


Purge Obj.http.cache-control ~ max-age=3600
Or
Purge Obj.http.cache-control ~ max-age? =? 3600[^0-9]

4, for a large number of cleaning, the need for the program interface to do.
4.1 Interface via HTTP purge.
<span style= "Color:rgb (0, 0, 0); Font-weight:bold; " ><?php</span>
<span style= "Color:rgb (102, 102, 102); Font-style:italic; " >//refresh varnish Cached functions, $IP for the varnish server IP address, $host for the site domain name to be refreshed, $url the URL address to be refreshed without domain name </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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.