主机环境: rhel6 selinux and iptables disabled
实验主机: 192.168.122.10 varnish server1.example.com
192.168.122.2 apache server2.example.com
192.168.122.3 apache server3.example.com
VCL 处理流程图
1:安装
http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/
# yum localinstall varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
2:配置:
将其监听端口修改为80端口以响应apache
# vim /etc/sysconfig/varnish
# # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=
VARNISH_LISTEN_PORT=80
# vim /etc/varnish/default.vcl
###配置一个后端服务器
backend web1 {
.host = "192.168.122.2";
.port = "80";
}
# /etc/init.d/varnish start
##查看缓存命中情况
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from data cache";
}
else {
set resp.http.X-Cache = "MISS from dat cache";
}
return (deliver);
}
# service varnish reload
[root@server1 ~]# curl -I 192.168.122.10
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Mon, 29 Jun 2015 12:41:02 GMT
ETag: "10c5-14-519a76705da97"
Content-Type: text/html; charset=UTF-8
Content-Length: 20
Accept-Ranges: bytes
Date: Mon, 29 Jun 2015 12:52:41 GMT
X-Varnish: 933387577
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from data cache #未命中
[root@server1 ~]# curl -I 192.168.122.10
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Mon, 29 Jun 2015 12:41:02 GMT
ETag: "10c5-14-519a76705da97"
Content-Type: text/html; charset=UTF-8
Content-Length: 20
Accept-Ranges: bytes
Date: Mon, 29 Jun 2015 12:53:09 GMT
X-Varnish: 933387578 933387577
Age: 28
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from data cache #命中
###通过 varnishadm 手动清除缓存
# varnishadm ban.url .*$
#清除所有
# varnishadm ban.url /index.html
#清除 index.html 页面缓存
# varnishadm ban.url /admin/$
#清除 admin 目录缓存
###定义多个不同域名站点的后端服务器
backend web1 {
.host = "192.168.122.2";
.port = "80";
}
backend web2 {
.host = "192.168.122.3";
.port = "80";
}
#当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据,
访问其他页面报错。
sub vcl_recv {
if (req.http.host ~ "^(www.)?plumxx.org") {
set req.http.host = "www.plumxx.org";
set req.backend = web1;
} elsif (req.http.host ~ "^bbs.plumxx.org") {
set req.backend = web2;
} else {error 404 "plumxx.cache";
}
}
# service varnish reload
当我们直接访问其ip时,就会出现如下报错
以域名的形式访问
###定义负载均衡
director lb round-robin { #定义其名称为lb
{.backend = web1;}
{.backend = web2; }
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?plumxx.org") {
set req.http.host = "www.plumxx.org";
set req.backend = lb; #注意这块儿要返回刚才定义的lb
} elsif (req.http.host ~ "^bbs.plumxx.org") {
set req.backend = web2;
} else {error 404 "plumxx.cache";
}
}
在server3上我们需要修改其配置文件
[root@server3 ~]# vim /etc/httpd/conf/httpd.conf
#
NameVirtualHost *:80 # 打开其虚拟端口
同时在最后一行加入
<VirtualHost *:80>
DocumentRoot /var/www/html/
ServerName www.plumxx.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/vhost1
ServerName bbs.plumxx.org
</VirtualHost>
[root@server3 ~]# mkdi /var/www/vhost1/
service httpd start
# service varnish reload
###varnish cdn 推送平台
需要安装 php 支持
# unzip bansys.zip -d /var/www/html
vim /var/www/html/config.php
$var_group1 = array(
‘host‘ => array(‘192.168.122.10‘),
‘port‘ => ‘8080‘, #因为80端口被占用这里定义其端口为8080
$var_group3 = array(
‘host‘ => $varnish_host,
‘port‘ => ‘6082‘,
);
同时修改server1上的apache端口
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80
Listen 8080
service http restart
#bansys 有两种工作模式,分别是:telnet 和 http 模式。
#telnet 模式需要关闭 varnish 服务管理端口的验证,注释掉/etc/sysconfig/varnish 文件中的“-S $
{VARNISH_SECRET_FILE}”这行,重启 varnish 服务即可。
#如果是 http 模式需要对 varnish 做以下设置
acl plumxx {
"127.0.0.1";
"192.168.122.0"/24;
}
sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ plumxx) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
}
# service varnish reload
varnish
原文:http://my.oschina.net/u/2368584/blog/507795