1. 程式人生 > >varnish 的安裝,配置及負載均衡

varnish 的安裝,配置及負載均衡

tex erro RM 3.0.5 pdf -m ocs 報錯 定義

系統環境: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 的安裝,配置及負載均衡