打开2台虚拟机
一、varnish基础搭建
在vm1上
vim /etc/security/limits.conf
修改本地最大文件数
vim /etc/sysconfig/varnish
修改varnish最大文件数,监听端口
NFILES=65535
最大文件数
VARNISH_LISTEN_PORT=80
监听
vim /etc/varnish/defa
搭建http服务的虚拟机ip
backend default {
.host = "172.25.9.2";
.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);
}
/etc/init.d/varnish restart
重起varnish服务
在vm2上
yum install -y httpd
安装http服务
vim /var/www/html/index.html
编辑主页
在物理机上
curl -I 172.25.9.1
查看缓存命中情况
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 25 Apr 2018 05:53:28 GMT
ETag: "7fc1a-17-56aa5e3da9b3e"
Content-Type: text/html; charset=UTF-8
Content-Length: 23
Accept-Ranges: bytes
Date: Wed, 25 Apr 2018 06:04:53 GMT
X-Varnish: 1476892690 1476892689
Age: 90
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
二、varnish控制访问内容
在vm1上
yum install -y httpd
vim /etc/httpd/conf/httpd.conf
Listen 8080
修改80端口为8080,varnish为80的端口
vim /var/www/html/index.html
修改主页面
vim /etc/sysconfig/varnish
backend web1 {
定义多个不同域名站点的后端服务器
.host = "172.25.9.1";
.port = "8080";
}
backend web2 {
.host = "172.25.9.2";
.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";
}
}
/etc/init.d/varnish reload
/etc/init.d/httpd start
在物理机上
curl www.westos.org
curl bbs.westos.org
三、varnish健康检查
在vm1上
vim /etc/sysconfig/varnish
把多个后端聚合为一个组,并检测后端健康状况
director lb round-robin {
{ .backend = web1; }
{ .backend = web2; }
}
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 = web2;
} else {error 404 "westos cache";
}
}
/etc/init.d/varnish reload
在vm2上
vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName bbs.westos.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www
ServerName www.westos.org
</VirtualHost>
/etc/init.d/httpd restart
在物理机上
curl www.westos.org
四、网页推送varnish
在vm1上
yum install php -y
unzip bansys.zip -d /var/www/html
vi /var/www/html/bansys/config.php
<?php
//varnish主机列表
//可定义多个主机列表
$var_group1 = array(
'host' => array('172.25.9.1'),
'port' => '80',
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
//varnish版本
//2.x和3.x推送命令不一样
$VAR_VERSION = "3";
?>
vim /etc/varnish/default.vcl
acl westos {
设置访问控制
"127.0.0.1";
"172.25.9.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";
}
}
在物理机上
firefox 打开http://172.25.9.1:8080/bansys/ 实现推送
查看推送页面访问www.westos.org/index.html
原文链接:https://blog.csdn.net/m0_37387597/article/details/80087130?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165934461816780366573479%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165934461816780366573479&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-14-80087130-null-null.nonecase&utm_term=%E6%90%AD%E5%BB%BAcdn
原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/6588