標簽(空格分隔): 未分類
開始前先修改Varnish的緩存存儲方式,監(jiān)聽端口號
[root@varnish varnish]# vim varnish.params
VARNISH_LISTEN_PORT=80
VARNISH_STORAGE="file,/data/web/cache,2g"
[root@varnish varnish]# mkdir -pv /data/web/cache
[root@varnish varnish]# systemctl restart varnish
示例
在響應首部增加一個cache是否命中的字段X-cache
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
if ( obj.hits>0 ) {
set resp.http.X-cache = "HIT via" + server.ip;
} else {
set resp.http.X-cache = "Miss via" + server.ip;
}
}
[root@varnish varnish]# varnish_reload_vcl
客戶端測試
第一次請求
[root@client ~]# curl -I http://192.168.30.33/test.html
HTTP/1.1 200 OK
...
Via: 1.1 varnish-v4
X-cache: Miss via192.168.30.33 #第一次MISS
第二次請求
[root@client ~]# curl -I http://192.168.30.33/test.html
...
X-cache: HIT via192.168.30.33 #第二次命中
Connection: keep-alive
強制對某類資源的請求不檢查緩存:
例如對url中開頭為/admin或者/login的不檢查緩存
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
if (req.url ~ "(?i)^/(admin|login)") {
return(pass);
}
}
~]# vcl.load test1 de.vcl
200
~]# VCL compiled.
vcl.use test1
在服務器上創(chuàng)建2個對應目錄和創(chuàng)建index文件
[root@rs1 ~]# mkdir -pv /usr/share/nginx/html/{admin,login}
mkdir: 已創(chuàng)建目錄 "/usr/share/nginx/html/admin"
mkdir: 已創(chuàng)建目錄 "/usr/share/nginx/html/login"
[root@rs1 ~]# echo "admin " > /usr/share/nginx/html/admin/index.html
[root@rs1 ~]# echo "login " > /usr/share/nginx/html/login/index.html
在客戶端上測試
[root@client ~]# curl -I http://192.168.30.33/admin
HTTP/1.1 301 Moved Permanently
...
X-cache: Miss via192.168.30.33 #無論多少次,都不會查緩存
對于特定類型的資源,例如公開的圖片等,取消其私有標識,并強行設(shè)定其可以由varnish緩存的時長;
sub vcl_backend_response {
if (beresp.http.cache-control !~ "s-maxage") { #如果是非公用數(shù)據(jù),則進入判斷
if (bereq.url ~ "(?!)\.(jpg|jpeg|png|png|gif|css|js)") { #如果數(shù)據(jù)是以靜態(tài)文件結(jié)尾的話,則進入
unset bereq.http.Set-cookie; #取消請求報文的cookie值
set beresp.ttl=3600s; #設(shè)置響應報文的ttl
}
}
}
緩存對象的修剪:purge, ban
(1) 能執(zhí)行purge操作
sub vcl_purge {
return (synth(200,"Purged"));
}
(2) 何時執(zhí)行purge操作
sub vcl_recv {
if (req.method == "PURGE") { #當方法時purge時執(zhí)行purge操作。
return(purge);
}
...
}
客戶端測試
[root@client ~]# curl -I http://192.168.30.33/test.html
X-cache: HIT via192.168.30.33 #已緩存
[root@client ~]# curl -X PURGE http://192.168.30.33/test.html
<!DOCTYPE html>
<html>
<head>
<title>200 Purged</title> #修剪成功
</head>
<body>
<h1>Error 200 Purged</h1>
<p>Purged</p>
<h3>Guru Meditation:</h3>
<p>XID: 63</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
為了防止其他人隨意修剪,應該設(shè)置一個訪問控制
acl purges {
"127.0.0.1"/8;
}
sub vcl_recv {
if (req.method == "PURGE") {
if (client.ip ~ purges) {
return(purge);
} else {
return(synth(405,"Purging not allowed for" + clietn.ip));
}
}
}
在客戶端測試
[root@client ~]# curl -X PURGE http://192.168.30.33/test.html
<!DOCTYPE html>
<html>
<head>
<title>405 Purging not allowed for192.168.30.148</title>
</head>
<body>
<h1>Error 405 Purging not allowed for192.168.30.148</h1>
<p>Purging not allowed for192.168.30.148</p>
<h3>Guru Meditation:</h3>
<p>XID: 32841</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
BAN
在CLI命令行中直接BAN,也可以在配置文件中定義
varnishadm:
ban <field> <operator> <arg>
示例:
ban req.url ~ ^/javascripts
設(shè)定使用多個后端主機
backend default {
.host = "172.16.100.6";
.port = "80";
}
backend appsrv {
.host = "172.16.100.7";
.port = "80";
}
sub vcl_recv {
if (req.url ~ "(?i)\.php") {
set req.backend_hint = appsrv; #php資源轉(zhuǎn)發(fā)至appsrv處理
} else {
set req.backend_hint = default;
}
...
}
定義后端服務器組
使用前需要在vcl配置中導入模塊:import director
三步走:先定義后端主機,然后定義組,往組中添加主機,最后調(diào)用組
示例:
import directors; # load the directors
backend server1 {
.host =
.port =
}
backend server2 {
.host =
.port =
}
sub vcl_init { #在init 子函數(shù)中定義
new GROUP_NAME = directors.round_robin(); #創(chuàng)建組,并命名為GROUP_NAME,指定調(diào)度方法
GROUP_NAME.add_backend(server1); #為組添加服務器成員
GROUP_NAME.add_backend(server2);
}
sub vcl_recv {
# send all traffic to the bar director:
set req.backend_hint = GROUP_NAME.backend(); #組引用方法
}
#示例:基于cookie的session sticky:
sub vcl_init {
new h = directors.hash(); h為組名
h.add_backend(one, 1); // backend 'one' with weight '1'
h.add_backend(two, 1); // backend 'two' with weight '1'
}
sub vcl_recv {
// pick a backend based on the cookie header of the client
set req.backend_hint = h.backend(req.http.cookie);
}
后端主機健康檢測機制
varnish可以對后端主機進行健康檢測,動態(tài)進行移除或恢復后端主機調(diào)度列表
.probe:定義健康狀態(tài)檢測方法;
.url:檢測時請求的URL,默認為"/";
.request:發(fā)出的具體請求;
.request =
"GET /.healthtest.html HTTP/1.1"
"Host: www.magedu.com"
"Connection: close"
.window:基于最近的多少次檢查來判斷其健康狀態(tài);
.threshhold:最近.window中定義的這么次檢查中至有.threshhold定義的次數(shù)是成功的;
.interval:檢測頻度;
.timeout:超時時長;
.expected_response:期望的響應碼,默認為200;
健康狀態(tài)檢測的配置方式:
(1) probe PB_NAME = { }
backend NAME = {
.probe = PB_NAME;
...
}
(2) backend NAME {
.probe = {
...
}
}
示例:
probe check { #定義probe檢查項目
.url = "/test.html";
.window = 5;
.threshold = 3;
.interval = 2s;
.timeout = 1s;
}
backend default {
.host = "192.168.30.11";
.port = "80";
.probe = check; #引用probe名
}
在varniadm 命令接口中查看檢測狀況
backend.list
200
Backend name Refs Admin Probe
default(192.168.30.11,,80) 7 probe Healthy 5/5