########################varnish#############################
一.安装
yum install varnish-* -y
二.配置文件
1.
vim /etc/sysconfig/varnish
66 VARNISH_LISTEN_PORT=80---->更改端口为80
2.
vim /etc/varnish/default.vcl
1)##配置一个后端服务器:通过连接172.25.26.2:80,让varnish 的cache获得2上的index.html的页面
7 backend default {
8 .host = "172.25.26.2";
9 .port = "80";
/etc/init.d/varnish start
**测试:
在浏览器中;输入172.25.26.1
在shell中,命令行:curl -I172.25.26.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.26.1
如图:X-Cache: MISS(HIT) from westos cache---增加的一行(MISS未命中,HIT命中)
3)##定义多个不同域名站点的后端服务器
7 backend web1 {
8 .host = "172.25.26.2";
9 .port = "80";
10 }
11 backend web2 {
12 .host = "172.25.26.3";
13 .port = "80";
14 }
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
<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 -y
unzip 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
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.26.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.26.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.26.1:8080
varnish
最新推荐文章于 2021-08-24 10:39:34 发布