码迷,mamicode.com
首页 > 其他好文 > 详细

varnish 的安装,配置及负载均衡

时间:2018-06-28 18:18:59      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:清除缓存   erro   reads   访问   mod   root   turn   tor   IV   

系统环境:rhel6.5 server8.9安装http服务
server7 172.25.35.7
server8 172.25.35.8
server9 172.25.35.9
[root@server7 ~]# cd varnish/
[root@server7 varnish]# ls
bansys.zip varnish-libs-3.0.4-1.el6.x86_64.rpm
rhel6 varnish.pdf varnish-libs-3.0.5-1.el6.x86_64.rpm
varnish-3.0.4-1.el6.x86_64.rpm Varnish权威指南-中文版.pdf
varnish-3.0.5-1.el6.x86_64.rpm
[root@server7 varnish]# yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[root@server7 varnish]# vim /etc/varnish/default.vcl
技术分享图片
[root@server7 varnish]# /etc/init.d/varnish start
[root@server7 varnish]# curl 172.25.35.8 简单测试一下
[root@server7 varnish]# vim /etc/sysconfig/varnish
7 # Maximum number of open files (for ulimit -n)
8 NFILES=65535
9
10 # Locked shared memory (for ulimit -l)
11 # Default log size is 82MB + header
12 MEMLOCK=64000
13
14 # Maximum number of threads (for ulimit -u)
15 NPROCS="unlimited"
66 VARNISH_LISTEN_PORT=80
[root@server7 varnish]# vim /etc/security/limits.conf
51 varnish - nofile 65535
server8 写 测试页
[root@server8 ~]# touch /var/www/html/index.html
[root@server8 ~]# echo "<h1>server8<h1>">/var/www/html/index.html
物理机测试:
[root@localhost ~]# vim /etc/hosts
172.25.35.7 server7 www.redhat.org #解析
[root@localhost ~]# curl 172.25.35.7
<h1>server8<h1>
[root@localhost ~]# curl www.redhat.org
<h1>server8<h1>
浏览器访问:
技术分享图片

###查看缓存命中情况
[root@server7 ~]# vim /etc/varnish/default.vcl
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);
}
技术分享图片

[root@server7 ~]# /etc/init.d/varnish reload
###测试缓存命中###第一次为MISS,第二次以后为HIT
[root@localhost ~]# curl -I www.redhat.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Thu, 28 Jun 2018 07:01:14 GMT
ETag: "3fe8f-10-56fae4c10bfcc"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Thu, 28 Jun 2018 07:43:48 GMT
X-Varnish: 657573205
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache

[root@localhost ~]# curl -I www.redhat.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Thu, 28 Jun 2018 07:01:14 GMT
ETag: "3fe8f-10-56fae4c10bfcc"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Thu, 28 Jun 2018 07:45:02 GMT
X-Varnish: 657573206 657573205
Age: 73
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache

通过 varnishadm 手动清除缓存

通过 varnishadm 手动清除缓存

varnishadm ban.url .*$ #清除所有#
varnishadm ban.url /index.html #清除 index.html 页面缓存#
varnishadm ban.url /admin/$ #清除 admin 目录缓存
定义多个不同域名站点的后端服务器
[root@server7 ~]# vim /etc/varnish/default.vcl
sub vcl_recv { if (req.http.host ~ "^(www.)?redhat.org") { set req.http.host = "www.redhat.org"; set req.backend = web1; } elsif (req.http.host ~ "^bbs.redhat.org") { set req.backend = web2; } else {error 404 "redhat cache"; }}
#当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据,访问其他页面报错。
技术分享图片
[root@server7 ~]# /etc/init.d/varnish reload
Loading vcl from /etc/varnish/default.vcl
Current running config name is reload_2018-06-28T15:43:07
Using new config name reload_2018-06-28T16:13:06
VCL compiled.

available 0 boot
available 2 reload_2018-06-28T15:43:07
active 0 reload_2018-06-28T16:13:06
物理机测试:
[root@localhost ~]# curl bbs.redhat.org
server9
[root@localhost ~]# curl www.redhat.org
<h1>server8<h1>
浏览器访问:
技术分享图片
技术分享图片
技术分享图片

定义负载均衡
#定义健康检查 ##可不配置
probe healthcheck { .url = "/index.html"; # 哪个 url 需要 varnish 请求 .interval = 5s; #检查的间隔时间
.timeout = 1s; #等待多长时间探针超时
.window = 5; #维持 5 个 sliding window 的结果
.threshold = 3; #至少有三次 window 是成功的,就宣告 bachend 健康 }

[root@server7 ~]# vim /etc/varnish/default.vcl
director lb round-robin { #把多个后端聚合为一个组,并检测后端健康状况 { .backend = web1; }
{ .backend = web2; }
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?redhat.org") {
set req.http.host = "www.redhat.org";
set req.backend = lb ;
return (pass); #为了测试方便,不进行缓存。
} elsif (req.http.host ~ "^bbs.redhat.org") {
set req.backend = web2;
} else {error 404 "redhat cache";
}
}
物理机测试;
[root@localhost ~]# for i in {1..10}; do curl www.redhat.org;done
<h1>server8<h1>
<h1>server 9 </h1>
<h1>server8<h1>
<h1>server 9 </h1>
<h1>server8<h1>
<h1>server 9 </h1>
<h1>server8<h1>
<h1>server 9 </h1>
<h1>server8<h1>
<h1>server 9 </h1>

varnish 的安装,配置及负载均衡

标签:清除缓存   erro   reads   访问   mod   root   turn   tor   IV   

原文地址:http://blog.51cto.com/13810716/2133834

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!