[root@node0 varnish]# vim default.vcl
acl purgers { # 定义acl访问控制,只允许以下网段或主机执行purgers操作
"127.0.0.1";
"172.16.0.0"/16;
}
sub vcl_recv {
if (req.request == "PURGE") { # 如果请求方法是PURGE,并且客户端IP在上面定义的网段内的就允许执行PURGE操作,否则生成一个错误页面返回给用户
if (!client.ip ~ purgers) {
error 405 "Method not allowed";
}
return (lookup);
}
}
sub vcl_hit {
if (req.request == "PURGE") { # 如果缓存中命中,那么就清除缓存内容
purge;
error 200 "Purged";
}
}
sub vcl_miss {
if (req.request == "PURGE") { # 如果缓存中未命中,说明缓存中没有内容
purge;
error 404 "Not in cache";
}
}
sub vcl_pass {
if (req.request == "PURGE") { # 如在pass中要清除缓存,直接返回错误码
error 502 "PURGE on a passed object";
}
}
# 保存退出,而后直接在命令行中进行测试一下:
[root@node0 varnish]# curl -I http://172.16.27.88/index.html
HTTP/1.1 200 OK
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sat, 17 May 2014 08:07:17 GMT
ETag: "120904-47-4f99404c6fde2"
Content-Type: text/html; charset=UTF-8
Content-Length: 71
Accept-Ranges: bytes
Date: Sat, 17 May 2014 13:38:51 GMT
X-Varnish: 364681188 364681185
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from 172.16.27.88 # 缓存命中
[root@node0 varnish]# curl -X PURGE http://172.16.27.88/index.html
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>200 Purged OK.</title> # 缓存清除成功
</head>
<body>
<h1>Error 200 Purged OK.</h1>
<p>Purged OK.</p>
<h3>Guru Meditation:</h3>
<p>XID: 364681189</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
[root@node0 varnish]# curl -I http://172.16.27.88/index.html
HTTP/1.1 200 OK
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sat, 17 May 2014 08:07:17 GMT
ETag: "120904-47-4f99404c6fde2"
Content-Type: text/html; charset=UTF-8
Content-Length: 71
Accept-Ranges: bytes
Date: Sat, 17 May 2014 13:42:39 GMT
X-Varnish: 364681194
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS # 清除后缓存MISS了
|