1、安装varnish
1
2
3
4
|
rpm -ivh http:
//mirrors
.opencas.cn
/epel/epel-release-latest-7
.noarch.rpm
yum
install
-y varnish
systemctl start varnish
systemctl
enable
varnish
|
2、设置监听端口
1
2
|
vim
/etc/varnish/varnish
.params
VARNISH_LISTEN_PORT=6081
|
3、设置后端服务器
1
2
3
4
5
|
vim
/etc/varnish/default
.vcl
backend default {
.host =
"172.16.1.21"
;
.port =
"80"
;
}
|
4、配置VCL文件
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples.
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host =
"127.0.0.1"
;
.port =
"80"
;
}
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
if
(req.url ~
"\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)"
) {
return
(pass);
}
if
(req.url ~
"\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)"
) {
unset
req.http.cookie;
return
(
hash
);
}
if
(req.restarts == 0) {
if
(req.http.x-forwarded-
for
) {
set
req.http.X-Forwarded-For = req.http.X-Forwarded-For +
", "
+ client.ip;
}
else
{
set
req.http.X-Forwarded-For = client.ip;
}
}
if
(req.http.Cache-Control ~
"(?i)no-cache"
) {
if
(!(req.http.Via || req.http.User-Agent ~
"(?i)bot"
|| req.http.X-Purge)) {
return
(purge);
}
}
if
(req.method !=
"GET"
&&
req.method !=
"HEAD"
&&
req.method !=
"PUT"
&&
req.method !=
"POST"
&&
req.method !=
"TRACE"
&&
req.method !=
"OPTIONS"
&&
req.method !=
"PATCH"
&&
req.method !=
"DELETE"
) {
return
(pipe);
}
if
(req.method !=
"GET"
&& req.method !=
"HEAD"
) {
return
(pass);
}
if
(req.http.Authorization) {
return
(pass);
}
if
(req.http.Accept-Encoding) {
if
(req.url ~
"\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$"
) {
unset
req.http.Accept-Encoding;
} elseif (req.http.Accept-Encoding ~
"gzip"
) {
set
req.http.Accept-Encoding =
"gzip"
;
} elseif (req.http.Accept-Encoding ~
"deflate"
) {
set
req.http.Accept-Encoding =
"deflate"
;
}
else
{
unset
req.http.Accept-Encoding;
}
}
if
(req.http.Upgrade ~
"(?i)websocket"
) {
return
(pipe);
}
if
(req.http.x-pipe && req.restarts > 0) {
unset
req.http.x-pipe;
return
(pipe);
}
return
(
hash
);
}
sub vcl_pipe {
if
(req.http.upgrade) {
set
bereq.http.upgrade = req.http.upgrade;
}
return
(pipe);
}
sub vcl_pass {
if
(req.method ==
"PURGE"
) {
return
(synth(502,
"PURGE on a passed object."
));
}
}
sub vcl_hash {
hash_data(req.url);
if
(req.http.host) {
hash_data(req.http.host);
}
else
{
hash_data(server.ip);
}
if
(req.http.Cookie) {
hash_data(req.http.Cookie);
}
if
(req.http.Accept-Encoding ~
"gzip"
) {
hash_data(
"gzip"
);
} elseif (req.http.Accept-Encoding ~
"deflate"
) {
hash_data(
"deflate"
);
}
}
sub vcl_hit {
if
(req.method ==
"PURGE"
) {
return
(synth(200,
"Purged."
));
}
if
(obj.ttl >= 0s) {
return
(deliver);
}
return
(deliver);
}
sub vcl_miss {
if
(req.method ==
"PURGE"
) {
return
(synth(404,
"Purged."
));
}
return
(fetch);
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
}
sub vcl_purge {
if
(req.method !=
"PURGE"
) {
set
req.http.X-Purge =
"Yes"
;
return
(restart);
}
}
#设置检查是否命中X-Cache
sub vcl_deliver {
if
(obj.hits > 0) {
set
resp.http.X-Cache =
"HIT"
;
}
else
{
set
resp.http.X-Cache =
"MISS"
;
}
unset
resp.http.X-Powered-By;
unset
resp.http.Server;
unset
resp.http.Via;
unset
resp.http.X-Varnish;
unset
resp.http.Age;
}
|
5、使用chrome浏览器是否命中
参考博文:http://sofar.blog.51cto.com/353572/1653683/
本文转自1321385590 51CTO博客,原文链接:http://blog.51cto.com/linux10000/1747881,如需转载请自行联系原作者