实验环境:物理机(访问测试): willis.example.com 172.25.254.6 varnish缓存端: server1.example.com 172.25.254.1
负载均衡端1: web1.example.com 172.25.254.10
负载均衡端2: web2.example.com 172.25.254.20
实验内容:1.解压安装包 2.配置数据库信息 3.安装varnish,http与php 4.配置varnish 5.访问端测试 6.cdn推送 实验前提:负载均衡端已配置好虚拟主机 实验安装包:bansys.zip varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
1.解压安装包 [iyunv@server1 mnt]# ls
bansys.zip
[iyunv@server1 mnt]# unzip bansys.zip -d /usr/local/lnmp/
2.配置数据库信息
[iyunv@server1 mnt]# vim /var/www/html/bansys/config.php #只保留如下设置,其余注释掉
<?php ///////////////////////数据库信息///////////////////////// //数据库信息 # $host = "localhost"; # $user = ""; # $passwd = ""; # $dbname = "bansys"; # $conn = mysql_connect($host, $user, $passwd) or die('Unable to connect database...'); # mysql_select_db($dbname,$conn); # $query = "select ResourceIP from purgeapp_resource"; # $results = mysql_query($query,$conn); # # while ($row=mysql_fetch_row($results)) { # $varnish_host[] = $row[0]; # } # #mysql_close($conn); ////////////////////////////////////////////////////////
//varnish主机列表 //可定义多个主机列表 $var_group1 = array( 'host' => array('172.25.254.1'), 'port' => '6082', );
//这个使用了mysql读取数据 # $var_group3 = array( # 'host' => $varnish_host, # 'port' => '6082', # );
//varnish群组定义 //对主机列表进行绑定 $VAR_CLUSTER = array( 'www.willis.com' => $var_group1, );
//varnish版本 //2.x和3.x推送命令不一样 $VAR_VERSION = "3"; ?>
3.安装varnish,http与php [iyunv@server1 mnt]# ls
varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[iyunv@server1 mnt]# yum install * -y
[iyunv@server1 mnt]# vim /etc/sysconfig/varnish VARNISH_LISTEN_PORT=80 ##设定varnish的端口为80 [iyunv@server1 mnt]# /etc/init.d/varnish start [iyunv@server1 html]# yum install php httpd -y [iyunv@server1 html]# vim /etc/httpd/conf/httpd.conf 136 Listen 8080
402 DirectoryIndex index.php index.html index.html.var
[iyunv@server1 html]# mv bansys/ upindex
[iyunv@server1 html]# /etc/init.d/httpd start
4.配置varnish [iyunv@server1 mnt]#vim /etc/varnish/default.vcl backend web1 {
.host = "172.25.254.20";
.port = "80";
}
backend web2 {
.host="172.25.254.10";
.port="80";
}
director willislb round-robin {
{ .backend = web1; }
{ .backend = web2; }
}
sub vcl_deliver{
if(obj.hits>0){
set resp.http.X-Cache="HIT from willis cache";
}
else{
set resp.http.X-Cache="MISS from willis cache";
}
return(deliver);
}
acl upindex {
"127.0.0.1";
"172.25.254.0"/24;
}
sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ upindex) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
if (req.http.host ~ "^(www.)?willis.com" ) {
set req.http.host = "www.willis.com";
set req.backend = willislb;
#return(pass); ###有缓存,用来测试缓存平台
}
elsif (req.http.host ~ "^(www.)?linux.com" ) {
set req.http.host = "www.linux.com";
set req.backend = willislb;
return(pass);
}
else {error 404 "willis cache";
}
}
5.访问端测试: [iyunv@willis Desktop]# curl -I http://www.willis.com/index.html
X-Cache: MISS from willis cache
[iyunv@willis Desktop]# curl -I http://www.willis.com/index.html
X-Cache: HIT from willis cache
[iyunv@willis Desktop]# curl -I http://www.willis.com/index.html
X-Cache: HIT from willis cache
6.cdn推送:
推送完之后测试:
X-Cache: MISS from willis cache
|