基于:varnish2.1.5
0 1 2 3 4 5 6 |
backend default { #定义一个后端(源)服务器,定义名称为default,可以定义多个后端 .host = "127.0.0.1"; #源服务器IP地址 .port = "80"; #源服务器端口 .connect_timeout = 4s; #定义等待连接后端的时间 .first_byte_timeout = 5s; #定义等待从backend传输过来的第一个字节的时间 .between_bytes_timeout = 20s; #定义两个字节的间隔时间 } |
0 1 2 3 4 5 |
sub vcl_recv { #支持动作包括:pass,nslookup(only),pipe(most) ##vcl_recl当用户请求[客户端]完成后,修改cookies或者添加删除请求头信息(request),决策那个后端服务器响应## if (req.url ~ "^/images") { #req请求的url包括images unset req.http.cookie; #unset不设置cookie } } |
0 1 2 3 4 5 6 |
sub vcl_fetch { #支持动作包括:pass,deliver ##vcl_fetch当用户请求在后端服务器完成后,修改响应头(response),若响应失败则返回ESI代码## if (req.url ~ "\.(png|gif|jpg)$") { #req求得的url中以.jpg unset beresp.http.set-cookie; #不设置cookies set beresp.ttl = 3600s; #beresp用于后端对象 } } |
default:基于varnish3.0.7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
/* 首次访问增加X-Forwarded-For头信息,方便后端程序获取客户端ip */ sub vcl_recv { if (req.restarts == 0) { #req.restarts表示重启次数,默认最大值为 4 if (req.http.x-forwarded-for) { #Varnish都会把客户端的IP地址放在HTTP_X_FORWARDED_FOR里面传给后端的Web服务器,所以后端的Web程序都要对其进行调用 set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip; } else { set req.http.X-Forwarded-For = client.ip; } } /* 该if判断表示如果请求的类型不是GET、HEAD、PUT、POST、TRACE、OPTIONS、DELETE时,则进入pipe模式。注意这里的"&&"是与的关系。 */ 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); } /* We only deal with GET and HEAD by default */ if (req.request != "GET" && req.request != "HEAD") { return (pass); } /* Not cacheable by default */ if (req.http.Authorization || req.http.Cookie) { #如果请求是认证或者cookies return (pass); #将请求直接传递至后端主机。后端主机在应答数据后将应答数据发送给客户端,但不进行任何缓存,在当前连接下每次都返回最新的内容 } return (lookup); #在缓存中查找被请求的对象,并且根据查找的结果把控制权交给函数 vcl_hit 或函数 vcl_miss } sub vcl_pipe { #此函数在进入 pipe 模式时被调用,用于将请求直接传递至后端主机,在请求和返回的内容没有改变的情况下,将不变的内容返回给客户端,直到这个连接被关闭。 # Note that only the first request to the backend will have # X-Forwarded-For set. If you use X-Forwarded-For and want to # have it set for all requests, make sure to have: # set bereq.http.connection = "close"; # here. It is not set by default as it might break some broken web # applications, like IIS with NTLM authentication. return (pipe); } sub vcl_pass { #此函数在进入 pass 模式时被调用,用于将请求直接传递至后端主机。后端主机在应答数据后将应答数据发送给客户端,但不进行任何缓存,在当前连接下每次都返回最新的内容。 return (pass); } sub vcl_hash { #当您想把一个数据添加到 hash 上时,调用此函数。 hash_data(req.url); #req.url指定请求的地址 if (req.http.host) { hash_data(req.http.host); } else { hash_data(server.ip); #server.ip表示服务器 IP } return (hash); } sub vcl_hit { #在执行 lookup 指令后,在缓存中找到请求的内容后将自动调用该函数。 return (deliver); #deliver:表示将找到的内容发送给客户端,并把控制权交给函数 vcl_deliver } sub vcl_miss { #在执行 lookup 指令后,在缓存中没有找到请求的内容时自动调用该方法。此函数可用于判断是否需要从后端服务器获取内容。 return (fetch); #fetch:表示从后端获取请求的内容,并把控制权交给 vcl_fetch 函数 } sub vcl_fetch { #在后端主机更新缓存并且获取内容后调用该方法,接着,通过判断获取的内容来决定是将内容放入缓存,还是直接返回给客户端 if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") { /* * Mark as "Hit-For-Pass" for the next 2 minutes */ set beresp.ttl = 120 s; return (hit_for_pass); #下次请求时不进行lookup,直接pass } return (deliver); #deliver:表示将找到的内容发送给客户端,并把控制权交给函数 vcl_deliver } sub vcl_deliver { #将在缓存中找到请求的内容发送给客户端前调用此方法 return (deliver); } sub vcl_error { #出现错误时调用此函数 set obj.http.Content-Type = "text/html; charset=utf-8"; set obj.http.Retry-After = "5"; synthetic {" <?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>"} + obj.status + " " + obj.response + {"</title> </head> <body> <h1>Error "} + obj.status + " " + obj.response + {"</h1> <p>"} + obj.response + {"</p> <h3>Guru Meditation:</h3> <p>XID: "} + req.xid + {"</p> <hr> <p>Varnish cache server</p> </body> </html> "}; return (deliver); } sub vcl_init { return (ok); } sub vcl_fini { return (ok); } |
wordpress::基于varnish3.0.7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
[root@Varnish varnish]# cat wordpress.vcl backend default { .host = "img.t4x.org"; .port = "80"; } acl purge { #配置允许清空某缓存的IP "10.0.1.100"; "10.0.1.101"; "10.0.1.102"; "10.0.1.103"; "10.0.1.104"; } # allow PURGE from localhost 10.0.1. sub vcl_recv { if (req.request == "PURGE") { if (!client.ip ~ purge) { error 405 "Not allowed."; } return (lookup); } # unset static cookies,Remove excess buffer if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") { unset req.http.cookie; set req.url = regsub(req.url, "\?.*$", ""); } # Remove excess buffer if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") { set req.url = regsub(req.url, "\?.*$", ""); } # 如果请求来源是用户登录、后台管理,来源的或者是未到日期发布的日志,直接请求后端服务器 if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") { return (pass); } # 如果请求的cookie match wordpress_或者wp-settings- 则直接去后端请求,否则则不设置http cookies if (req.http.cookie) { if (req.http.cookie ~ "(wordpress_|wp-settings-)") { return(pass); } else { unset req.http.cookie; } } } # 判断不是登录或者管理页面或者是登录页面但是请求是get,则不设置cookies,缓存1小时, sub vcl_fetch { if ( (!(req.url ~ "(wp-(login|admin)|login)")) || (req.request == "GET") ) { unset beresp.http.set-cookie; set beresp.ttl = 1h; } # 判断url是以jpg等结尾,设在缓存时间为365天 if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") { set beresp.ttl = 365d; } } sub vcl_deliver { #将在缓存中找到请求的内容发送给客户端前调用此方法 # multi-server webfarm? set a variable here so you can check # the headers to see which frontend served the request # 设置服务器名称为server-01,如果缓存对象大于0,则返回hit,否则返回miss set resp.http.X-Server = "server-01"; if (obj.hits > 0) { set resp.http.X-Cache = "HIT"; } else { set resp.http.X-Cache = "MISS"; } } # 在执行 lookup 指令后,在缓存中找到请求的内容后将自动调用该函数。 sub vcl_hit { if (req.request == "PURGE") { purge; error 200 "OK"; } } # 执行lookup后,Varnish在缓存中没有找到请求的内容时会自动调用该方法。此模块可以用于判断是否需要从后端服务器获取内容。 sub vcl_miss { if (req.request == "PURGE") { purge; error 404 "Not cached"; } } [root@Varnish varnish]# /usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/wordpress.vcl -u varnish |
vcl_recv:
0 1 2 |
vcl_recv (yes, we’re skimpy with characters, it’s Unix) is called at the beginning of a request, after the complete request has been received and parsed. Its purpose is to decide whether or not to serve the request, how to do it, and, if applicable, which backend to use. In vcl_recv you can also alter the request. Typically you can alter the cookies and add and remove request headers. Note that in vcl_recv only the request object, req is available. |
vcl_fetch:
0 1 |
vcl_fetch is called after a document has been successfully retrieved from the backend. Normal tasks her are to alter the response headers, trigger ESI processing, try alternate backend servers in case the request failed. In vcl_fetch you still have the request object, req, available. There is also a backend response, beresp. beresp will contain the HTTP headers from the backend. |
参考文档:
1:https://www.varnish-cache.org/docs/2.1/reference/vcl.html
2:https://www.varnish-cache.org/docs/3.0/reference/vcl.html
3:https://www.varnish-cache.org/docs/4.0/reference/vcl.html
4:https://www.varnish-cache.org/docs/3.0/installation/upgrade.html
5:https://www.varnish-cache.org/docs/4.0/whats-new/upgrading.html#changes-to-vcl
6:https://www.varnish-cache.org/docs/4.1/reference/index.htmlSourceByrd's Weblog-https://note.t4x.org/basic/varnish-configuration-language/ SourceByrd's Weblog-https://note.t4x.org/basic/varnish-configuration-language/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!