vanish
varnish
Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好
打开三个虚拟机:配置ip分别为172.25.25.1,172.25.25.2,172.25.25.3
主机名分别为server1,server2,server3
主机环境: rhel6 selinux and iptables disabled
实验主机: server2:172.25.25.2 --->varnish主机
server1:172.25.25.1 --->apache主机
server3:172.25.25.3 --->apache主机
VCL处理流程图:
处理过程大致分为如下几个步骤:
(1)Receive 状态,也就是请求处理的入口状态,根据 VCL 规则判断该请求应该是 Pass 或
Pipe,或者进入 Lookup(本地查询)。
(2)Lookup 状态,进入此状态后,会在 hash 表中查找数据,若找到,则进入 Hit 状态,否则进
入 miss 状态。
(3)Pass 状态,在此状态下,会进入后端请求,即进入 fetch 状态。
(4)Fetch 状态,在 Fetch 状态下,对请求进行后端的获取,发送请求,获得数据,并进行本地
的存储。
(5)Deliver 状态, 将获取到的数据发送给客户端,然后完成本次请求。
1.安装varnish
varnish主机的配置:
在server2上安装ssh和varnish
http://repo.varnish-cache.org/redhat/varnish-3.0/el 6/ x 86_64/
下载varnish安装包:varnish-3.0.5-1.el6.x86_64.rpm和
varnish-libs-3.0.5-1.el6.x86_64.rpm进行安装
[root@server2 html]# yum install openssh-clients
[root@server2 html]# cd /mnt/
[root@server2 mnt]# yum install gcc
[root@server2 mnt]# rpm -ivh *rpm
apache主机1:
在server1上安装apache并开启服务
[root@server1 ~]# yum install httpd
[root@server1 ~]# /etc/init.d/httpd start
apache主机2:
在server3上安装apache并开启服务
[root@server1 ~]# yum install httpd
[root@server1 ~]# /etc/init.d/httpd start
2.配置
1)只有一个后端服务器时,配置如下
varnish主机上的配置:
[root@server2 mnt]# vim /etc/sysconfig/varnish #配置varnish服务端口
# 因后期测试httpd服务,所以将端口改为80
VARNISH_LISTEN_PORT=80
[root@server2 mnt]# vim /etc/security/limits.conf
varnish - npfile 131073
varnish - memlock 82000
varnish - nproc unlimited
[root@server2 mnt]# /etc/init.d/varnish start #开启服务
[root@server2 mnt]# vim /etc/varnish/default.vcl
#配置一个后端服务器
backend web1 {
.host = "172.25.25.1";
.port = "80";
}
#查看缓存命中情况
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache"; #命中
}
else {
set resp.http.X-Cache = "MISS from westos cache"; #未命中
}
return (deliver);
}
/etc/sysconfig/varnish文件内容:
vim /etc/security/limits.conf文件内容:
/etc/varnish/default.vcl文件内容:
apache主机1上的配置:
[root@server1 ~]# vim /var/www/html/index.html
www.redhat.org
在真实主机做本地解析:
[root@foundation25 ~]# vim /etc/hosts
172.25.25.2 www.redhat.org
在浏览器测试,访问172.25.25.2可以访问到172.25.25.1的内容
测试缓存命中
[root@foundation25 ~]# curl -I www.redhat.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 07:28:39 GMT
ETag: "9fa3e-f-5720a2d57f5de"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 07:45:26 GMT
X-Varnish: 768727449
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache #####未命中
[root@foundation25 ~]# curl -I www.redhat.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 07:28:39 GMT
ETag: "9fa3e-f-5720a2d57f5de"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 07:45:32 GMT
X-Varnish: 768727450 768727449
Age: 6
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache #######命中
通过 varnishadm 手动清除缓存
varnishadm ban.url .*$ #清除所有
varnishadm ban.url /index.html #清除 index.html 页面缓存
varnishadm ban.url /admin/$ #清除 admin 目录缓存
varnish主机上清除缓存
[root@server2 mnt]# varnishadm ban.url /index.html
在真实主机上测试:
[root@foundation25 ~]# curl www.redhat.org -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 07:28:39 GMT
ETag: "9fa3e-f-5720a2d57f5de"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 08:27:16 GMT
X-Varnish: 61603931 61603928
Age: 17
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
[root@foundation25 ~]# curl -I www.redhat.org/index.html
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 07:28:39 GMT
ETag: "9fa3e-f-5720a2d57f5de"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 08:27:22 GMT
X-Varnish: 61603932
Age: 0 # 缓存被清空
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
2)有多个后端服务器的配置
apache主机2上的配置:
[root@server3 ~]# vim /var/www/html/index.html
[root@server3 ~]# cat /var/www/html/index.html
bbs.westos.org
varnish主机上的配置:
[root@server2 mnt]# vim /etc/varnish/default.vcl
#定义多个不同域名站点的后端服务器
backend web1 {
.host = "172.25.25.1";
.port = "80";
}
backend web2 {
.host = "172.25.25.3";
.port = "80";
}
#当访问 www.westos.org 域名时从 web1 上取数据
#访问 bbs.westos.org 域名时到 web2 取数据,访问其他页面报错。
sub vcl_recv {
if (req.http.host ~ "^(www.)?redhat.org") {
set req.http.host = "www.redhat.org";
set req.backend = web1;
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {
error 404 "westos cache";
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
[root@server2 mnt]# /etc/init.d/varnish reload
/etc/varnish/default.vcl中添加和更改的内容
重新加载varnish
在真实主机上测试:
[root@foundation25 ~]# curl www.redhat.org -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 07:28:39 GMT
ETag: "9fa3e-f-5720a2d57f5de"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 08:27:16 GMT
X-Varnish: 61603931 61603928
Age: 17
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
[root@foundation25 ~]# curl -I bbs.westos.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Fri, 27 Jul 2018 08:16:02 GMT
ETag: "9fa19-f-571f6b8f9fa0a"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 08:29:58 GMT
X-Varnish: 61603934 61603933
Age: 2
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
3)负载均衡
apache主机2上的配置:
[root@server3 ~]# cat /var/www/html/index.html
bbs.westos.org
[root@server3 ~]# vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName bbs.westos.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www1
ServerName www.redhat.org
</VirtualHost>
[root@server3 ~]# mkdir /www1
[root@server3 ~]# cd /www1/
[root@server3 www1]# ls
[root@server3 www1]# vim index.html
[root@server3 www1]# cat index.html
www.redhat.org-server3
[root@server3 www1]# /etc/init.d/httpd restart
/etc/httpd/conf/httpd.conf的内容
varnish主机上的配置:
[root@server2 mnt]# vim /etc/varnish/default.vcl
#把多个后端聚合为一个组,并检测后端健康状况
director redhat round-robin { #把后端聚合为redhat组
{ .backend = web1;}
{ .backend = web2;}
}
sub vcl_deliver {
if (req.http.host ~ "^(www.)?redhat.org") {
set req.http.host = "www.redhat.org";
set req.backend = redhat; #把原来的web1组改为renhat组
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {
error 404 "westos cache";
}
}
[root@server2 mnt]# /etc/init.d/varnish reload
/etc/varnish/default.vcl中添加和更改的内容
重新加载
清除缓存并在真实主机上测试
[root@server2 mnt]# varnishadm ban.url /index.html
[root@foundation25 ~]# curl www.redhat.org/index.html
www.redhat.org-server3
[root@server2 mnt]# varnishadm ban.url /index.html
[root@foundation25 ~]# curl www.redhat.org/index.html
www.redhat.org
[root@server2 mnt]# varnishadm ban.url /index.html
[root@foundation25 ~]# curl www.redhat.org/index.html
www.redhat.org-server3
varnish主机
真实主机
3.varnish cdn 推送平台
在varnish主机上的配置:
需要安装 php 支持
[root@server2 mnt]# yum install httpd php unzip -y
[root@server2 mnt]# vim /etc/httpd/conf/httpd.conf
# 配置apache端口为8080
listen 8080
[root@server2 mnt]# ls
bansys.zip varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[root@server2 mnt]# unzip bansys.zip -d /var/www/html/
[root@server2 mnt]# cd /var/www/html/
[root@server2 html]# cd bansys/
[root@server2 bansys]# ls
class_socket.php config.php index.php purge_action.php static
[root@server2 bansys]# mv * ..
[root@server2 bansys]# ls
[root@server2 bansys]# cd ..
[root@server2 html]# ls
bansys class_socket.php config.php index.php purge_action.php static
[root@server2 html]# vim config.php #只保留如下设置,其余注释掉
//可定义多个主机列表
$var_group1 = array(
'host' => array('172.25.25.2'),
'port' => '80',
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.redhat.org' => $var_group1,
'cdn.ywjt.org' => $var_group1,
);
//varnish版本
//2.x和3.x推送命令不一样
$VAR_VERSION = "3";
?>
[root@server2 html]# /etc/init.d/httpd start
[root@server2 html]# netstat -antlp
#bansys 有两种工作模式,分别是:telnet 和 http 模式。
#telnet 模式需要关闭 varnish 服务管理端口的验证
#注释掉/etc/sysconfig/varnish 文件中的 “ -S ${VARNISH_SECRET_FILE}”这行,重启 varnish 服务即可。
#如果是 http 模式需要对 varnish 做以下设置:
[root@server2 html]# vim /etc/varnish/default.vcl
# 设置访问控制
acl westos {
"127.0.0.1";
"172.25.25.0"/24;
}
sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ westos) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
if (req.http.host ~ "^(www.)?redhat.org") {
set req.http.host = "www.redhat.org";
set req.backend = redhat;
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {
error 404 "westos cache";
}
}
[root@server2 html]# /etc/init.d/varnish start
[root@server2 html]# /etc/init.d/varnish reload
/etc/httpd/conf/httpd.conf配置http端口
/var/www/html/config.php文件内容:
/etc/varnish/default.vcl更改和添加的内容
在两个apache主机上的设置:
Server1:
[root@server1 ~]# /etc/init.d/httpd start
[root@server1 ~]# cat /var/www/html/index.html
www.redhat.org
Server3:
[root@server3 ~]# vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
DocumentRoot /www1
ServerName www.redhat.org
</VirtualHost>
[root@server3 ~]# /etc/init.d/httpd start
[root@server3 ~]# cat /www1/index.html
www.redhat.org-server3
server1:
server3:
/etc/httpd/conf/httpd.conf内容:
推送前:
浏览器访问www.redhat/index.html
cdn推送:
在浏览器访问172.25.25.2:8080
刷新页面: