yum安装varnish
1.安装varnish(从Centos7开始,varnish已被收入到epel仓库)
yum install epel-release -y
yum -y install varnish
以下开始配置文件
以上是两台web的IP地址设置
vi /etc/varnish/default.vcl
sub vcl_recv{
if (req.http.host ~ “(?i)^(www.)?aa.com
"
)
s
e
t
r
e
q
.
h
t
p
.
h
o
s
t
=
"
w
w
w
.
a
a
.
c
o
m
"
;
s
e
t
r
e
q
.
b
a
c
k
e
n
d
h
i
n
t
=
w
e
b
1
;
e
l
s
i
f
(
r
e
q
.
h
t
t
p
.
h
o
s
t
"
(
?
i
)
w
w
w
.
b
b
.
c
o
m
") { set req.htp.host = "www.aa.com"; set req.backend_hint = web1; } elsif (req.http.host ~ "(?i)^www.bb.com
")setreq.htp.host="www.aa.com";setreq.backendhint=web1;elsif(req.http.host "(?i)www.bb.com”) {
set req.backend_hint = web2;
return(hash);
}
}
判断当访问www.aa.com域名时从web1上取数据,访问www.bb.com域名是到web2取数据。
#添加一个Header标识,以判断缓存是否命中
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = “HIT FROM” + req.http.host;
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = “MISS FROM” + req.http.host;
}
return (deliver);
}
在hosto中加入域名
配置成功后去浏览器测试