环境:
三台虚拟机:
192.168.8.1 server1(varnish 服务器)
192.168.8.2 server2(后端主机http)
192.168.8.3 server3(后端主机http)
安装方式:yum(搭建略)
安装包:varnish-4.0.5-1.el7.x86_64.rpm
varnish-libs-4.0.5-1.el7.x86_64.rpm
jemalloc-3.6.0-1.el7.x86_64.rpm(依赖)
参考:https://my.oschina.net/u/144160/blog/1608021
一.反向代理
Server1:
Yum install -y varnish-4.0.5-1.el7.x86_64.rpm varnish-libs-4.0.5-1.el7.x86_64.rpm jemalloc-3.6.0-1.el7.x86_64.rpm
[root@server1 mnt]# rpm -qc varnish ##查看相关配置文件
/etc/logrotate.d/varnish
/etc/varnish/default.vcl
/etc/varnish/varnish.params
[root@server1 mnt]# vim /usr/lib/systemd/system/varnish.service
14 LimitNOFILE=131072 ##最大访问文件数量
18 LimitMEMLOCK=82000 ##运行占用的内存
[root@server1 mnt]# sysctl -a | grep file ##查看本机系统最大访问量
fs.file-max = 180302 ##系统最大文件访问量
fs.file-nr = 4032 0 180302
fs.xfs.filestream_centisecs = 3000
如果不满足需求,则需提升本机配置
[root@server1 mnt]# vim /etc/security/limits.conf ##编辑用户限制文件,在文件末尾添加如下(与配置文件相对应)
varnish - nofile 131072
varnish - memlock 82000
[root@server1 mnt]# vim /etc/varnish/varnish.params ##修改端口号为80
14 VARNISH_LISTEN_PORT=80
[root@server1 mnt]# vim /etc/varnish/default.vcl ##添加调度主机
backend defaults {
.host = "192.168.8.2";
.port = "80";
}
Server2:
[root@server2 ~]# yum install httpd
[root@server2 ~]# systemctl start httpd
[root@server2 ~]# vim /var/www/html/index.html
<h1> -AIX- </h1>
测试:
[root@h-74-1-50-168 ~]# curl 192.168.8.1
<h1> -AIX- </h1>
[root@h-74-1-50-168 ~]# curl 192.168.8.1
<h1> -AIX- </h1>
缓存命中测试:
Server1:
[root@server1 mnt]# vim /etc/varnish/default.vcl ##编辑以下模块
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT cache";
}
else {
set resp.http.X-Cache = "MISS cache";
}
return (deliver);
}
配置参数:
# 例子
# set resp.http.* 用来添加header头 如 set resp.http.xxxxx = "haha"; unset为删除
# set resp.status 用来设置返回状态 如 set resp.status = 404;
# obj.hits 会返回缓存命中次数 用于判断或赋值给header头
# req.restarts 会返回该请求经历restart事件次数 用户判断或赋值给header头
# 根据判断缓存时间来设置xxxxx-Cache header头
配置完成后,清除掉之前的缓存
[root@server1 mnt]# varnishadm ban req.url "~" /
[root@server1 mnt]# systemctl restart varnish
测试:
[root@h-74-1-50-168 ~]# curl -I 192.168.8.1
HTTP/1.1 200 OK
Date: Thu, 22 Aug 2019 07:57:34 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Thu, 22 Aug 2019 07:36:24 GMT
ETag: "12-590afbf2c95c0"
Content-Length: 18
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS cache
Connection: keep-alive
[root@h-74-1-50-168 ~]# curl -I 192.168.8.1
HTTP/1.1 200 OK
Date: Thu, 22 Aug 2019 07:57:34 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Thu, 22 Aug 2019 07:36:24 GMT
ETag: "12-590afbf2c95c0"
Content-Length: 18
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770 3
Age: 2
Via: 1.1 varnish-v4
X-Cache: HIT cache
Connection: keep-alive
二.多台主机访问
Server1:
[root@server1 mnt]# vim /etc/varnish/default.vcl
######添加后端主机#####
backend web1 { ##第一台
.host = "192.168.8.2";
.port = "80";
}
backend web2 { ##第二台
.host = "192.168.8.3";
.port = "80";
}
#####修改此模块如下###
sub vcl_recv {
if (req.http.host ~ "^(www.)?bss.org") {
set req.http.host = "www.bss.org";
set req.backend_hint = web1;
}
elsif (req.http.host ~ "^bss.westos.org"){
set req.backend_hint = web2;
}
else {
return (synth (405));
}
}
[root@server1 mnt]# systemctl restart varnish
[root@server1 mnt]# varnishadm ban req.url "~" /
Server3
[root@server3 ~]# yum install httpd -y
[root@server3 ~]# systemctl start httpd
[root@server3 ~]# vim /var/www/html/index.html
<h1> -bss- </h1>
测试:
在本地添加域名,通过域名进行访问
[root@h-74-1-50-168 ~]# vim /etc/hosts
192.168.8.1 www.bss.org bss.westos.org bss.org
[root@h-74-1-50-168 ~]# curl -i www.bss.org
HTTP/1.1 200 OK
Date: Thu, 22 Aug 2019 08:06:00 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Thu, 22 Aug 2019 07:36:24 GMT
ETag: "12-590afbf2c95c0"
Content-Length: 18
Content-Type: text/html; charset=UTF-8
X-Varnish: 32772
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS cache
Connection: keep-alive
Accept-Ranges: bytes
<h1> -AIX- </h1>
[root@h-74-1-50-168 ~]# curl -i www.bss.org
HTTP/1.1 200 OK
Date: Thu, 22 Aug 2019 08:06:00 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Thu, 22 Aug 2019 07:36:24 GMT
ETag: "12-590afbf2c95c0"
Content-Length: 18
Content-Type: text/html; charset=UTF-8
X-Varnish: 32775 32773
Age: 5
Via: 1.1 varnish-v4
X-Cache: HIT cache
Connection: keep-alive
Accept-Ranges: bytes
<h1> -AIX- </h1>
[root@h-74-1-50-168 ~]# curl -i bss.org
HTTP/1.1 200 OK
Date: Thu, 22 Aug 2019 08:30:34 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Thu, 22 Aug 2019 07:36:24 GMT
ETag: "12-590afbf2c95c0"
Content-Length: 18
Content-Type: text/html; charset=UTF-8
X-Varnish: 5 3
Age: 19
Via: 1.1 varnish-v4
X-Cache: HIT cache
Connection: keep-alive
Accept-Ranges: bytes
<h1> -AIX- </h1>
[root@h-74-1-50-168 ~]# curl -i bss.westos.org
HTTP/1.1 200 OK
Date: Thu, 22 Aug 2019 08:31:02 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Wed, 21 Aug 2019 07:14:59 GMT
ETag: "11-5909b54c1d8d0"
Content-Length: 17
Content-Type: text/html; charset=UTF-8
X-Varnish: 7
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS cache
Connection: keep-alive
Accept-Ranges: bytes
<h1> -bss- </h1>
[root@h-74-1-50-168 ~]# curl -i bss.westos.org
HTTP/1.1 200 OK
Date: Thu, 22 Aug 2019 08:31:02 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Wed, 21 Aug 2019 07:14:59 GMT
ETag: "11-5909b54c1d8d0"
Content-Length: 17
Content-Type: text/html; charset=UTF-8
X-Varnish: 32772 8
Age: 2
Via: 1.1 varnish-v4
X-Cache: HIT cache
Connection: keep-alive
Accept-Ranges: bytes
<h1> -bss- </h1>
三.负载均衡
# 配置后端集群事件
#sub vcl_init {
# 后端集群有4种模式 random, round-robin, fallback, hash
# random 随机
# round-robin 轮询
# fallback 后备
# hash 固定后端 根据url(req.http.url) 或 用户cookie(req.http.cookie) 或 用户session(req.http.sticky)(这个还有其他要配合)
Server1:
[root@server1 mnt]# vim /etc/varnish/default.vcl
import directors; ##加载后端轮询模块
##配置后端集群事件
sub vcl_init {
new ld = directors.round_robin(); ## ld为集群名,集群为轮询模式.
ld.add_backend(web1); ##集群成员,web1和web2
ld.add_backend(web2);
}
#####修改下方模块配置#####
sub vcl_recv {
if (req.http.host ~ "^(www.)?bss.org") {
set req.http.host = "www.bss.org";
set req.backend_hint = ld.backend(); ##指定使用名为ld的后端集群
return (pass);
}
elsif (req.http.host ~ "^bss.westos.org"){
set req.backend_hint = web2;
}
else {
return (synth (405));
}
}
[root@server1 mnt]# systemctl restart varnish
[root@server1 mnt]# varnishadm ban req.url "~" /
测试:
[root@h-74-1-50-168 ~]# curl bss.westos.org ##未对此地址做配置,所以不轮询
<h1> -bss- </h1>
[root@h-74-1-50-168 ~]# curl bss.westos.org
<h1> -bss- </h1>
[root@h-74-1-50-168 ~]# curl www.bss.org
<h1> -AIX- </h1>
[root@h-74-1-50-168 ~]# curl www.bss.org
<h1> -bss- </h1>
[root@h-74-1-50-168 ~]# curl bss.org
<h1> -AIX- </h1>
[root@h-74-1-50-168 ~]# curl bss.org
<h1> -bss- </h1>