varnish cdn 推送平台
实验环境:物理机(访问测试): willis.example.com172.25.254.6 varnish缓存端: server1.example.com172.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.解压安装包# ls
bansys.zip
# unzip bansys.zip -d /usr/local/lnmp/
2.配置数据库信息
# 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;# }# #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# ls
varnish-3.0.5-1.el6.x86_64.rpmvarnish-libs-3.0.5-1.el6.x86_64.rpm
# yum install * -y
# vim /etc/sysconfig/varnish VARNISH_LISTEN_PORT=80 ##设定varnish的端口为80# /etc/init.d/varnish start# yum install php httpd -y# vim /etc/httpd/conf/httpd.conf 136Listen 8080
402DirectoryIndex index.phpindex.html index.html.var
# mv bansys/upindex
# /etc/init.d/httpd start
4.配置varnish#vim /etc/varnish/default.vclbackend 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.访问端测试: # curl -I http://www.willis.com/index.html
X-Cache: MISS from willis cache
# curl -I http://www.willis.com/index.html
X-Cache: HIT from willis cache
# curl -I http://www.willis.com/index.html
X-Cache: HIT from willis cache
6.cdn推送:
推送完之后测试:
# curl -I http://www.willis.com/index.html X-Cache: MISS from willis cache
页:
[1]