刚刚学习使用varnish,这个章节测试varnish的purge功能。
一、 配置vcl,在vcl中添加如下代码。可以参考官网配置,http://www.varnish-cache.org/trac/wiki/VCLExamplePurging
- 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.";
- }
- }
二、 使用varnishadm来purge
1. 清除某个文件,$表示正则表达式的结尾附;
/opt/varnish/bin/varnishadm -T :4488 purge.url test.jpg$ |
2. 清除某个文件夹
varnishadm -T :4488 purge.url ^/p_w_picpaths/.*$ |
三、通过varnishlog来验证,访问某个元素,每刷新一次,x-cache-hits会增加
[root@VMSHcentos01 ~]# /opt/varnish/bin/varnishlog -i TxHeader 13 TxHeader c Date: Fri, 13 May 2011 06:28:18 GMT 13 TxHeader c Via: 1.1 varnish 13 TxHeader c X-Varnish: 1688554888 13 TxHeader c Last-Modified: Thu, 14 Apr 2011 03:52:45 GMT 13 TxHeader c Cache-Control: max-age=31536000 13 TxHeader c Connection: keep-alive 13 TxHeader c X-Cache: HIT 13 TxHeader c X-Cache-Hits: 1 13 TxHeader c Date: Fri, 13 May 2011 06:28:26 GMT 13 TxHeader c Via: 1.1 varnish 13 TxHeader c X-Varnish: 1688554889 13 TxHeader c Last-Modified: Thu, 14 Apr 2011 03:52:45 GMT 13 TxHeader c Cache-Control: max-age=31536000 13 TxHeader c Connection: keep-alive 13 TxHeader c X-Cache: HIT 13 TxHeader c X-Cache-Hits: 2 |
执行purge,
/opt/varnish/bin/varnishadm -T :4488 purge.url 82839fc8-f49c-4191-bf02-420fe0c40310.jpg$ |
重新访问,看到Hits是0.
15 TxHeader b X-Varnish: 1688554890 |
转载于:https://blog.51cto.com/18567/564841