标签:varnish
Varnish 是一个 web 加速器,被安装在 web 应用程序前面,缓存 web 应用程序,并响应
用户请求,varnish 让您的 web 应用程序运行的更快,并且 varnish 灵活好用。
1.varnish安装
需要软件包:varnish-libs-3.0.5-1.el6.x86_64.rpm varnish-3.0.5-1.el6.x86_64.rpm
yum install -y varnish-libs-3.0.5-1.el6.x86_64.rpm varnish-3.0.5-1.el6.x86_64.rpm
vim /etc/sysconfig/varnish
将66行VARNISH_LISTEN_PORT改为80
66 VARNISH_LISTEN_PORT=80
vim /etc/varnish/default.vcl
backend default {
.host = "127.0.0.1";
.port = "8080";
}
将port值改为8080
vim /etc/httpd/conf/httpd.conf
将136行Listen值改为8080
Listen 8080
/etc/init.d/httpd start
/etc/init.d/varnish start
netstat -antlpe
测试:
echo server1.example.com > /var/www/html/index.html
http://172.25.45.1
显示server1.example.com
vim /var/www/html/index.html ##修改iindex.html的内容,刷新网页后,网页内容更新
curl -I 172.25.45.1
curl -I 172.25.45.1/index.html
curl -dump 172.25.45.1
????测试:
可以更改/etc/sysconfig/varnish文件更新时间的设定
94 # # Default TTL used when the backend does not specify one
95 VARNISH_TTL=5
/etc/init.d/varnish restart
echo test....> /var/www/html/index.html
5秒后刷新页面则内容更改
可用curl -I localhost 查看时间
curl -I localhost/index.html
curl -dump localhost/index.html##显示内容
##查看缓存命中情况
vim /etc/varnish/default.vcl
在backend default下添加以下内容:
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/init.d/varnish reload
测试:
curl -I 172.25.45.1/index.html
会多出一行X-Cache,显示缓存命中情况
通过curl 172.25.45.1/index.html和curl 172.25.45.1查看缓存,然后修改iindex.html的内容,curl不更新,可以通过varnishadm ban.url .*$清除所有缓存
varnishadm ban.url /index.html#清除 index.html 页面缓存
##定义多个不同域名站点的后端服务器
vim /etc/varnish/default.vcl
在backend default下添加以下内容:
backend server2 {
.host = "172.25.45.2";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = default;
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = server2;
} else {error 404 "westos cache";
}
}
echo server1-www.westos.org > /var/www/html/index.html##server1虚拟机
echo server2-bbs.westos.org > /var/www/html/index.html##server2虚拟机
/etc/init.d/varnish reload
在物理机中修改/etc/hosts文件
vim /etc/hosts
http://172.25.45.1/
http://www.westos.org/
http://bbs.westos.org/
#把多个后端聚合为一个组,并检测后端健康状况
vim /etc/varnish/default.vcl
在backend server2下添加以下内容:
director westoslb round-robin {
{.backend = default;}
{.backend = server2;}
}
将sub vcl_recv中第一个req.backend值改为westoslb
【server2】
vim /etc/httpd/conf/httpd.conf
将990行 NameVirtualHost *:80 的注释取消
并在最后添加以下内容:
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.westos.org
serveralias westos.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /bbs
ServerName bbs.westos.org
</VirtualHost>
mkdir /bbs
echo bbs.westos.org > /bbs/index.html
echo server2-www.westos.org > /var/www/html/index.html
/etc/init.d/httpd restart
【server1】
/etc/init.d/varnish reload
http://bbs.westos.org/
显示bbs.westos.org
http://www.westos.org/
一开始显示server1-www.westos.org,过一会儿显示server2-www.westos.org
在set req.backend = westoslb;下添加以下内容:
return (pass);
/etc/init.d/varnish reload
测试:
http://www.westos.org/
不断刷新,页面在server1-www.westos.org与server2-www.westos.org中切换
/etc/init.d/httpd stop
页面将停在server2-www.westos.org,而不会切换
bansys
所需软件包:bansys.zip
yum install -y unzip
unzip bansys.zip -d /var/www/html/
yum install -y php
cd /var/www/html/bansys/
mv * ..
cd ..
rm -rf bansys
vim config.php
将 $var_group1的‘host‘ => array的值改为当前虚拟机IP,端口改为6082;并将$var_group3注释掉
将$VAR_CLUSTER的内容改为‘www.westos.org‘ => $var_group1,多余部分删掉
/etc/init.d/httpd restart
测试:
http://172.25.45.1:8080/index.php
vim /etc/varnish/default.vcl
在sub vcl_recv下面添加以下内容:
if (req.request == "BAN") {
if (!client.ip ~ westos) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
在最上面添加以下内容:
acl westos {
"127.0.0.1";
"172.25.45.0"/24;
}
并将return (pass);注释掉
/etc/init.d/varnish reload
测试:
http://www.westos.org/index.html
显示server1-www.westos.org,且刷新时不能切换
http://172.25.45.1:8080/index.php
选择HTTP,在推送内容中输入/index.html,并提交
打开http://www.westos.org/index.html,刷新后页面变为server2-www.westos.org
重复上述步骤,页面会切换成server1-www.westos.org
标签:varnish
原文地址:http://wjl19940429.blog.51cto.com/11354210/1792392