上一小节我们我们讲到关于varnish cache 编译安装,这里将围绕关于varnish讲解有关varnish的实例配置,即varnish主配置文件:default.vcl的配置
一.varnish的配置实例模板
一般我们将varnish的主配置文件default.vcl文件分为三大部分:后端webserver健康检查机制,后端webserver 主机配置区域,http请求url应用请求分发机制;
1.1 后端webserver健康检查机制模板样例配置
新建一个健康检查文件,并命名为health_check.vcl,并添加如下内容:
vim /usr/local/varnish/etc/health_check.vcl 内容如下: probe healthcheck { .url = "/"; #定义健康检查的页面 .interval = 6s; #探测请求的发送周期,默认为5秒; .timeout = 0.3 s; #每次探测请求的过期时长 .window = 8; #设定在判定后端主机健康状态时基于最近多少次的探测进行 .threshold = 3; #在.window中指定的次数中,至少有多少次是成功的才判定后端主机正健康运行 .initial = 3; #Varnish启动时对后端主机至少需要多少次的成功探测,默认同.threshold; }
1.2 后端webserver主机池区域模板样例配置
新建一个健康检查文件,并命名为backends.vcl.vcl,并添加如下内容:
vim /usr/local/varnish/etc/backends.vcl.vcl 内容如下: include "health_check.vcl"; #This is sale webserver configure backend web01 { #定义后端webserver的别名 .host = "10.0.11.145"; #定义后端webserver的IP地址 .port = "80"; #定义后端webserver的web服务侦听端口 .first_byte_timeout = 9s; #定义等待从backend传输过来的第一个字节的时间 .connect_timeout = 3s; #定义等待连接后端的时间 .between_bytes_timeout = 1s; #定义两个字节的间隔时间 .probe = healthcheck; #定义两个字节的间隔时间 } backend web02 { .host = "10.0.11.146"; .port = "80"; .first_byte_timeout = 9s; .connect_timeout = 3s; .between_bytes_timeout = 1s; .probe = backend_healthcheck; } import directors; #应用后端webserver负载机制 sub vcl_init { new web = directors.random(); #设置负载均衡池的名字(这里为:web,即new后面值)以及负载均衡算法(即“directors的值”.round-robin“ web.add_backend(web01); #在web负载池里添加后端web,即()里的值 web.add_backend(web02); } 注:varnish的调度算法有三种: 1.round-robin :加权轮询,因为每个backend都有它的权重; 2.random :随机调度; 3.dns :基于DNS名称解析之后进行调度
1.2 后端webserver主机池区域模板样例配
vim /usr/local/varnish/etc/health_check.vcl 内容如下: vcl 4.0; import std; include "backends.vcl"; acl allow_purge_cache { #定义了允许哪些主机通过HTTP来执行PURG进行缓存删除策略 "127.0.0.1"; "10.0.0.0"/8; "172.0.0.0"/8; } sub vcl_recv { if (req.method == "PURGE") { if (!client.ip ~ allow_purge_cache) { return (synth(405, "Not Allowed.")); } return (purge); } set req.backend_hint = web.backend(); #选择使用轮询的方式负载后端web-server if (req.url ~ "\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)") { return (pass); } #如果是请求为以上URL开头的请求(即动态请求)则直接转交到后端web处理,即进入到fetch(vcl_fetch)状态; if (req.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") { unset req.http.cookie; return (hash); } #如果是请求为以上类型的请求时,则会自动删除http头部的cookie信息,并且进入hash状态,会在hash表中查找数据,若找到,则进入 hit(vcl_hit)状态,否则进入 miss(vcl_miss)状态; ##首次访问增加X-Forwarded-For头信息,方便后端程序获取客户端IP信息 if (req.restarts == 0) { #如果设置过此header则要再次附加上,用,隔开,如果只有一层代理的话,就无需设置了 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.http.Cache-Control ~ "(?i)no-cache") { if (!(req.http.Via || req.http.User-Agent ~ "(?i)bot" || req.http.X-Purge)) { return (purge); } } if (req.method != "GET" && req.method != "HEAD" && req.method != "PUT" && req.method != "POST" && req.method != "TRACE" && req.method != "OPTIONS" && req.method != "PATCH" && req.method != "DELETE") { return (pipe); } #对于非正规的请求不进行缓存处理 if (req.method != "GET" && req.method != "HEAD") { return (pass); } #对于不是“get”和“head”类型的请求直接转交给后端处理 if (req.http.Authorization) { return (pass); } #对于http认证的页面也直接交给后端web处理 ################定以支持压缩功能############################ if (req.http.Accept-Encoding) { if (req.url ~ "\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$") { unset req.http.Accept-Encoding; } elseif (req.http.Accept-Encoding ~ "gzip") { set req.http.Accept-Encoding = "gzip"; } elseif (req.http.Accept-Encoding ~ "deflate") { set req.http.Accept-Encoding = "deflate"; } else { unset req.http.Accept-Encoding; } } if (req.http.Upgrade ~ "(?i)websocket") { return (pipe); } if (!std.healthy(req.backend_hint)) { unset req.http.Cookie; } if (req.http.x-pipe && req.restarts > 0) { unset req.http.x-pipe; return (pipe); } return (hash); } sub vcl_pipe { if (req.http.upgrade) { set bereq.http.upgrade = req.http.upgrade; } return (pipe); } ################定以vcl_pass函数段############################ sub vcl_pass { if (req.method == "PURGE") { return (synth(502, "PURGE on a passed object.")); } } ################定以vcl_hash函数段############################ sub vcl_hash { hash_data(req.url); if (req.http.host) { hash_data(req.http.host); } else { hash_data(server.ip); } if (req.http.Cookie) { hash_data(req.http.Cookie); } if (req.http.Accept-Encoding ~ "gzip") { hash_data("gzip"); } elseif (req.http.Accept-Encoding ~ "deflate") { hash_data("deflate"); } } ################定以vcl_hit函数段############################ sub vcl_hit { if (req.method == "PURGE") { return (synth(200, "Purged.")); } if (obj.ttl >= 0s) { return (deliver); } if (std.healthy(req.backend_hint)) { if (obj.ttl + 10s > 0s) { return (deliver); } else { return(fetch); } } else { if (obj.ttl + obj.grace > 0s) { return (deliver); } else { return (fetch); } } return (deliver); } ################定以vcl_miss函数段############################ sub vcl_miss { if (req.method == "PURGE") { return (synth(404, "Purged.")); } return (fetch); } sub vcl_backend_response { set beresp.grace = 5m; set beresp.ttl = std.duration(regsub(beresp.http.Cache-Control, ".*s-maxage=([0-9]+).*", "\1") + "s", 0s); if (beresp.ttl > 0s) { unset beresp.http.Set-Cookie; } if (beresp.http.Set-Cookie) { set beresp.uncacheable = true; return (deliver); } if (beresp.http.Cache-Control && beresp.ttl > 0s) { set beresp.grace = 1m; unset beresp.http.Set-Cookie; } if (beresp.http.Content-Length ~ "[0-9]{8,}") { set bereq.http.x-pipe = "1"; return (retry); } if (bereq.url ~ "\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)") { set beresp.uncacheable = true; return (deliver); } if (bereq.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") { unset beresp.http.set-cookie; } if (bereq.url ~ "^[^?]*\.(mp[34]|rar|tar|tgz|gz|wav|zip|bz2|xz|7z|avi|mov|ogm|mpe?g|mk[av])(\?.*)?$") { unset beresp.http.set-cookie; set beresp.do_stream = true; set beresp.do_gzip = false; } if ((!beresp.http.Cache-Control && !beresp.http.Expires) || beresp.http.Pragma ~ "no-cache" || beresp.http.Cache-Control ~ "(no-cache|no-store|private)") { set beresp.ttl = 120s; set beresp.uncacheable = true; return (deliver); } if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") { set beresp.ttl = 120s; set beresp.uncacheable = true; return (deliver); } if (bereq.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico)($|\?)") { set beresp.ttl = 15m; } elseif (bereq.url ~ "\.(gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") { set beresp.ttl = 30m; } else { set beresp.ttl = 10m; } return (deliver); } sub vcl_purge { if (req.method != "PURGE") { set req.http.X-Purge = "Yes"; return (restart); } } sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT from " + req.http.host; set resp.http.X-Cache-Hits = obj.hits; } else { set resp.http.X-Cache = "MISS from " + req.http.host; } unset resp.http.X-Powered-By; unset resp.http.Server; unset resp.http.Via; unset resp.http.X-Varnish; unset resp.http.Age; } sub vcl_backend_error { if (beresp.status == 500 || beresp.status == 501 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) { return (retry); } } sub vcl_fini { return (ok); }
以上即为varnish的三个相关配置模板示例,应用的时候,我们只需要根据应用需要,选取部分应用配置参考即可。
本文出自 “菜鸟的成长记” 博客,请务必保留此出处http://blief.blog.51cto.com/6170059/1732507
原文:http://blief.blog.51cto.com/6170059/1732507