1
2
3
4
5
|
yum
install
-y
make
automake pkgconfig libtool pcre-devel
#安装依赖包
tar
zxvf varnish-3.0.3.
tar
.gz
.
/autogen
.sh
.
/configure
--prefix=
/usr/local/varnish
--
enable
-debugging-symbols --
enable
-developer-warnings --
enable
-dependency-tracking
make
&&
make
install
|
1
2
3
4
|
groupadd varnish
useradd
-g varnish -s
/sbin/nologin
varnish
touch
/usr/local/varnish/access
.log
#创建访问日志文件
chown
-R varnish.varnish
/usr/local/varnish
|
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
|
vi
/usr/local/varnish/etc/varnish/default
.vcl
backend web1 {
.host =
"192.168.0.203"
;
#后端web1服务器ip
.port =
"80"
;
#通过80端口来访问后端web
.connect_timeout = 1s;
#连接超时时间
.probe = {
.url =
"/"
;
.interval = 5s;
.timeout = 1 s;
#检测超时时间
.window = 5;
.threshold = 3;
}
}
backend web2 {
.host =
"192.168.0.204"
;
.port =
"80"
;
.connect_timeout = 1s;
#连接超时时间
.probe = {
.url =
"/"
;
.interval = 5s;
.timeout = 1 s;
#检测超时时间
.window = 5;
.threshold = 3;
}
}
director default round-robin {
#定义后端服务器组,默认轮询方式
{
.backend = web1;
}
{
.backend = web2;
}
}
sub vcl_recv {
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.request !=
"GET"
&&
req.request !=
"HEAD"
&&
req.request !=
"PUT"
&&
req.request !=
"POST"
&&
req.request !=
"TRACE"
&&
req.request !=
"OPTIONS"
&&
req.request !=
"DELETE"
) {
return
(pipe);
}
if
(req.request !=
"GET"
&& req.request !=
"HEAD"
) {
return
(pass);
}
if
(req.http.Authorization || req.http.Cookie) {
return
(pass);
}
return
(lookup);
}
sub vcl_pipe {
return
(pipe);
}
sub vcl_pass {
return
(pass);
}
sub vcl_hash {
hash_data(req.url);
if
(req.http.host) {
hash_data(req.http.host);
}
else
{
hash_data(server.ip);
}
return
(
hash
);
}
sub vcl_hit {
return
(deliver);
}
sub vcl_miss {
return
(fetch);
}
sub vcl_fetch {
if
(beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary ==
"*"
) {
set
beresp.ttl = 120 s;
return
(hit_for_pass);
}
return
(deliver);
}
sub vcl_deliver {
return
(deliver);
}
sub vcl_error {
set
obj.http.Content-Type =
"text/html; charset=utf-8"
;
set
obj.http.Retry-After =
"5"
;
synthetic {"
<?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>
"} + obj.status + "
" + obj.response + {"
<
/title
>
<
/head
>
<body>
<h1>Error
"} + obj.status + "
" + obj.response + {"
<
/h1
>
<p>
"} + obj.response + {"
<
/p
>
<h3>Guru Meditation:<
/h3
>
<p>XID:
"} + req.xid + {"
<
/p
>
<hr>
<p>Varnish cache server<
/p
>
<
/body
>
<
/html
>
"};
return
(deliver);
}
sub vcl_init {
return
(ok);
}
sub vcl_fini {
return
(ok);
}
|
1
|
/usr/local/varnish/sbin/varnishd
-f
/usr/local/varnish/etc/varnish/default
.vcl -s malloc,512M -g varnish -u varnish -T 127.0.0.1:2000 -a 0.0.0.0:80
|
1
2
3
4
5
6
7
8
|
/usr/local/
var
nish/bin/
var
nishncsa -n /usr/local/
var
nish/
var
/
var
nish/test.com -a -w /usr/local/
var
nish/access.log & #将
var
nish访问日志写入到access.log里
/usr/local/
var
nish/
var
/
var
nish/test.com :默认缓存目录
netstat -tuplan |grep
var
nish #查看是否启动
2000
和
80
端口
tcp
0
0
0.0
.
0.0
:
80
0.0
.
0.0
:* LISTEN
28551
/
var
nishd
tcp
0
0
127.0
.
0.1
:
2000
0.0
.
0.0
:* LISTEN
28550
/
var
nishd
tcp
0
0
:::
80
:::* LISTEN
28551
/
var
nishd
curl -I http:
//ip #使用curl命令查看是否被缓存
killall -
9
var
nishd #关闭
var
nish
|
1
|
echo
'/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,512M -g varnish -u varnish -T 127.0.0.1:2000 -a 0.0.0.0:80'
>>
/etc/rc
.
local
|