系统环境: rhel6 x86_64 iptables and selinux disabled

主机: 192.168.122.160:virnish apache server60.example.com

192.168.122.154:apache server54.example.com

192.168.122.1 desktop36.example.com

相关网址: http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/


所需的包:varnish-libs-3.0.4-1.el6.x86_64.rpm varnish-3.0.4-1.el6.x86_64.rpm bansys.zip


VCL处理流程图

 

处理过程大致分为如下几个步骤:

(1)Receive 状态,也就是请求处理的入口状态,根据 VCL 规则判断该请求应该是 Pass

Pipe,或者进入 Lookup(本地查询)

(2)Lookup 状态,进入此状态后,会在 hash 表中查找数据,若找到,则进入 Hit 状态,否则进

miss 状态。

(3)Pass 状态,在此状态下,会进入后端请求,即进入 fetch 状态。

(4)Fetch 状态,Fetch 状态下,对请求进行后端的获取,发送请求,获得数据,并进行本地

的存储。

(5)Deliver 状态, 将获取到的数据发送给客户端,然后完成本次请求。


virnish的安装和配置

(1)安装

[root@server60 kernel]# rpm -ivh varnish-libs-3.0.4-1.el6.x86_64.rpm

[root@server60 kernel]# rpm -ivh varnish-3.0.4-1.el6.x86_64.rpm


(2)配置

#配置单个后端服务器

[root@server60 kernel]# vim /etc/sysconfig/varnish

VARNISH_LISTEN_PORT=80 (配置varnish监听端口)

[root@server60 kernel]# vim /etc/varnish/default.vcl

backend default {

.host = "192.168.122.160";

.port = "8080";

} (配置一个后端服务器)

[root@server60 kernel]# yum install httpd -y

[root@server60 kernel]# vim /etc/httpd/conf/httpd.conf

#Listen 12.34.56.78:80

Listen 8080 (配置apache监听端口)

[root@server60 kernel]# echo `hostname` > /var/www/html/index.html

[root@server60 kernel]# /etc/init.d/httpd start

[root@server60 kernel]# /etc/init.d/varnish start

[root@server60 kernel]# netstat -antple

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 10028 1501/varnishd

tcp 0 0 :::8080 :::* LISTEN 0 9967 1475/httpd

可查看到808080端口说明启动成功.


测试:访问192.168.122.160和执行curl -I server60.example.com能看到如下页面说明配置成功.

 

#查看缓存命中情况

[root@server60 kernel]# vim /etc/varnish/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);

}

[root@server60 kernel]# /etc/init.d/varnish reload

测试:执行curl -I server60.example.com能看到如下页面说明配置成功.

上图为无命中

上图为命中

 

 

 

#通过 varnishadm 手动清除缓存

 

# varnishadm ban.url .*$ #清除所有

 

# varnishadm ban.url /index.html #清除 index.html 页面缓存

 

# varnishadm ban.url /admin/$ #清除 admin 目录缓存

 

#配置多个不同域名站点的后端服务器

 

以下步骤在server60上实施:

 

[root@server60 ~]# vim /etc/varnish/default.vcl

 

backend server60 {

 

.host = "192.168.122.160";

 

.port = "8080";

 

}

 

backend server54 {

 

.host = "192.168.122.154";

 

.port = "80";

 

}

 

sub vcl_recv {

 

if (req.http.host ~ "^(www.)?westos.org") {

 

set req.http.host = "www.westos.org";

 

set req.backend =server60;

 

} elsif (req.http.host ~ "^bbs.westos.org") {

 

set req.backend = server54;

 

} else {

 

error 404 "westos cache";

 

}

 

}

 

#当访问 www.westos.org 域名时从 server60 上取数据,访问 bbs.westos.org 域名时到 server54 取数据, 访问其他页面报错。

 

[root@server60 ~]# echo www.westos.org > /var/www/html/index.html

 

[root@server60 ~]# /etc/init.d/httpd restart

 

[root@server60 ~]# /etc/init.d/varnish reload

 


 

以下步骤在server54上实施:

 

[root@server54 kernel]# yum install httpd -y

 

[root@server54 kernel]# echo bbs.westos.org > /var/www/html/index.html

 

