CDN实现varnish缓存

实验环境:

server5:varnish机(ip:172.25.254.5)
server2:real server机(ip:172.25.254.2)
server3:real server机(ip:172.25.254.3)

server5配置

一、安装varnish相关软件

[root@server5 ~]# yum install -y varnish-libs-3.0.5-1.el6.x86_64.rpm 
[root@server5 ~]# yum install -y varnish-3.0.5-1.el6.x86_64.rpm 

二、修改varnish配置文件

vim /etc/sysconfig/varnish
修改varnish的端口
CDN实现varnish缓存_第1张图片
vim /etc/varnish/default.vcl

backend default {
  .host = "172.25.20.2";
  .port = "80";
}

三、修改内核显示,保证流量大时,服务正常运行

[root@server5 ~]# vim /etc/security/limits.conf
这里写图片描述

四、varnish主机配置

修改varnish配置文件

backend web1 {   ##访问web1时,指向172.25.20.2
  .host = "172.25.254.2";
  .port = "80";
}

backend web2 {	 ##访问web1时,指向172.25.20.3
  .host = "172.25.254.3";
  .port = "80";
}

sub vcl_recv {	 ##www.westos.org和westos.org指向同一地址,节省varnish空间
		 ##实质上,域名不同,varnish缓存地址不同
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";
}
}

sub vcl_deliver {   ##第一次访问时MISS,后续访问HIT,varnish默认清楚时间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);
}

测试:
在物理机做域名解析:

172.25.254.5    www.westos.org  bbs.westos.org  westos.org

CDN实现varnish缓存_第2张图片
CDN实现varnish缓存_第3张图片

五、varnish负载均衡设置

server5

1、修改配置文件

backend web1 {
  .host = "172.25.254.2";
  .port = "80";
}

backend web2 {
  .host = "172.25.254.3";
  .port = "80";
}
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";   
}
}

server3开启httpd虚拟主机设置

/etc/httpd/cong/httpd.conf
CDN实现varnish缓存_第4张图片
重新启动server3的httpd服务

测试:

物理机访问:
CDN实现varnish缓存_第5张图片

七、varnish推送管理

目的:当web有更新时,varish的cache实现同步更新
安装httpd,php,unzip
修改httpd端口8080
默认发布目录/var/www/html下解压 bansys.zip
修改varnish配置文件 /etc/varnish/default.vcl
acl westos {
"127.0.0.1";
"172.25.254.0"/24;   指定 254 网段的可以推送
}

backend web1 {
.host = "172.25.254.2";   指定服务器
.port = "80";
}

backend web2 {
.host = "172.25.254.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;    引用 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);
}

访问 172.25.254.5:8080 即可进行推送
varnishadm ban.url /index.html
清除 index.html 页面缓存
varnishadm ban.url /admin/$
清除 admin 目录缓存

你可能感兴趣的:(企业级运维方案)