varnish4基础应用
varnish4使用
1、安装varnish4
[root@master1 ~]# yum install -y varnish
1.1 内存回收模块
Name : jemalloc
Arch : x86_64
Version : 3.6.0
Release : 1.el7
Size : 317 k
Repo : installed
From repo : ali-epel
Summary : General-purpose scalable concurrent malloc implementation
URL : http://www.canonware.com/jemalloc/
License : BSD
Description : General-purpose scalable concurrent malloc(3) implementation.
: This distribution is the stand-alone "portable" implementation of
: jemalloc.
[root@master1 ~]#
1.2 查看服务启动文件
[root@master1 ~]# cat /usr/lib/systemd/system/varnish.service
2、修改服务器启动配置选项###
[root@master1 ~]# vim /etc/varnish/varnish.params
#VARNISH_STORAGE="malloc,256M"
VARNISH_STORAGE="malloc,256M" #修改缓存方式
3、配置另一台web服务器(节点2)
[root@master2 ~]# yum -y install httpd
配置测试页:
[root@master2 ~]# for i in {1..10}; do echo "Page $i on Web1" > /var/www/html/test$i.html;done
[root@master2 ~]# ls /var/www/html/
test10.html test2.html test4.html test6.html test8.html
test1.html test3.html test5.html test7.html test9.html
[root@master2 ~]#
启动服务:
[root@master2 ~]# systemctl start httpd
设置开机启动:
[root@master2 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@master2 ~]#
[root@master2 ~]# systemctl is-enabled httpd
enabled
[root@master2 ~]#
4、配置varnish的acl后端主机文件
# Default backend definition. Set this to point to your content server.
backend default {
.host = "10.201.106.132"; 跳转到web2
.port = "80";
}
开启varnish服务:
[root@master1 ~]# systemctl start varnish.service
varnish:6081,6082
[root@master1 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:6081 *:*
LISTEN 0 10 127.0.0.1:6082 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 :::6081 :::*
[root@master1 ~]#
访问测试:
http://10.201.106.131:6081/test9.html
正常
5、varnishadm
5.1 查看缓存命中结果
连进去:
[root@master1 ~]# 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.4 revision 386f712
Type 'help' for command list.
Type 'quit' to close CLI session.
ping
200
PONG 1484619827 1.0
status
200
Child in state running
vcl.list
200
active 0 boot
vcl.load test default.vcl
200
VCL compiled.
vcl.list
200
active 0 boot
available 0 test
vcl.use test
200
VCL 'test' now active
vcl.use t
varnish> vcl .list
101
Unknown request.
Type 'help' for more info.
vcl.list
200
active 0 boot
available 0 test
vcl.discard test
200
vcl.list
200
active 0 boot
5.2 查看当前参数值
Type 'help' for more info.
param.show
设置参数值:
param.set thread_pools 4
显示单个参数:
param.show thread_pools
200
thread_pools
Value is: 4 [pools]
Default is: 2
Minimum is: 1
查看进程上次挂掉原因
panic.show
300
Child has not panicked or panic has been cleared
显示正在使用的存储:
varnish> storage.list
200
Storage devices:
storage.Transient = malloc
storage.s0 = malloc
显示配置文件编译前的样子:
vcl.show boot
查看后端服务器列表:
varnish> backend.list
200
Backend name Refs Admin Probe
default(10.201.106.132,,80) 1 probe Healthy (no probe)
列出定义的ban规则,(缓存对象清除规则)
varnish> ban.list
200
Present bans:
1484618521.742461 0 C
6、显示varnish日志
[root@master1 ~]# varnishlog
7、另一种日志格式
[root@master1 ~]# varnishncsa
10.201.106.1 - - [17/Jan/2017:21:19:42 +0800] "GET http://10.201.106.131:6081/test1.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
8、查看状态和排序
[root@master1 ~]# varnishstat
[root@master1 ~]# varnishtop
配置vcl
1、备份vcl,配置备份的vcl
[root@master1 ~]# cd /etc/varnish/
[root@master1 varnish]# cp default.vcl test.vcl
[root@master1 varnish]# vim test.vcl
2、编辑配置文件
[root@master1 varnish]# vim test.vcl
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.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);
}
3、vcl装载新配置文件
连入vcl:
[root@master1 varnish]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
装载配置文件:
vcl.load test1 test.vcl
200
VCL compiled.
查看:
vcl.list
200
active 0 boot
available 0 test1
使用test1,使其生效:
vcl.use test1
200
VCL 'test1' now active
查看test1配置:
vcl.show test1
200
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "10.201.106.132";
.port = "80";
}
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.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);
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
}
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.
}
4、deliver测试
4.1 自定义命中响应内容
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";
} else {
set resp.http.X-Cache = "MISS";
}
}
4.2 重新载入激活配置文件
载入文件
vcl.load test2 test.vcl
200
VCL compiled.
查看配置文件列表
vcl.list
200
available 0 boot
active 0 test1
available 0 test2
激活使用配置文件
vcl.use test2
200
VCL 'test2' now active
4.3 测试
curl测试:
[root@node1 ~]# curl http://10.201.106.131:6081/test3.html
Page 3 on Web1
curl只响应首部:
[root@node1 ~]# curl -I http://10.201.106.131:6081/test3.html
HTTP/1.1 200 OK
Date: Tue, 17 Jan 2017 20:05:02 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 16 Jan 2017 21:28:25 GMT
ETag: "f-5463cdd1ee54f"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 131079 131075
Age: 38
Via: 1.1 varnish-v4
X-Cache: HIT #已命中
Connection: keep-alive
[root@node1 ~]#
4.4 第一次请求网页MISS,第二次就HIT了
[root@node1 ~]# curl -I http://10.201.106.131:6081/test4.html
HTTP/1.1 200 OK
Date: Tue, 17 Jan 2017 20:06:33 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 16 Jan 2017 21:28:25 GMT
ETag: "f-5463cdd1ee54f"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 23
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS ###
Connection: keep-alive
[root@node1 ~]# curl -I http://10.201.106.131:6081/test4.html
HTTP/1.1 200 OK
Date: Tue, 17 Jan 2017 20:06:33 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 16 Jan 2017 21:28:25 GMT
ETag: "f-5463cdd1ee54f"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 131081 24
Age: 6
Via: 1.1 varnish-v4
X-Cache: HIT ###
Connection: keep-alive
[root@node1 ~]#
5、添加server.ip
5.1 修改配置
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 from " + server.ip;
} else {
set resp.http.X-Cache = "MISS " + server.ip;
}
}
5.2 装载激活配置文件
vcl.load test3 test.vcl
200
VCL compiled.
vcl.use test3
200
VCL 'test3' now active
5.3 测试
root@node1 ~]# curl -I http://10.201.106.131:6081/test1.html
HTTP/1.1 200 OK
Date: Tue, 17 Jan 2017 20:41:33 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 16 Jan 2017 21:28:25 GMT
ETag: "f-5463cdd1ee54f"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 26
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS 10.201.106.131 ###
Connection: keep-alive
[root@node1 ~]# curl -I http://10.201.106.131:6081/test1.html
HTTP/1.1 200 OK
Date: Tue, 17 Jan 2017 20:41:33 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 16 Jan 2017 21:28:25 GMT
ETag: "f-5463cdd1ee54f"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 163844 27
Age: 24
Via: 1.1 varnish-v4
X-Cache: HIT from 10.201.106.131 ###
Connection: keep-alive
[root@node1 ~]#