一、原理
用server1当作缓存的,主机请求server1,server1通过varnish设置,访问server2,将server2信息缓存下来,显示给主机,server1 起到一个缓存效果。
二、配置过程
1.安装varnish,获得如下安装包
并且在你做缓存的主机上安装它们
2.步骤:
(1)server1配置:
vim /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80 配置varnish服务端口,主机在访问的时候就不需要输入端口号,因为http端口就是80
/etc/init.d/varnish start 打开varnish服务
vim /etc/varnish/default.vcl
backend default { #配置后端服务器(请求缓存的主机server2).host = "172.25.39.2";.port = "80";
}
sub vcl_deliver { #查看缓存命中情况,若有缓存,就得到,没有就miss,缓存刷新时间为120s
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 reload #重启varnish服务
netstat -antlp 查看server1端口开向主机
(2)后台主机server2配置:
安装httpd服务
yum install httpd -y
/etc/init.d/httpd start
cd /var/www/html
vim index.html
server2
curl localhost 查看http配置是否正确
netstat -antl 查看实际访问自己的是谁
(3)250主机(访问主机):
curl -I 172.25.39.1 访问server1,其实server1从server2缓存数据,显示给主机
三、定义多个不同域名站点的后端服务器
1.server1配置,加一台server3,输入不同的域名,从不同的服务器缓存
server3操作如图:
vim /etc/sysconfig/varnish
backend web1 {.host = "172.25.39.2";.port = "80";
}
backend web2 { .host = "172.25.39.3";.port = "80";
}sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = web1; #当访问www.westos.org域名时,从web1上取数据
}elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2; #当访问bbs.westos.org域名时,从web2上取数据
}else {error 404 "westos cache"; #访问其他页面显示报错
}
}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);
}
测试主机配置如图:
四、限定参数
1.vim /etc/sysconfig/varnish
NFILES=65535 # Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=82000# Maximum number of threads (for ulimit -u)
NPROCS="unlimited"
2.vim /etc/security/limits.conf
varnish - nofile 65535
varnish - memlock 82000
varnish - nproc unlimited
3.sysctl -a | grep file
4.cat /etc/passwd 查看varnish用户的密码和所属shell
5.usermod -s /bin/bash varnish
6.su - varnish
7.ulimit -n
8.ulimit -l
五、清理缓存
varnishadm ban.url /index.html 清理此文件缓存
varnishadm ban.url .*$ 清理全部缓存(慎做)
varnishadm ban.url /admin/$ 清理admin目录缓存
六、把多个后端聚合成一个组,并检测后端健康情况
1.vim /etc/sysconfig/varnish
director lb round-robin {{ .backend = web1; } #当主机web1挂掉(stop),就访问web2,不挂掉,进行轮询{ .backend = web2; } #访问到web2时访问的是www.westos.org
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb; #当访问www.westos.org时,web1和web2进行轮询显示
return (pass); #为了测试方便,不进行缓存,浏览器就会轮询显示不同的页面
}elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
}else {error 404 "westos cache";
}
}
2.server3设置apache虚拟主机,不同域名访问的页面不一样
vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80DocumentRoot /var/www/htmlServerName bbs.westos.org
DocumentRoot /wwwServerName www.westos.org
mddir /www
cd /www
vim index.html
www.westos.org-server3
cd /var/www/html
vim index.html
bbs.westos.org-server3
vim /etc/hosts 加上地址解析
172.25.39.3 server3 www.westos.org bbs.westos.org/etc/init.d/httpd restart
3.测试:
curl www.westos.org
curl bbs.westos.org
显示结果为不同的页面,表示虚拟主机配置成功CD
七、CDN推送管理
cd pub/docs/varnish/ #在老师的主机上下载bansys,zip
get bansys.zip
yum install unzip -y #安装解压命令unzip
yum install php -y #安装php,因为cdn是php的一个页面
unzip bansys.zip #解压bansys.zip文件
cd bansys #切换到bansys目录
mv * /var/www/html/ #将解压的东西移动到/var/www/html目录下vim /etc/httpd/conf/httpd.conf #编辑httpd配置文件,改一下端口为8080
listen 8080vim config.php #修改此文件内容如下
前面没用的可以删掉
$var_group1 = array('host' => array('172.25.39.1'),'port' => '80',);//varnish群组定义//对主机列表进行绑定$VAR_CLUSTER = array('www.westos.org' => $var_group1,);/etc/init.d/httpd start #重启httpd服务
若不修改http文件里面的80端口会出现以下错误:
若修改了,结果为:
vim /etc/varnish/default.vcl
acl westos { #设置访问控制
"127.0.0.1";
"172.25.0.0"/24;
}backend web1 {.host = "172.25.39.2";.port = "80";
}
backend web2 {.host = "172.25.39.3";.port = "80";
}
director lb round-robin {{ .backend = web1; }{ .backend = web2; }
}sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ westos) {
error 405 "Not allowed";
}
ban("req.url ~" + req.url);
error 200 "ban added";
}
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
}elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
}else {error 404 "westos cache";
}
}
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 reload #重启varnish服务
推送后就清理缓存,访问www.westos.org就会出现轮询现象