varnish工作原理详细讲解

Varish Configuration Language 简称VCL,通过它我们可以完成一些复杂的逻辑处理。下面将详细介绍:

 

Backend declarations:

 

Java代码   收藏代码
  1. backend www {  
  2.   .host = "www.example.com";  
  3.   .port = "80";  
  4.   .connect_timeout = 1s;  
  5.   .first_byte_timeout = 5s;  
  6.   .between_bytes_timeout = 2s;  
  7. }  

 

或者

 

Java代码   收藏代码
  1. backend www {  
  2.   .host = "www.example.com";  
  3.   .port = "80";  
  4. }  

 

 .host和.port为必填项

Directors:

varnish支持负载均衡,其中有random,client.hash,round-robin,dns,fallback等算法

 

Java代码   收藏代码
  1. director b2 random {//设置调度算法为random  
  2.   .retries = 5;  
  3.   {  
  4.     // 这里可以引用之前定义的backend  
  5.     .backend = b1;  
  6.     .weight  = 7;  
  7.   }  
  8.   {  
  9.     // 也可以直接定义backend  
  10.     .backend  = {  
  11.       .host = "www.example.com";  
  12.       .port="80";  
  13.     }  
  14.    .weight  = 3;  
  15.   }  
  16. }  

 

 

Java代码   收藏代码
  1. director b2 round-robin {//设置调度算法为round-robin  
  2.   {  
  3.     .backend = b1;//引用之前定义的backend  
  4.   }  
  5.   {  
  6.     .backend = b2;//引用之前定义的backend  
  7.   }  
  8. }  

 

其他调度算法的设置都大同小异具体可参考官方文档

ACLs

访问控制列表的定义

 

Java代码   收藏代码
  1. acl local {  
  2.   "localhost";           
  3.   "192.0.2.0"/24;       
  4.   ! "192.0.2.23";       
  5. }  

 

 在vcl子程序中使用(子程序将在下面讲到)

 

Java代码   收藏代码
  1. if (client.ip ~ local) {//来源ip符合上面定义则直接访问后端服务器,不缓存  
  2.   return (pass);  
  3. }  

 

Regular Expressions:

vcl中还支持正则表达式

 

Java代码   收藏代码
  1. if (req.http.host ~ "(?i)example.com$") {  
  2.         //一些逻辑  
  3. }  
 

 

Functions

hash_data(str) //主要用来生成缓存对象的hash key

 

regsub(str, regex, sub) //替换字符换str中匹配上正则表达式的内容为sub字符串,只替换一次

 

regsuball(str, regex, sub) //同上,不过这个方法是替换所有匹配的

 

ban(ban expression) //使符合表达式的对象不被缓存

 

ban_url(regex) //使符合的url的对象不被缓存

 

Subroutines

子程序这个说白了就是方法。

我们可以把一个请求分为多个阶段,每个阶段都会调用不同的方法,这样我们只要写出相应阶段的方法,我们的方法就会在相应的阶段被执行,想想这有多么美妙,我们可以干很多事情,我们对请求完全可控!


说白就是方法名称别人定义好了返回类型别人也定义好了你只需要填写你的逻辑就OK了!

下面介绍各个阶段的方法

vcl_init:

当vcl被load的时候被调用,一般情况下你基本不需要更改这里,当然你有这个需求那就另说


vcl_recv:

当请求到达的时候被调用,我们大部分的逻辑都在这里,访问控制,是否需要缓存等


vcl_pipe:

管道模式下被调用


vcl_pass:

请求直接到后端时被调用


vcl_hash:

缓存对象生成hash key时被调用


vcl_hit:

缓存命中时调用


vcl_miss:

缓存未命中时调用


vcl_fetch:

后端请求成功返回时调用


vcl_deliver:

缓存对象被传递到客户端前被调用


vcl_error:

当其他方法但会error时被调用


vcl_fini:

请求结束时调用

 

从上面几个方法大家可以看出一个请求需要经过好几个阶段,下面的图清晰的说明一个请求在不同的条件下所要经过的阶段,每个阶段都有不同的内置变量可以使用,所有精华都在这张图中!

 


 

下面贴几个官方的例子

 

