一、Varnish的安装与配置
1、Varnish安装(rpm包软件)
rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-3.0.el5.rpm
yum install varnish varnish-libs-devel -y
如有安装疑惑请访问varnish官网https://www.varnish-cache.org/installation/redha
2、Varnish目录结构
/etc/sysconfig/varnish #存放Varnish运行参数设置
/etc/varnish/ #存放Varnish VCL配置文件
3、Varnish配置基本实例
3.1、修改 /etc/sysconfig/varnish
vim /etc/sysconfig/varnish
#VARNISH_VCL_CONF=/etc/varnish/default.vcl
VARNISH_VCL_CONF=/etc/varnish/test.vcl #调用test.vcl文件
VARNISH_LISTEN_PORT=80 #改为80端口
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1 #监听地址
VARNISH_ADMIN_LISTEN_PORT=6082 #管理端口
VARNISH_SECRET_FILE=/etc/varnish/secret #密钥文件
3.2、提供/etc/varnish/test.vcl
cd /etc/varnish/
cp default.vcl test.vcl #复制默认配置文件
3.3、配置/etc/varnish/test.vcl
############定义健康状态检测###############
probe healthcheck {
.url = "/"; #定义健康检查的页面
.interval = 6s; #探测请求的发送周期,默认为5秒;
.timeout = 0.3 s; #每次探测请求的过期时长
.window = 8; #设定在判定后端主机健康状态时基于最近多少次的探测进行
.threshold = 3; #在.window中指定的次数中,至少有多少次是成功的才判定后端主机正健康运行
.initial = 3; #Varnish启动时对后端主机至少需要多少次的成功探测,默认同.threshold;
}
############定义两组服务器##############
backend web1 {
.host = "172.16.10.7"; #服务器主机
.port = "80"; #服务器端口
.probe = healthcheck; #健康状态检测
}
backend web2 {
.host = "172.16.10.3";
.port = "80";
.probe = healthcheck;
}
backend app1 {
.host = "172.16.10.7";
.port = "8080";
.probe = healthcheck;
}
backend app2 {
.host = "172.16.10.3";
.port = "8080";
.probe = healthcheck;
}
############定义集群,调用服务器##############
director webserver random { #定义一个名为webserver的directory,由web1,和web2分但请求,使用random算法,处理静态请求
{.backend = web1;.weight = 2;} #设置权重为2
{.backend = web2;.weight = 5;}
}
director appserver random { #定义一个名为appserver的directory,由app1,和app2分但请求,使用random算法,处理动态请求
{.backend = app1;.weight = 2;}
{.backend = app2;.weight = 5;}
}
############定义Acl##############
acl purgers { #设置清理缓存的IP
"127.0.0.1";
"172.16.10.0"/16;
}
############vcl_recv函数段##############
sub vcl_recv {
#使得后端服务能记录访问者的真实IP
if (req.http.x-forwarded-for) { #添加首部信息
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
#配置动静分离
if (req.url ~ "\.php$"){
set req.backend = appserver; #php结尾的交给appserver服务器组处理,否则交给webserver服务器组处理
}else{
set req.backend = webserver;
}
#不正常的访问不缓存
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
#不缓存认证信息和Cookie
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
#定义清楚缓存IP,调用上面的Acl
if (req.request == "PURGE"){ #使用PURGE命令清除缓存
if(!client.ip ~ purgers){ #非ACl定义的IP,则不能清除缓存
error 405 "Method not allowed";
}
return (lookup);
}
#支持压缩功能
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
# No point in compressing these
remove req.http.Accept-Encoding;
} else if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} else if (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
remove req.http.Accept-Encoding;
}
}
}
############vcl_hit函数段##############
sub vcl_hit {
if (req.request == "PURGE"){ #请求方法是PURGE,这清理缓存
purge;
error 200 "Purged";
}
}
############vcl_miss函数段##############
sub vcl_miss {
if (req.request == "PURGE"){
purge;
error 404 "Not in cache";
}
}
############vcl_pass函数段##############
sub vcl_pass {
if (req.request == "PURGE"){
error 502 "PURGE on a passed object";
}
}
############vcl_fetch函数段##############
sub vcl_fetch {
#定义缓存时长
if (req.request == "GET" && req.url ~ "\.html$") {
set beresp.ttl = 300s; #超时时长为300秒
if (req.request == "GET" && req.url ~ "\.(png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|swf|txt|png|gif|jpg|css|js|html|htm)$") {
set beresp.ttl = 600s;
}
return (deliver);
}
}
############vcl_deliver函数段##############
sub vcl_deliver {
#定义Header标识,判断缓存是否命中
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT"; #命中则返回HIT
} else {
set resp.http.X-Cache = "MISS"; #未命中则返回MISS
}
}
3、4启动Varnish服务
/etc/init.d/varnish start
4、访问测试(查看是否命中)
222132184.png
缓存命中率的高低,直接反映Varnish的运行状态,以下通过varnishstat命令查看状态信息
[root@nginx2 varnish]# varnishstat
itrate ratio: 1 1 1
Hitrate avg: 0.6771 0.6771 0.6771
368 0.00 0.01 client_conn - Client connections accepted
824 0.00 0.02 client_req - Client requests received
369 0.00 0.01 cache_hit - Cache hits
176 0.00 0.01 cache_miss - Cache misses
95 0.00 0.00 backend_conn - Backend conn. success
83 0.00 0.00 backend_unhealthy - Backend conn. not attempted
33 0.00 0.00 backend_fail - Backend conn. failures
260 0.00 0.01 backend_reuse - Backend conn. reuses
51 0.00 0.00 backend_toolate - Backend conn. was closed
315 0.00 0.01 backend_recycle - Backend conn. recycles
222 0.00 0.01 fetch_length - Fetch with Length
20 0.00 0.00 fetch_chunked - Fetch chunked
96 0.00 0.00 fetch_304 - Fetch no body (304)
16 . . n_sess_mem - N struct sess_mem
3 . . n_objectcore - N struct objectcore
3 . . n_objecthead - N struct objecthead
4 . . n_waitinglist - N struct waitinglist
4 . . n_vbc - N struct vbc
100 . . n_wrk - N worker threads
100 0.00 0.00 n_wrk_create - N worker threads created
7 . . n_backend - N backends
86 . . n_expired - N expired objects
130 . . n_lru_moved - N LRU moved objects
453 0.00 0.01 n_objwrite - Objects sent with write
368 0.00 0.01 s_sess - Total Sessions
824 0.00 0.02 s_req - Total Requests
17 0.00 0.00 s_pipe - Total pipe
252 0.00 0.01 s_pass - Total pass
338 0.00 0.01 s_fetch - Total fetch
221522 0.00 6.37 s_hdrbytes - Total header bytes
注释:
Client connections accepted:表示客户端向反向代理服务器成功发送HTTP请求的总数量
Client requests received: 表示到现在为止,浏览器向反向代理服务器发送HTTP请求的累积次数,由于可能会使用长连接,所以这个值一般会大 于“Client connections accepted”。
Cache hits:表示反向代理服务器在缓存区中查找并且命中缓存的次数。
Cache misses:表示直接访问后端主机的请求数量,也就是非命中数。
N struct object:表示当前被缓存的数量。
N expired objects:表示过期的缓存内容数量。
N LRU moved objects:表示被淘汰的缓存内容个数
二、Varnish 管理
1、手动清除缓存
[root@nginx2 varnish]# curl -X PURGE http://172.16.10.2/index.html
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>200 Purged</title>
</head>
<body>
<h1>Error 200 Purged</h1>
<p>Purged</p>
<h3>Guru Meditation:</h3>
<p>XID: 1003529630</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
2、查看varnish日志
[root@nginx2 varnish]# varnishlog
0 Backend_health - app Still healthy 4--X-RH 8 3 8 0.011716 0.014211 HTTP/1.1 200 OK
0 Backend_health - app1 Still healthy 4--X-RH 8 3 8 0.016078 0.014856 HTTP/1.1 200 OK
3、 varnishadm 命令
[root@nginx2 varnish]# varnishadm
varnish> help
200
help [command]
ping [timestamp]
auth response
quit
banner
status #查看状态
start #启动varnish
stop #关闭varnish
vcl.load <configname> <filename> #动态加载vcl
vcl.inline <configname> <quoted_VCLstring>
vcl.use <configname> #动态使用vcl
vcl.discard <configname>
vcl.list #查看使用的vcl
vcl.show <configname> #查看vcl的配置
.........................................................