主程序:varnish
配置文件:
·/etc/varnish/varnish.params—配置varnish服务进程的工作特性,例如监听的地址和端口,缓存机制;
·/etc/varnish/default.vcl—配置各Child/Cache线程的缓存工作属性;
VCL状态引擎:
内建变量:
·req.*:request,表示由客户端发来的请求报文相关;
·bereq.*:由varnish发往BE主机的httpd请求相关;
·beresp.*:由BE主机响应给varnish的响应报文相关;
·resp.*:由varnish响应给client相关;
·obj.*:存储在缓存空间中的缓存对象的属性;只读;
常用变量:
·bereq.*, req.*:
bereq.http.HEADERS
bereq.request:请求方法;
bereq.url:请求的url;
bereq.proto:请求的协议版本;
bereq.backend:指明要调用的后端主机;
req.http.Cookie:客户端的请求报文中Cookie首部的值;
req.http.User-Agent ~ “chrome”
·beresp.*, resp.*:
beresp.http.HEADERS
beresp.status:响应的状态码;
reresp.proto:协议版本;
beresp.backend.name:BE主机的主机名;
beresp.ttl:BE主机响应的内容的余下的可缓存时长;
·obj.*
obj.hits:此对象从缓存中命中的次数;
obj.ttl:对象的ttl值
·server.*
server.ip
server.hostname
·client.*
client.ip
基本配置目录:
- 交互式配置
- 对某种请求不检查缓存
- 拒绝某种请求访问
- 对公开资源,取消私有标记,并设定缓存时长
- 显示后端主机IP
- 根据purge请求清除缓存
- 设置acl访问控制
- 用ban命令清除缓存
- 配置varnish后端多台主机
- varnish动静分离
- 对后端主机进行健康状态检测
- varnish的性能优化
- varnish日志查看
1.交互式配置
varnishadm登录:-S /etc/varnish/secret -T 127.0.0.1:80配置文件相关:vcl.list :状态引擎列表;vcl.load:装载,加载并编译;vcl.use:激活;vcl.discard:删除;vcl.show [-v] <configname>:查看指定的配置文件的详细信息,可看默认配置;运行时参数:param.show -l:显示列表;param.show <PARAM>param.set <PARAM> <VALUE>缓存存储:storage.list后端服务器:backend.list
2.对某种请求不检查缓存
示例:vcl_recv {if (req.url ~ “(?i)^/(login|admin)”) {return(pass);}}
3.拒绝某种请求访问
示例:vcl_recv {if (req.http.User-Agent ~ “(?i)curl”) {return(synth(405));}}
4.对公开资源,取消私有标记,并设定缓存时长
示例:if (beresp.http.cache-control !~ “s-maxage”) {if (bereq.url ~ “(?i)\.(jpg|jpeg|png|gif|css|js)$”) {unset beresp.http.Set-Cookie;set beresp.ttl = 3600s;}}
5.显示后端主机IP
示例:if (req.restarts == 0) {if (req.http.X-Fowarded-For) {set req.http.X-Forwarded-For = req.http.X-Forwarded-For + “,” + client.ip;} else {set req.http.X-Forwarded-For = client.ip;}}
6.根据purge请求清除缓存
示例:sub vcl_recv {if (req.method == “PURGE”) {return(purge);}}
7.设置acl访问控制
示例:acl purgers {“127.0.0.0”/8;“10.1.0.0”/16;}sub vcl_recv {if (req.method == “PURGE”) {if (!client.ip ~ purgers) {return(synth(405,”Purging not allowed for ” + client.ip));}return(purge);}}
8.用ban命令清除缓存
示例:ban req.url ~ ^/javascriptsban req.url ~ /js$
9.配置varnish后端多台主机
示例:import directors; # 导入模块backend server1 {.host = “172.16.42.2”;.port = “80”;}backend server2 {.host = “172.16.42.3”;.port = “80”;
}
sub vcl_init {new websrvs = directors.round_robin();websrvs.add_backend(server1);websrvs.add_backend(server2);}sub vcl_recv {# 用哪一组server来请求set req.backend_hint = websrvs.backend();}
10.varnish动静分离
示例:backend default {.host = “172.16.42.10”;.port = “80”;}backend appsrv {.host = “172.16.42.2”;.port = “80”;}sub vcl_recv {if (req.url ~ “(?i)\.php$”) {set req.backend_hint = appsrv;} else {set req.backend_hint = default;}}
11.对后端主机进行健康状态检测
.probe:定义健康状态检测方法;.url:检测时请求的URL,默认为”/”;.request:发出的具体请求;.window:基于最近的多少次检查来判断其健康状态;.threshhold:最近.window中定义的这么次检查中至有.threshhold定义的次数是成功的;.interval:检测频度;.timeout:超时时长;.expected_response:期望的响应码,默认为200;示例:backend server1 {.host = “172.16.42.3”;.port = “80”;.probe = {.url= “/.healthcheck.html” #得先创建这个测试页面;.timeout= 1s;.interval= 2s;.window=5;.threshold=5;}}12.varnish的性能优化
·thread_pools: 最好小于或等于CPU核心数量;·thread_pool_max:每线程池的最大线程数;·thread_pool_min:额外意义为“最大空闲线程数”;·thread_pool_timeout:线程超时时间·thread_pool_add_delay:新创建线程·thread_pool_destroy_delay:杀死空闲线程延迟时间设置方式:/etc/varnish/varnish.params (永久有效)param.set示例:DAEMON_OPTS=”-p thread_pools=6 -p thread_pool_min=5 -p thread_pool_max=500 -p thread_pool_timeout=300″
13.varnish日志查看
1、varnishstat – Varnish Cache statistics-1-1 -f FILED_NAME-l:可用于-f选项指定的字段名称列表;MAIN.cache_hitMAIN.cache_miss示例:varnishstat -1 -f MAIN.cache_hit -f MAIN.cache_missvarnishstat -l -f MAIN -f MEMPOOL2、varnishtop – Varnish log entry ranking-1 Instead of a continously updated display, print the statistics once and exit.-i taglist,可以同时使用多个-i选项,也可以一个选项跟上多个标签;-I <[taglist:]regex>-x taglist:排除列表-X <[taglist:]regex>3、varnishlog – Display Varnish logs4、varnishncsa – Display Varnish logs in Apache / NCSA combined log format
原创文章,作者:z long,如若转载,请注明出处:http://www.178linux.com/84224