varnish
1.yum install varnish-libs-3.0.5-1.el6.x86_64.rpm -y ##安装
yum install varnish-3.0.5-1.el6.x86_64.rpm -y
[root@server1 ~]# cd /etc/varnish/ ##进入到varnish目录
[root@server1 varnish]# ls ##查看该目录里面的文件
default.vcl secret
[root@server1 varnish]# ll ##查看权限
total 8
-rw-r--r-- 1 root root 3119 Dec 2 2013 default.vcl
-rw------- 1 root root 37 Jul 23 15:48 secret
2.[root@server1 varnish]# vim /etc/sysconfig/varnish ##编辑vatnish配置文件
66 VARNISH_LISTEN_PORT=80 把该行注释去掉,更改端口为80
(1)[root@server1 varnish]# vim default.vcl ##编辑配置文件
配置一个后端服务器:通过连接172.25.26.2:80,让varnish 的cache获得2上的index.html的页面
7 backend default {
8 .host = "172.25.80.2";
9 .port = "80";10 }[root@server1 varnish]# /etc/init.d/varnish start ##开启vatnish
[root@server1 varnish]# /etc/init.d/varnish reload ##重载varnish改完其配置文件后一般要做的操作
在浏览器的地址栏里输入:172.25.80.1,即可显示出:server2
或者打开一个终端,执行命令:curl -I 172.25.80.1
(2)查看缓存命中情况
11 sub vcl_deliver {
12 if (obj.hits > 0) {
13 set resp.http.X-Cache = "HITfrom westos cache";
14 }
15 else {
16 set resp.http.X-Cache = "MISSfrom westos cache";
17 }
18 return (deliver);
19 }
/etc/init.d/varnish reload
测试:在shell中,curl -I172.25.80.1
X-Cache: MISS(HIT) from westos cache---增加的一行(MISS未命中,HIT命中)
(3)定义多个不同域名站点的后端服务器
7 backend web1 {
8 .host = "172.25.80.2";
9 .port = "80";
10 }
11 backend web2 {
12 .host = "172.25.80.3";
13 .port = "80";
14 }
15
16 sub vcl_recv {
17 if (req.http.host ~"^(www.)?westos.org") {
18 set req.http.host ="www.westos.org";
19 set req.backend = web1;
20 } elsif (req.http.host ~"^bbs.westos.org") {
21 set req.backend = web2;
22 } else {error 404 "westoscache";
23 }
24 } /etc/init.d/varnish reload
测试:
在浏览器中,输入:www.westos.org
可看到:server2-westos.org
在浏览器中,输入:bbs.westos.org
可看到:server3-bbs.westos.org
(4)定义负载均衡
director lb round-robin {
{ .backend = web1; }
{ .backend = web2; }
}
16 sub vcl_recv {
17 if (req.http.host ~"^(www.)?westos.org") {
18 set req.http.host ="www.westos.org";
19 set req.backend = lb;
20 } elsif (req.http.host ~"^bbs.westos.org") {
21 set req.backend = web1;
22 } else {error 404 "westoscache";
23 }
24 }
在后台服务端,建立虚拟主机:
(1)在server2上:
vim /etc/httpd/conf/httpd.conf ##编辑apache的主配置文件
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName server2
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/bbs
ServerName bbs.westos.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/westos
ServerName www.westos.org</VirtualHost>
(2)建立目录
mkdir /www/bbs -p
mkdir /www/westos
vim /www/bbs/index.html
<h1>server2-bbs.westos.org</h1>
vim /www/westos/index.html
<h1>server2-www.westos.org</h1>
测试:
在浏览器中,输入:www.westos.org
可看到:server2-westos.org
在浏览器中,输入:bbs.westos.org
可看到:server2-bbs.westos.org
3.通过varnishadm手动清除缓存
varnishadm ban.url /index.html ##清除 index.html 页面缓存
varnishadm ban.url .*$
三,需要安装PHP支持
yum install unzip -y ##安装
yum install httpd -yunzip bansys.zip -d /var/www/html ##解压到指定的目录
cd /var/www/html/bansys/
mv *(class_socket.php config.php index.php purge_action.php static)/var/www/html/
在/var/www/html/中,
rm -fr bansys/ ##删除
yum install php -y ##安装php
vim /var/www/html/config.php (注释掉数据库信息)
# $host = "localhost";
# $user = "";
# $passwd = "";
# $dbname = "bansys";
# $conn = mysql_connect($host, $user, $passwd) or die('Unable to connectdatabase...');
# 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);
27 $var_group1 = array(
28 'host' =>array('172.25.80.1'),
29 'port' => '80',
30 );
31
32
33
34 //varnish群组定义
35 //对主机列表进行绑定
36 $VAR_CLUSTER = array(
37 'www.westos.org'=> $var_group1,
38 );
vim /etc/varnish/default.vcl
acl westos{
"127.0.0.1";
"172.25.80.0"/24;
}
sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ westos) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
/etc/init.d/varnish reload
测试:在浏览器中,输入:www.westos.org/index.html
可以由server2--->server3
推送CDN:
输入:172.25.80.1:8080