系統環境: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>
轉載于:https://blog.51cto.com/13810716/2133834