1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| vcl 4.0; //必须要写,注明版本
import directors;
probe check1 { //制定健康检测策略
.url = "/index.html";
.timeout=1s;
.interval=2s;
.window=5;
.threshold=3;
}
backend server1 {
.host = "172.17.252.63"; //lnmp服务器
.port = "80";
.probe = check1;
}
backend server2 {
.host = "172.17.253.59";
.port = "80";
.probe = check1;
}
sub vcl_init { //初始化
new web_cluster = directors.random();
web_cluster.add_backend(server1,10);
web_cluster.add_backend(server2,20);
}
acl purgers { # 定义可访问来源IP
"127.0.0.1";
"172.17.0.0"/16;
}
sub vcl_recv {
if (req.method == "GET" && req.http.cookie) {
return(hash);
}
if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "PURGE" &&
req.method != "DELETE") {
return (pipe);
}
}
sub vcl_hash { //对访问的URL进行hash
hash_data(req.url);
}
sub vcl_backend_response { # 自定义缓存文件的缓存时长,即TTL值
if (bereq.url ~ "\.(jpg|jpeg|gif|png)$") {
set beresp.ttl = 3600s;
}
if (bereq.url ~ "\.(html|css|js)$") {
set beresp.ttl = 7200s;
}
set beresp.grace = 30m;
return(deliver);
}
sub vcl_deliver {
if (obj.hits > 0) { # 为响应添加X-Cache首部,显示缓存是否命中
set resp.http.X-Cache = "HIT from " + server.ip;
} else {
set resp.http.X-Cache = "MISS";
}
unset resp.http.X-Powered-By;
unset resp.http.Via;
}
|