一、安装varnish
在server1中安装两个包
varnish-3.0.5-1.el6.x86_64.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm
1.在server1中(varnish)配置varnish服务
vim /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80 ##varnish监听端口
在server1中安装两个包
varnish-3.0.5-1.el6.x86_64.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm
1.在server1中(varnish)配置varnish服务
vim /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80 ##varnish监听端口
vim /etc/varnish/default.vcl
backend default {
.host = "172.25.8.2";
.port = "80";
}
backend default {
.host = "172.25.8.2";
.port = "80";
}
然后开启服务
/etc/init.d/varnish start
测试
新开一台虚拟机(server2)
yum install httpd
yum install httpd
/etc/init.d/httpd start
vim /var/www/html/index.html
<h1>www.westos.org-sever2</h1>
vim /var/www/html/index.html
<h1>www.westos.org-sever2</h1>
在浏览器中输入172.25.8.1
会出现server2index.html内容
会出现server2index.html内容
二、缓存命中
在varnish中:
vim default.vcl
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);
}
vim default.vcl
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 ##使用reload重新加载
测试
curl www.westos.org
www.westos.org-sever2
vim /etc/hosts
172.25.8.1 server1 www.westos.org www.linux.org westos.org ##加入本地解析
www.westos.org-sever2
vim /etc/hosts
172.25.8.1 server1 www.westos.org www.linux.org westos.org ##加入本地解析
curl -I www.westos.org
三、varnishadm 清除缓存
varnishadm ban.url .*$ 清除所有
varnishadm ban.url /index.html 清除 index.html 页面缓存
varnishadm ban.url /admin/$ 清除 admin 目录缓存
varnishadm ban.url .*$ 清除所有
varnishadm ban.url /index.html 清除 index.html 页面缓存
varnishadm ban.url /admin/$ 清除 admin 目录缓存
四、定义多个不同域名站点的后端服务器
再开启一台虚拟机(server3)
yum install httpd
vim /var/www/html/index.html
<h1>www.linux.org</h1>
再开启一台虚拟机(server3)
yum install httpd
vim /var/www/html/index.html
<h1>www.linux.org</h1>
在varnish中:
vim default.vcl
vim default.vcl
backend web1 {
.host = "172.25.8.2";
.port = "80";
}
.host = "172.25.8.2";
.port = "80";
}
backend web2 {
.host = "172.25.8.3";
.port = "80";
}
.host = "172.25.8.3";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
} elsif (req.http.host ~ "^www.linux.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
} elsif (req.http.host ~ "^www.linux.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
/etc/init.d/varnish reload 重载
测试:
curl www.westos.org
www.westos.org-sever2
curl westos.org
www.westos.org-sever2
curl www.linux.org
www.linux.org
curl www.westos.org
www.westos.org-sever2
curl westos.org
www.westos.org-sever2
curl www.linux.org
www.linux.org
五、负载均衡
在server3中:
vim /etc/httpd/conf/httpd.conf
vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.linux.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www1
ServerName www.westos.org
</VirtualHost>
在server3中
mkdir /www1
vim /www1/index.html
<h1>www.westos.org-server3</h1>
mkdir /www1
vim /www1/index.html
<h1>www.westos.org-server3</h1>
/etc/init.d/httpd restart
[root@server3 ~]# curl www.linux.org
<h1>www.linux.org</h1>
[root@server3 ~]# curl www.linux.org
<h1>www.linux.org</h1>
[root@server3 ~]# curl www.westos.org
<h1>www.westos.org-server3</h1>
<h1>www.westos.org-server3</h1>
在varnish中:
vim default.vcl
backend web1 {
.host = "172.25.8.2";
.port = "80";
}
vim default.vcl
backend web1 {
.host = "172.25.8.2";
.port = "80";
}
backend web2 {
.host = "172.25.8.3";
.port = "80";
}
.host = "172.25.8.3";
.port = "80";
}
director lb round-robin {
{ .backend = web1; }
{ .backend = web2; }
}
{ .backend = web1; }
{ .backend = web2; }
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
#return (pass) 加入此行可以更加直观看到实验效果,正常情况下不加
} elsif (req.http.host ~ "^www.linux.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
#return (pass) 加入此行可以更加直观看到实验效果,正常情况下不加
} elsif (req.http.host ~ "^www.linux.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
/etc/init.d/varnish reload
测试:实现负载均衡
当server2的httpd闭
[root@foundation8 ~]# curl westos.org/index.html
<h1>www.westos.org-server3</h1>
[root@foundation8 ~]# curl westos.org/index.html
<h1>www.westos.org-server3</h1>
<h1>www.westos.org-server3</h1>
[root@foundation8 ~]# curl westos.org/index.html
<h1>www.westos.org-server3</h1>
当server3的httpd闭
[root@foundation8 ~]# curl westos.org/index.html
<h1>www.westos.org-sever2</h1>
[root@foundation8 ~]# curl westos.org/index.html
<h1>www.westos.org-sever2</h1>
当server2和server3的http都开启
[root@foundation8 ~]# curl westos.org
<h1>www.westos.org-server3</h1>
[root@foundation8 ~]# curl westos.org
<h1>www.westos.org-sever2</h1>
[root@foundation8 ~]# curl westos.org
<h1>www.westos.org-server3</h1>
<h1>www.westos.org-server3</h1>
[root@foundation8 ~]# curl westos.org
<h1>www.westos.org-sever2</h1>
[root@foundation8 ~]# curl westos.org
<h1>www.westos.org-server3</h1>
六、cdn推送平台搭建
在varnish主机上
yum install httpd
vim /etc/httpd/conf/http.conf
将监听端口改为8080,防止和varnish主机80端口冲突
vim /etc/httpd/conf/http.conf
将监听端口改为8080,防止和varnish主机80端口冲突
/etc/init.d/httpd restart
下载bansys.zip包
yum install php
yum install unzip
unzip bansys.zip -d /var/www/html
cd /var/www/html/bansys
mv * ..
cd ..
vim config.php
/etc/init.d/httpd start
<?php
yum install unzip
unzip bansys.zip -d /var/www/html
cd /var/www/html/bansys
mv * ..
cd ..
vim config.php
/etc/init.d/httpd start
<?php
//varnish主机列表
//可定义多个主机列表
$var_group1 = array(
'host' => array('172.25.8.1'),
'port' => '80',
);
//可定义多个主机列表
$var_group1 = array(
'host' => array('172.25.8.1'),
'port' => '80',
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
vim /etc/varnish/default.vcl
acl westos {
"127.0.0.1";
"172.25.8.0"/24;
}
"127.0.0.1";
"172.25.8.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 (!client.ip ~ westos){
error 405 "Not allowed.";
}
ban ("req.url ~ " + req.url);
error 200 "ban added";
}