[root@server54 kernel]# /etc/init.d/httpd start

 

 

 

测试:

 

:在访问的那台机子上/etc/hosts里要写上解析:

 

192.168.122.160 www.westos.org bbs.westos.org

 

访问www.westos.org和执行curl -I www.westos.org可看到如下页面说明成功.

访问bbs.westos.org和执行curl -I bbs.westos.org可看到如下页面说明成功.

 

#负载均衡

以下步骤在server54上实施:

[root@server54 ~]# vim /etc/httpd/conf/httpd.conf

NameVirtualHost *:80 (去掉注释)

 

<VirtualHost *:80>

DocumentRoot /var/www/html

ServerName bbs.westos.org

</VirtualHost>


<VirtualHost *:80>

DocumentRoot /var/www/westos.org

ServerName www.westos.org

</VirtualHost>

[root@server54 ~]# echo bbs.westos.org > /var/www/html/index.html

[root@server54 ~]# mkdir /var/www/westos.org

[root@server54 ~]# echo www2.westos.org > /var/www/westos.org/index.html

[root@server54 ~]# /etc/init.d/httpd restart


以下步骤在server60上实施:

[root@server60 ~]# vim /etc/varnish/default.vcl

backend server60 {

.host = "192.168.122.160";

.port = "8080";

}

backend server54 {

.host = "192.168.122.154";

.port = "80";

}

director lb round-robin {

{ .backend = server60; }

{ .backend = server54; }

}

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 ~ "^bbs.westos.org") {

set req.backend = server54;

} else {

error 404 "westos cache";

}

}

[root@server60 ~]# echo www1.westos.org > /var/www/html/index.html

[root@server60 ~]# /etc/init.d/varnish reload


测试:

分别访问bbs.westos.orgwww.westos.org

若访问bbs.westos.org出现如下页面,访问www.westos.org页面在www1.westos.orgwww2.westos.org之前切换说明配置成功

 

 

#varnish cdn 推送平台

以下步骤在desktop36上实施:

[root@desktop36 ~]# yum install httpd php -y

[root@desktop36 ~]# unzip bansys.zip -d /var/www/html/

[root@desktop36 ~]# cd /var/www/html/

[root@desktop36 html]# cp -r bansys/* /var/www/html/

[root@desktop36 html]# vim config.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('192.168.122.160'), (varnish所在主机的ip)

'port' => '6082',(varnish所在主机的/etc/sysconfig/varnish中的VARNISH_ADMIN_LISTEN_PORT所对应的端口一致)

);

//这个使用了mysql读取数据 /* $var_group3 = array(

'host' => $varnish_host,

'port' => '6082',

);*/

//varnish群组定义

//对主机列表进行绑定

$VAR_CLUSTER = array(

// 'cdn.hit008.com' => $var_group3,

'www.westos.org' => $var_group1,

);

//varnish版本

//2.x3.x推送命令不一样

$VAR_VERSION = "3"; (根据你所用varnish的版本设定)

 

以下步骤在server60上实施:

#bansys 有两种工作模式,分别是:telnet http 模式。

#telnet 模式需要关闭 varnish 服务管理端口的验证,注释掉/etc/sysconfig/varnish 文件中的“-S $ {VARNISH_SECRET_FILE}”这行,重启 varnish 服务即可。

#如果是 http 模式需要对 varnish 做以下设置:

[root@server60 ~]# vim /etc/varnish/default.vcl

acl westos {

"127.0.0.1";

"192.168.122.0"/24;

}

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 ~ "^bbs.westos.org") {

set req.backend = server54;

} else {

error 404 "westos cache";

}

 

if(req.request == "BAN") {

if (!client.ip ~ westos) {

error 405 "Not allowed.";

}

ban("req.url ~ " + req.url);

error 200 "ban added";

}

}

[root@server60 ~]# /etc/init.d/varnish reload


测试:

先访问下www.westos.org,然后按下图设置推送一次.

 

 

刷新下页面,若网页变成另一台机子上的页面,或者执行curl -I www.westos.org推送后X-CacheHIT from westos cache变成 MISS from westos cache则说明推送成功.

 

posted on 2015-03-31 16:08  ↗℡.Koen↘  阅读(201)  评论(0编辑  收藏  举报