系统环境:rhel6.5 server8.9安装http服务
server7 172.25.35.7
server8 172.25.35.8
server9 172.25.35.9
[[email protected] ~]# cd varnish/
[[email protected] 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
[[email protected] varnish]# yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[[email protected] varnish]# vim /etc/varnish/default.vcl
[[email protected] varnish]# /etc/init.d/varnish start
[[email protected] varnish]# curl 172.25.35.8 简单测试一下
[[email protected] 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
[[email protected] varnish]# vim /etc/security/limits.conf
51 varnish - nofile 65535
server8 写 测试页
[[email protected] ~]# touch /var/www/html/index.html
[[email protected] ~]# echo "<h1>server8<h1>">/var/www/html/index.html
物理机测试:
[[email protected] ~]# vim /etc/hosts
172.25.35.7 server7 www.redhat.org #解析
[[email protected] ~]# curl 172.25.35.7
<h1>server8<h1>
[[email protected] ~]# curl www.redhat.org
<h1>server8<h1>
浏览器访问:
###查看缓存命中情况
[[email protected] ~]# 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);
}
[[email protected] ~]# /etc/init.d/varnish reload
###测试缓存命中###第一次为MISS,第二次以后为HIT
[[email protected] ~]# 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
[[email protected] ~]# 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 目录缓存
定义多个不同域名站点的后端服务器
[[email protected] ~]# 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 取数据,访问其他页面报错。
[[email protected] ~]# /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
物理机测试:
[[email protected] ~]# curl bbs.redhat.org
server9
[[email protected] ~]# 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 健康 }
[[email protected] ~]# 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";
}
}
物理机测试;
[[email protected] ~]# 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>
原文地址:http://blog.51cto.com/13810716/2133834