Java代码   收藏代码
  1. backend default {  
  2.  .host = "www.example.com";  
  3.  .port = "80";  
  4. }  
  5.   
  6. sub vcl_recv {  
  7.     if (req.restarts == 0) {  
  8.         if (req.http.x-forwarded-for) {  
  9.             set req.http.X-Forwarded-For =  
  10.                 req.http.X-Forwarded-For + ", " + client.ip;  
  11.         } else {  
  12.             set req.http.X-Forwarded-For = client.ip;  
  13.         }  
  14.     }  
  15.     if (req.request != "GET" &&  
  16.       req.request != "HEAD" &&  
  17.       req.request != "PUT" &&  
  18.       req.request != "POST" &&  
  19.       req.request != "TRACE" &&  
  20.       req.request != "OPTIONS" &&  
  21.       req.request != "DELETE") {  
  22.         /* Non-RFC2616 or CONNECT which is weird. */  
  23.         return (pipe);  
  24.     }  
  25.     if (req.request != "GET" && req.request != "HEAD") {  
  26.         /* We only deal with GET and HEAD by default */  
  27.         return (pass);  
  28.     }  
  29.     if (req.http.Authorization || req.http.Cookie) {  
  30.         /* Not cacheable by default */  
  31.         return (pass);  
  32.     }  
  33.     return (lookup);  
  34. }  
  35.   
  36. sub vcl_pipe {  
  37.     # Note that only the first request to the backend will have  
  38.     # X-Forwarded-For set.  If you use X-Forwarded-For and want to  
  39.     # have it set for all requests, make sure to have:  
  40.     # set bereq.http.connection = "close";  
  41.     # here.  It is not set by default as it might break some broken web  
  42.     # applications, like IIS with NTLM authentication.  
  43.     return (pipe);  
  44. }  
  45.   
  46. sub vcl_pass {  
  47.     return (pass);  
  48. }  
  49.   
  50. sub vcl_hash {  
  51.     hash_data(req.url);  
  52.     if (req.http.host) {  
  53.         hash_data(req.http.host);  
  54.     } else {  
  55.         hash_data(server.ip);  
  56.     }  
  57.     return (hash);  
  58. }  
  59.   
  60. sub vcl_hit {  
  61.     return (deliver);  
  62. }  
  63.   
  64. sub vcl_miss {  
  65.     return (fetch);  
  66. }  
  67.   
  68. sub vcl_fetch {  
  69.     if (beresp.ttl <= 0s ||  
  70.         beresp.http.Set-Cookie ||  
  71.         beresp.http.Vary == "*") {  
  72.                 /* 
  73.                  * Mark as "Hit-For-Pass" for the next 2 minutes 
  74.                  */  
  75.                 set beresp.ttl = 120 s;  
  76.                 return (hit_for_pass);  
  77.     }  
  78.     return (deliver);  
  79. }  
  80.   
  81. sub vcl_deliver {  
  82.     return (deliver);  
  83. }  
  84.   
  85. sub vcl_error {  
  86.     set obj.http.Content-Type = "text/html; charset=utf-8";  
  87.     set obj.http.Retry-After = "5";  
  88.     synthetic {"  
  89. <?xml version="1.0" encoding="utf-8"?>  
  90. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
  91.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
  92. <html>  
  93.   <head>  
  94.     <title>"} + obj.status + " " + obj.response + {"</title>  
  95.   </head>  
  96.   <body>  
  97.     <h1>Error "} + obj.status + " " + obj.response + {"</h1>  
  98.     <p>"} + obj.response + {"</p>  
  99.     <h3>Guru Meditation:</h3>  
  100.     <p>XID: "} + req.xid + {"</p>  
  101.     <hr>  
  102.     <p>Varnish cache server</p>  
  103.   </body>  
  104. </html>  
  105. "};  
  106.     return (deliver);  
  107. }  
  108.   
  109. sub vcl_init {  
  110.         return (ok);  
  111. }  
  112.   
  113. sub vcl_fini {  
  114.         return (ok);  
  115. }  

 

 

Java代码   收藏代码
  1. backend www {  
  2.   .host = "www.example.com";  
  3.   .port = "80";  
  4. }  
  5.   
  6. backend images {  
  7.   .host = "images.example.com";  
  8.   .port = "80";  
  9. }  
  10.   
  11. sub vcl_recv {  
  12.   if (req.http.host ~ "(?i)^(www.)?example.com$") {  
  13.     set req.http.host = "www.example.com";  
  14.     set req.backend = www;  
  15.   } elsif (req.http.host ~ "(?i)^images.example.com$") {  
  16.     set req.backend = images;  
  17.   } else {  
  18.     error 404 "Unknown virtual host";  
  19.   }  
  20. }  
  21.   
  22. The following snippet demonstrates how to force a minimum TTL for  
  23. all documents.  Note that this is not the same as setting the  
  24. default_ttl run-time parameter, as that only affects document for  
  25. which the backend did not specify a TTL:::  
  26.   
  27. import std; # needed for std.log  
  28.   
  29. sub vcl_fetch {  
  30.   if (beresp.ttl < 120s) {  
  31.     std.log("Adjusting TTL");  
  32.     set beresp.ttl = 120s;  
  33.   }  
  34. }  

 

 

Java代码   收藏代码
  1. acl purge {  
  2.   "localhost";  
  3.   "192.0.2.1"/24;  
  4. }  
  5.   
  6. sub vcl_recv {  
  7.   if (req.request == "PURGE") {  
  8.     if (!client.ip ~ purge) {  
  9.       error 405 "Not allowed.";  
  10.     }  
  11.     return(lookup);  
  12.   }  
  13. }  
  14.   
  15. sub vcl_hit {  
  16.   if (req.request == "PURGE") {  
  17.     purge;  
  18.     error 200 "Purged.";  
  19.   }  
  20. }  
  21.   
  22. sub vcl_miss {  
  23.   if (req.request == "PURGE") {  
  24.     purge;  
  25.     error 200 "Purged.";  
  26.   }  
  27. }  

 

 

官方参考文档:https://www.varnish-cache.org/docs/3.0/reference/vcl.html

 

  • 大小: 59 KB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值