02 varnish4基础应用
配置环境:
node1 CentOS7.2 192.168.1.131
[root@node1 ~]# yum -y install varnish
[root@node1 ~]# vim /etc/varnish/varnish.params
修改
VARNISH_STORAGE="file,/var/lib/varnish/varnish_storage.bin,1G"
为
VARNISH_STORAGE="malloc,256M"
[root@node1 ~]# vim /etc/varnish/default.vcl
修改backend default 段的内容为
backend default {
.host = "192.168.1.132";
.port = "80";
}
[root@node1 ~]# systemctl start varnish.service
访问测试页
[root@node3 ~]# curl 192.168.1.131:6081/test1.html
Page 1 on Web1
[root@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
200
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,3.10.0-327.el7.x86_64,x86_64,-smalloc,-smalloc,-hcritbit
varnish-4.0.3 revision b8c4a34
Type ‘help‘ for command list.
Type ‘quit‘ to close CLI session.
[root@node2 ~]# yum -y install httpd
[root@node2 ~]# for i in {1..10};do echo "Page $i on Web1" > /var/www/html/test$i.html;done
[root@node2 ~]# systemctl start httpd.service
[root@node2 ~]# systemctl enable httpd.service
03 varnish状态引擎详解
[root@node1 ~]# cd /etc/varnish/
[root@node1 varnish]# cp default.vcl test.vcl
[root@node1 varnish]# vim test.vcl
修改sub vcl_recv段的内容为:
sub vcl_recv {
if (req.method == "PRI") {
/* We do not support SPDY or HTTP/2.0 */
return (synth(405));
}
if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.method != "GET" && req.method != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
return (hash);
}
[root@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
200
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,3.10.0-327.el7.x86_64,x86_64,-smalloc,-smalloc,-hcritbit
varnish-4.0.3 revision b8c4a34
Type ‘help‘ for command list.
Type ‘quit‘ to close CLI session.
vcl.load test1 test.vcl
200
VCL compiled.
vcl.list
200
active 0 boot
available 0 test1
vcl.use test1
200
VCL ‘test1‘ now active
[root@node1 varnish]# vim test.vcl
在sub vcl_deliver 程序段添加
if (obj.hits>0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
[root@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test2 test.vcl
200
VCL compiled.
vcl.use test2
200
VCL ‘test2‘ now active
测试
1)同一个地址测试3次,结果显示为HIT
[root@node3 ~]# curl http://192.168.1.131:6081/test5.html
Page 5 on Web1
[root@node3 ~]# curl http://192.168.1.131:6081/test5.html
Page 5 on Web1
[root@node3 ~]# curl http://192.168.1.131:6081/test5.html
Page 5 on Web1
[root@node3 ~]# curl -I http://192.168.1.131:6081/test5.html
HTTP/1.1 200 OK
Date: Wed, 19 Oct 2016 02:27:57 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 18 Oct 2016 08:59:47 GMT
ETag: "f-53f1fea1d7c0d"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 65556 65554
Age: 14
Via: 1.1 varnish-v4
X-Cache: HIT
Connection: keep-alive
2)测试一次,结果显示为MISS
[root@node3 ~]# curl http://192.168.1.131:6081/test6.html
Page 6 on Web1
[root@node3 ~]# curl -I http://192.168.1.131:6081/test6.html
HTTP/1.1 200 OK
Date: Wed, 19 Oct 2016 02:39:41 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 18 Oct 2016 08:59:47 GMT
ETag: "f-53f1fea1d7ff5"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 12
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS
Connection: keep-alive
显示客户端IP
[root@node1 varnish]# vim test.vcl
修改sub vcl_deliver的内容为:
if (obj.hits>0) {
set resp.http.X-Cache = "HIT from" + server.ip;
} else {
set resp.http.X-Cache = "MISS from" + server.ip;
}
[root@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test3 test.vcl
200
VCL compiled.
vcl.use test3
200
VCL ‘test3‘ now active
[root@node3 ~]# curl -I http://192.168.1.131:6081/test6.html
HTTP/1.1 200 OK
Date: Wed, 19 Oct 2016 03:08:40 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 18 Oct 2016 08:59:47 GMT
ETag: "f-53f1fea1d7ff5"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770 3
Age: 5
Via: 1.1 varnish-v4
X-Cache: HIT from 192.168.1.131
Connection: keep-alive
[root@node3 ~]# curl -I http://192.168.1.131:6081/test7.html
HTTP/1.1 200 OK
Date: Wed, 19 Oct 2016 03:09:11 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 18 Oct 2016 08:59:47 GMT
ETag: "f-53f1fea1d7ff5"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 5
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from 192.168.1.131
Connection: keep-alive
04 varnish状态引擎及vcl
#添加后端
[root@node1 varnish]# vim test.vcl
在backend default段后添加
backend imgsrv {
.host = "192.168.1.133";
.port = "80";
}
#强制对某资源的请求,不检查缓存
#区分大小写(默认)
[root@node1 varnish]# vim test.vcl
在sub vcl_recv段中添加
if (req.url ~ "^/test7.html$") {
return(pass);
}
[root@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test4 test.vcl
200
VCL compiled.
vcl.use test4
200
VCL ‘test4‘ now active
[root@node3 ~]# curl -I http://192.168.1.131:6081/test7.html
HTTP/1.1 200 OK
Date: Wed, 19 Oct 2016 04:11:58 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 18 Oct 2016 08:59:47 GMT
ETag: "f-53f1fea1d7ff5"
Accept-Ranges: bytes
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 32791
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from 192.168.1.131
Connection: keep-alive
结果:不管测试几次,结果均为MISS
#不区分大小写,在url前面加(?i)
[root@node1 varnish]# vim test.vcl
在sub vcl_recv段中添加
if (req.url ~ "(?i)^/login" || req.url ~ "(?i)^/admin") {
return(pass);
}
[root@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test5 test.vcl
200
VCL compiled.
vcl.use test5
200
VCL ‘test5‘ now active
[root@node2 ~]# mkdir /var/www/html/admin
[root@node2 ~]# vim /var/www/html/admin/index.html
From Admin Page
[root@node3 ~]# curl -I http://192.168.1.131:6081/admin/index.html
HTTP/1.1 200 OK
Date: Wed, 19 Oct 2016 04:27:53 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Wed, 19 Oct 2016 04:27:22 GMT
ETag: "10-53f3039b4f7b4"
Accept-Ranges: bytes
Content-Length: 16
Content-Type: text/html; charset=UTF-8
X-Varnish: 32794
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from 192.168.1.131
Connection: keep-alive
结果:不论测试几次,结果均为MISS
对特定类型的资源取消其私有的cookie标识:
[root@node1 varnish]# vim test.vcl
在vcl_backend_response段中添加
if (beresp.http.cache-control !~ "s-maxage") {
if (bereq.url ~ "(?i)\.jpg$") {
set beresp.ttl = 3600s;
unset beresp.http.Set-Cookie;
}
if (bereq.url ~ "(?i)\.css$") {
set beresp.ttl = 600s;
unset beresp.http.Set-Cookie;
}
}
[root@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test6 test.vcl
200
VCL compiled.
vcl.use test6
200
VCL ‘test6‘ now active
[root@node2 ~]# cd /var/www/html/
上传两张图片1.jpg,2.jpg
示例:
[root@node1 ~]# cd /etc/varnish/
[root@node1 varnish]# vim test.vcl
修改backend段的内容为:
backend websrv1 {
.host = "192.168.1.132";
.port = "80";
.probe = {
.url = "test1.html";
}
}
backend websrv2 {
.host = "192.168.1.133";
.port = "80";
}
在vcl_recv段内添加
if (req.url ~ "(?i)^\.(jpg|png|gif)$") {
set req.backend_hint = websrv1;
} else {
set req.backend_hint = websrv2;
}
[root@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test7 test.vcl
200
VCL compiled.
vcl.use test7
200
VCL ‘test7‘ now active
backend.list
200
Backend name Refs Admin Probe
default(192.168.1.132,,80) 7 probe Healthy (no probe)
websrv1(192.168.1.132,,80) 2 probe Sick 0/8
websrv2(192.168.1.133,,80) 2 probe Healthy (no probe)
[root@node3 ~]# yum -y install httpd
[root@node3 ~]# for i in {1..10};do echo "<h1>Test Page $i Web2 </h1>" > /var/www/html/test$i.html;done
[root@node3 ~]# systemctl start httpd.service
示例2:
[root@node1 varnish]# vim test.vcl
添加
import directors;
sub vcl_init {
new mycluster = directors.round_robin();
mycluster.add_backend(websrv1);
mycluster.add_backend(websrv2);
}
在sub vcl_recv段内添加
if (req.url ~ "(?i)test1.html$") {
return(pass);
}
set req.backend_hint = mycluster.backend();
vcl.load test8 test.vcl
200
VCL compiled.
vcl.use test8
200
VCL ‘test8‘ now active
本文出自 “追梦” 博客,请务必保留此出处http://sihua.blog.51cto.com/377227/1863759
36 web系统架构及cache基础、varnish4基础应用、varnish状态引擎详解及vcl
原文地址:http://sihua.blog.51cto.com/377227/1863759