Varnish是一款高性能的开源HTTP加速器。

主机环境: rhel6 selinux and iptables disabled

 

实验主机: 192.168.0.250 varnish

           192.168.0.188 apache

           192.168.0.189 apache

下载软件:

1. 安装

http :/ / repo . varnish - cache . org / redha t / varnish -3.0 / e l 6 / x 86_64 /

yum localinstall -y varnish-3.0.3-1.el6.x86_64.rpm varnish-libs-3.0.3-1.el6.x86_64.rpm

 

2. 配置

# vi /etc/varnish/default.vcl

###配置一个后端服务器

backend web1 {

.host = "192.168.0.188";

.port = "80";

}

 

###查看缓存命中情况

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);

}

 

# vi /etc/sysconfig/varnish

###配置varnish端口

VARNISH_LISTEN_PORT=80

# service varnish start

 

###测试缓存命中

# curl -I 192.168.0.250

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Mon, 20 Aug 2012 15:22:19 GMT

ETag: "1c13aa-16-4c7b4135e08a6"

Content-Type: text/html; charset=UTF-8

Content-Length: 22

Accept-Ranges: bytes

Date: Fri, 24 Aug 2012 14:30:40 GMT

X-Varnish: 766467032

Age: 0

Via: 1.1 varnish

Connection: keep-alive

X-Cache: MISS from westos cache #未命中

 

# curl -I 192.168.0.250

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Mon, 20 Aug 2012 15:22:19 GMT

ETag: "1c13aa-16-4c7b4135e08a6"

Content-Type: text/html; charset=UTF-8

Content-Length: 22

Accept-Ranges: bytes

Date: Fri, 24 Aug 2012 14:30:54 GMT

X-Varnish: 766467033 766467032

Age: 14

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT from westos cache #命中

 

 

###定义访问控制列表以及缓存清除设置

acl westos {

"127.0.0.1";

"192.168.0.0"/24;

}

sub vcl_recv {

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

if (!client.ip ~ westos) {

error 405 "Not allowed.";

}

return (lookup);

}

 

}

sub vcl_hit {

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

set obj.ttl = 0s;

error 200 "Purged.";

}

}

sub vcl_miss {

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

error 404 "Not in cache.";

}

}

# service varnish reload

 

###定义多个不同域名站点的后端服务器

backend web1 {

.host = "192.168.0.188";

.port = "80";

}

backend web2 {

.host = "192.168.0.189";

.port = "80";

}

 

#当访问www.westos.org域名时从web1上取数据,访问bbs.westos.org域名时到web2取数据,

访问其他页面报错。

sub vcl_recv {

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

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

set req.backend = web1;

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

set req.backend = web2;

} else {

error 404 "westos cache";

}

}

 

###定义负载均衡

backend web1 {

.host = "192.168.0.2";

.port = "80";

.probe = {             #后端健康检查

 

.url = "/index.html";

.interval = 5s;

.timeout = 1 s;

.window = 5;

.threshold = 3;

}

}

 

backend web2 {

.host = "192.168.0.3";

.port = "80";

.probe = {

.url = "/index.html";

.interval = 5s;

.timeout = 1 s;

.window = 5;

.threshold = 3;

}

}

 

director lb round-robin {

#把多个后端聚合为一个组

{

.backend = web1;

}

{

 

.backend = web2;

}

}

 

###varnish 推送平台    #当有新内容提供的时候通知varnish

两种方式:PURGE BAN。

系统支持telnet和http接口方式的推送模式,而且同时支持目录和正则方式推送数据。

http://code.google.com/p/varnish-php-bansys/    下载管理软件

在另一台主机安装http,php,用来推送varnish。将bansys解压到/var/www/html/目录。cd  /var/www/html ,vim config.php,将数据库信息注释掉,在可定义多个主机列表下面修改,$var_group1=array( 'host'=>array('192.168.0.101'),'port'=>6082, 此端口为varnish的管理端口,在/etc/sysconfig/varnish 文件中定义 );

再将下面的mysql读取数据 注释掉。

在 对主机进行绑定 下面修改:$VAR_CLUSTER=array( 'www.westos.org'=>$var_group1, );

下面的varnish 版本保持默认3.

netstat -antlpe

使用http 推送:

vim /etc/varnish/default.vcl

添加acl:acl ban { "127.0.0.1";"192.168.0.0"/24;}

在sub vcl_rect { 下面添加:if (req.request=="BAN") { if (!client.ip~ban) { error 405 "Not allowed.";} ban ("req.url~" +req.url); error 200 "ban added.";}

然后在浏览器 192.168.0.101 ,选http cdn群组为www.westos.org (文件里面定义好的),推送内容 .* 或 /index.html ,再3确认就好了。

 

当用telnet 推送时,vim /etc/sysconfig/varnish

-S ${VARNISH_SECRET_FILE}\   将此行注释掉,要不然telnet需要认证。然后将varnish重启。telnet 192.168.0.101 6082 ,此时任何客户端都可以telnet,可以在/etc/varnish/default.vcl 文件里面添加acl 进行控制访问。