欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

varnish 配置文件分享(sens杨 注释)

程序员文章站 2023-02-18 22:03:18
前言: varnish 为目前新兴起来的软件,由于中文文档比较少,配置文件比较复杂,所以在运用起来也是特别的费劲。一个偶然的机会在一个群里,有位varnish高手( se...

前言:
varnish 为目前新兴起来的软件,由于中文文档比较少,配置文件比较复杂,所以在运用起来也是特别的费劲。一个偶然的机会在一个群里,有位varnish高手( sens杨 )发表了一篇他对varnish配置文件理解的文档。对于学者来说很有价值。所以转载了过来。

原文如下:

varnish配置文件原文地址:http://groups.drupal.org/node/63203

注:红色字体是英文的直接翻译,有些地方翻译的不好
绿色部分是一些思考,这个配置对自身的业务配置的很详细,现在对除了cookie和ttl那部分外其他可以理解80%,慢慢学习体会

  1. backend default {  
  2.   .host = "127.0.0.1";  
  3.   .port = "8008";  
  4.   .connect_timeout = 600s;  
  5.   .first_byte_timeout = 600s;  
  6.   .between_bytes_timeout = 600s;  
  7. }  
  8.   
  9. backend lighttpd {  
  10.   .host = "127.0.0.1";  
  11.   .port = "81";  
  12.   .connect_timeout = 600s;  
  13.   .first_byte_timeout = 600s;  
  14.   .between_bytes_timeout = 600s;  
  15. }  
  16.   
  17. acl techmission_internal {  
  18.   "localhost";  
  19.   "127.0.0.1";  
  20. }  
  21. sub vcl_recv {  
  22.   // allow a grace period for offering "stale" data in case backend lags (http://varnish-cache.org/wiki/vclexamplegrace)  
  23. // 如果backend数据滞后,允许为“过时”数据提供一个宽松期  
  24.   set req.grace = 5m;  
  25.   // block outside world from our test sites  
  26. // 阻止非自己说测试网站(的数据访问)  
  27.   if ((req.http.host ~ "www.domain1.org|www.domain2.org") && !(client.ip ~ techmission_internal) && !(req.url ~ "^/ad|^/files")) {  
  28.     error 403 "forbidden";  
  29.   }  
  30.   if((req.url ~ "/server-status" || req.url ~ "/whm-server-status") && !(client.ip ~ techmission_internal)) {  
  31.   error 404 "not found";  
  32.   }  
  33.   // add ping url to test varnish status  
  34. // 增加ping url测试varnish状态(这个功能使用大部分vcl都没配置,可以增加一个监控状态)  
  35.   if (req.request == "get" && req.url ~ "/varnish-ping") {  
  36.   error 200 "ok";  
  37.   }  
  38. /* normalize host header to reduce variation in cache */  
  39. // 使host头规范化,以减少在cache中变化(这个为什么会说变化呢?)  
  40. if (req.http.host == "domain.org" && req.url !~ "^/blogs") {  
  41.   set req.http.host = "www.domain.org";  
  42. }  
  43.   
  44. /* normalize accept-encoding to reduce effects of vary: accept-encoding   
  45.    (cf. http://varnish-cache.org/wiki/faq/compression)  
  46.    also note that vary: user-agent is considered harmful   
  47.    (cf. http://www.mail-archive.com/varnish-misc@projects.linpro.no/msg03296.html) */  
  48. //规范化accept-encoding以减少vary:accept-encoding影响(cf),也要注意vary: user-agent认为是有害的  
  49.   
  50.   if (req.http.accept-encoding) {  
  51. //if先判断是否可以存在,是为了缩小处理范围?  
  52. //看到的其他大部分配置直接就下面了,没有先判断accept-encoding是否存在,这点可以再考虑考虑  
  53. //现在有req.can_gzip参数了,判断客户端是否接受压缩代码传输  
  54.     if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {  
  55.       // don't compress already-compressed files  
  56.       remove req.http.accept-encoding;  
  57.     }   
  58.     elsif (req.http.accept-encoding ~ "gzip") {  
  59.         set req.http.accept-encoding = "gzip";  
  60.     }   
  61.     elsif (req.http.accept-encoding ~ "deflate") {  
  62.         set req.http.accept-encoding = "deflate";  
  63.     }   
  64.     else {  
  65.       // unknown algorithm  
  66. // 不了解运算  
  67.       remove req.http.accept-encoding;  
  68.     }  
  69.   }  
  70.   
  71.   // remove has_js and google analytics __* cookies. also remove collapsiblock cookies.  
  72. //删除has_js和谷歌统计__*的cookies,同时删除collapsiblock cookies  
  73.   set req.http.cookie = regsuball(req.http.cookie, "(^|;\s*)(__[a-z]+|__utma_a2a|has_js|collapsiblock)=[^;]*""");  
  74.   // remove jsessionid cookie from christianvolunteering.org static files and pages that are same for all users  
  75. //从christianvolunteering.org静态文件和网页中删除jsessionid cookie,所有的用户是一样(处理)的  
  76.     if (req.http.host ~ "christianvolunteering.org" &&  
  77.          (req.url ~ "^/$" ||  
  78.           req.url ~ "(searchform|advancedsearch|shorttermmissions|recruitvolunteers|volunteergettingstarted|virtualvolunteer|organizationsearch|abs-bible-outreach|ab*ecutive-volunteers|abs-traveling-engagement-center|churchinstructions|sitemap|city|virtual|organizationlistings|orglistings7407|technology|volunteerlistings|forgotpassword|churchvolunteer|churchvolunteering|servicetrip|region|citysitemap|searchformadv|personalitytest|groupvolunteering|disasterreliefvolunteering|disasterrelief|internships|christiangapyear|about|faqs|bookrecommendations|contact|pressrelease|training|volunteerstart|volunteerstories|articles)\.jsp$" ||  
  79.           req.url ~ "org/org[0-9]+\.jsp$" ||  
  80.           req.url ~ "org/opp[0-9]+\.jsp$" ||  
  81.           req.url ~ "orglistings[0-9]+\.jsp$" ||  
  82.           req.url ~ "org/[^/]+\.jsp$" ||  
  83.           req.url ~ "volunteer/[^/]+\.jsp$")  
  84.         ) {   
  85.     set req.http.cookie = regsuball(req.http.cookie, "(^|;\s*)(jsessionid)=[^;]*""");  
  86.   }  
  87.   // remove a ";" prefix, if present.  
  88. //如果有”;”前缀,则删除  
  89.   set req.http.cookie = regsub(req.http.cookie, "^;\s*""");  
  90.   // remove empty cookies.  
  91. // 删除空cookies  
  92.   if (req.http.cookie ~ "^\s*$") {  
  93.     unset req.http.cookie;  
  94.   }   
  95.   
  96.   // exclude umjobs and gospelpedia test sites  
  97. // 排除umjos和gospelpedia测试站点  
  98.   if (req.http.host ~ "domain1.org" || req.http.host ~ "domain2.org") {  
  99.     return (pass);  
  100.   }  
  101.   
  102.   // exclude the cron and supercron pages  
  103. // 排除cron和supercron网页  
  104.   if (req.url ~ "cron.php") {  
  105.     return (pass);  
  106.   }  
  107.   // exclude dynamic pages (as did boost)  
  108. // 排除动态网页(也是提高(处理))  
  109.   if (req.http.host ~ "domain.org" && req.url ~ "^/(user/login|user/password|user/register|logout|cart|post-blog|site-feedback|cgi-bin/webscr|redirect-home|cv-enroll|recommended-content|node/8755|node/8830|node/8351|node/8757|node/8831|cart/(.*)|uc_paypal/(.*)|civicrm/(.*)|admin/(.*)|recommended-content/(.*)|comment/reply/(.*)|node/add/(.*))" ) {  
  110.     return (pass);  
  111.   }  
  112.   
  113.   // exclude in case of referer theme  
  114. // 排除referer的一些主题  
  115.   if (req.http.host ~ "domain.org" && req.http.referer ~ "www.christianvolunteering.org|worldvision.christianvolunteering.org|ccda.christianvolunteering.org|www.ccda.org|www.urbanresource.net|mobile.urbanministry.org|www.ministeriourbano.com") {  
  116.     return (pass);  
  117.   }  
  118.   /* rules to fix moodle (thanks, gchaix!) */  
  119. // 修复moodle规则  
  120.    // cache moodle theme files  
  121. //缓存moodle主题文件  
  122.    if (req.url ~ "/pix/.*\.gif$") {  
  123.      return (lookup);  
  124.    }  
  125.   
  126.     // moodle doesn't like to be cached, passing  
  127. //moodle主题不能缓存的就pass  
  128.     if (req.http.cookie ~ "(moodlesession|moodlesessiontest)") {  
  129.       return (pass);  
  130.     }  
  131.     if (req.http.host == "www.domain.edu" && req.url ~ "^/courses") {  
  132.       return (pass);  
  133.     }  
  134.     if (req.url ~ "file.php") {  
  135.       return (pass);  
  136.     }  
  137.   
  138.     // wpmu themes are not playing well with static file caching  
  139. //wpmu主题使用静态文件缓存运行的不太好  
  140.     if (req.http.host == "domain.org" && req.url ~ "/blogs/(.*)/wp-content/themes") {  
  141.       return (pass);  
  142.     }  
  143.   /* rules for static file caching */  
  144.   /* 静态文件缓存规则  
  145.   // static files get served by lighttpd  
  146. // 使用lightted服务静态文件  
  147.   if (req.http.host != "server2.techmission.org" && req.url ~ "\.(gif|jpg|swf|css|js|png|jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|doc|ppt|pps|xls|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw)$") {  
  148.      // lighttpd does not require cookies  
  149.      unset req.http.cookie;  
  150.      unset req.http.authorization;  
  151.      set req.backend = lighttpd;  
  152.   }  
  153.   
  154.   // large media files get piped directly to lighttpd, to avoid overfilling cache  
  155. // 大媒体文件直接使用pipe方式到lightted,避免缓存溢出  
  156.   if (req.url ~ "\.(mp3|mp4|m4a|ogg|mov|avi|wmv)$" && req.url !~ "audio/download") {  
  157.     set req.backend = lighttpd;  
  158.     pipe;  
  159.   }  
  160.   // pipe large media files that come from drupal, also, but they can't go to lighty  
  161. // pipe从drupal过来的大媒体文件,同时不去lighty  
  162.   if (req.url ~ "audio/play" || req.url ~ "audio/download") {  
  163.     pipe;  
  164.   }  
  165.   
  166. }  
  167. sub vcl_hash {  
  168.   if (req.http.cookie) {  
  169.     set req.hash += req.http.cookie;  
  170.   }  
  171.   /* have a separate object cache for mobile site based on user-agent */  
  172. /* 基于user-agent的移动网站有一个单独的对象缓存  
  173.   if (req.http.host == "www.domain.org" && req.http.user-agent ~ "(iphone|ipod)") {  
  174.     set req.hash += "mobile";  
  175.   }  
  176. }  
  177. sub vcl_fetch {  
  178.   // grace to allow varnish to serve content if backend is lagged  
  179. // 如果backend滞后,允许varnish服务内容有一个缓冲期  
  180.   set obj.grace = 5m;  
  181.   
  182.   // add line showing what cookie is once stripped by regex in vcl_recv  
  183. //在vcl_recv中通过regex增加展示cookie一次被剥夺的行  
  184.   set obj.http.x-stripped-cookie = req.http.cookie;  
  185.   set obj.http.x-request-url = req.url;  
  186.   
  187.   /* removing set-cookie headers that prevent caching */  
  188.  //删除那些阻止缓存的set-cookie头  
  189.   // don't have cookies on static files (gchaix says may cause loss of session; i haven't observed that)  
  190.     if (req.url ~ "\.(gif|jpg|swf|css|js|png|jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|doc|ppt|pps|xls|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw)$") {  
  191.     remove obj.http.set-cookie;  
  192.   }  
  193.   
  194.   // don't set session cookie on christianvolunteering.org static files or pages that are same for all users  
  195. //对于(头)christianvolunteering.org的静态文件和网页,对所有用户不设置session cookie  
  196.     if (req.http.host ~ "christianvolunteering.org" &&  
  197.          (req.url ~ "^/$" ||  
  198.           req.url ~ "(searchform|advancedsearch|shorttermmissions|recruitvolunteers|volunteergettingstarted|virtualvolunteer|organizationsearch|abs-bible-outreach|ab*ecutive-volunteers|abs-traveling-engagement-center|churchinstructions|sitemap|city|virtual|organizationlistings|orglistings7407|technology|volunteerlistings|forgotpassword|churchvolunteer|churchvolunteering|servicetrip|region|citysitemap|searchformadv|personalitytest|groupvolunteering|disasterreliefvolunteering|disasterrelief|internships|christiangapyear|about|faqs|bookrecommendations|contact|pressrelease|training|volunteerstart|volunteerstories|articles)\.jsp$" ||  
  199.           req.url ~ "org/org[0-9]+\.jsp$" ||  
  200.           req.url ~ "org/opp[0-9]+\.jsp$" ||  
  201.           req.url ~ "orglistings[0-9]+\.jsp$" ||  
  202.   req.url ~ "org/[^/]+\.jsp$" ||  
  203.           req.url ~ "volunteer/[^/]+\.jsp$")  
  204.         ) {     
  205.     set obj.http.set-cookie = regsuball(req.http.cookie, "(^|;\s*)(jsessionid)=[^;]*""");  
  206.     // remove empty set-cookie.  
  207.     if (obj.http.set-cookie ~ "^\s*$") {  
  208.       unset obj.http.set-cookie;  
  209.     }  
  210.   }  
  211.   
  212. /* ttl extensions */  
  213. /* ttl 扩展 */  
  214. // if on www.urbanministry.org or static.urbanministry.org, extend ttl by default (pt. 1)  
  215. //对于www.urbanministry.org或static.urbanministry.org,默认情况下扩展ttl  
  216.   if (req.http.host == "www.domain.org" || req.http.host == "static.domain.org") {  
  217.     set obj.http.x-ttl-extend = "yes";  
  218.   }  
  219.   
  220.   // if on cityvision.edu, but not in moodle, or if on blazinggrace.org, but not in forums, or if on techmission.org, change obj.ttl  
  221. //如果主机头是cityvision.edu但是不在moodle中,或者主机头是blazinggrace.org,但不在forums中,或者主机头是techmission.org,更改obj.ttl  
  222.   if ((req.http.host ~ "domain.edu" && req.url !~ "/courses") || (req.http.host ~ "blazinggrace.org" && req.url !~ "/forums")  || (req.http.host ~ "techmission.org")) {  
  223.    set obj.ttl = 7d;  
  224.    set obj.http.x-extended-ttl = "7d";  
  225. }  
  226.   
  227. if (obj.status == 404) {   
  228.    set obj.ttl = 1s;   
  229. if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  230. }   
  231.   
  232.   /* debugging of why a page was not cacheable */  
  233. /* debug为什么页面没有缓存 */  
  234.   if (!obj.cacheable) {  
  235.     set obj.http.x-cacheable = "no: varnish says not cacheable " obj.http.x-cacheable;  
  236.     if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  237.   }  
  238.   
  239.   
  240.   // exclude umjobs and gospelpedia test sites  
  241. // 排除umjobs和gospelpedia测试站点  
  242.   if (req.http.host ~ "domain1.org" || req.http.host ~ "domain2.org") {  
  243.     set obj.http.x-cacheable = "no: test domain " obj.http.x-cacheable;  
  244.     if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  245.   }  
  246.   
  247.   if (obj.http.set-cookie) {  
  248.     set obj.http.x-cacheable = "no: set-cookie " obj.http.x-cacheable;  
  249.     if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  250.   }  
  251.   
  252.   if (req.http.cookie ~ "drupal_uid|sess") {  
  253.     set obj.http.x-cacheable = "no: got session " obj.http.x-cacheable;  
  254.     if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  255.   }  
  256.   
  257.   if (obj.http.cache-control ~ "private" || obj.http.cache-control ~ "no-cache") {  
  258.     set obj.http.x-cacheable = "no: cache-control set to not cache " obj.http.x-cacheable;  
  259.     if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  260.   }  
  261.   
  262.   if (req.url ~ "cron.php") {  
  263.     set obj.http.x-cacheable = "no: cron job " obj.http.x-cacheable;  
  264.     if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  265.   }  
  266.   
  267.   if (req.http.host ~ "domain.org" && req.url ~ "^/(user/login|user/password|user/register|logout|cart|post-blog|site-feedback|cgi-bin/webscr|redirect-home|cv-enroll|recommended-content|node/8755|node/8830|node/8351|node/8757|node/8831|cart/(.*)|uc_paypal/(.*)|civicrm/(.*)|admin/(.*)|recommended-content/(.*)|comment/reply/(.*)|node/add/(.*))" ) {  
  268.     set obj.http.x-cacheable = "no: drupal un-cacheable path " obj.http.x-cacheable;  
  269.     if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  270.   }  
  271.   
  272.   if (req.http.host ~ "domain.org" && req.http.referer ~ "www.christianvolunteering.org|worldvision.christianvolunteering.org|ccda.christianvolunteering.org|www.ccda.org|www.urbanresource.net|mobile.urbanministry.org|www.ministeriourbano.com") {  
  273.     set obj.http.x-cacheable = "no: referer theme " obj.http.x-cacheable;  
  274.     if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  275.   }  
  276.   
  277.   if (req.request == "post") {  
  278.     set obj.http.x-cacheable = "no: post request " obj.http.x-cacheable;  
  279.     if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  280.   }  
  281.   
  282.   if (req.http.authorization) {  
  283.     set obj.http.x-cacheable = "no: http authentication " obj.http.x-cacheable;  
  284.     if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; }  
  285.   }  
  286.   
  287.   // extend ttl for urbanministry.org objects (but not panels, views, or quicktabs); invalidation thru varnish.module + um_common.module  
  288. //为urbaministry.org对象扩展ttl(不是面板、视图,或者quicktabs);经过varnish.module + um_common.module失效  
  289.   if((req.http.host == "www.domain.org" || req.http.host == "static.domain.org") && obj.http.x-ttl-extend == "yes" && !obj.http.x-cache-type) {  
  290.     set obj.ttl = 7d;  
  291.     set obj.http.x-extended-ttl = "7d";  
  292.   }  
  293.   
  294. }  
  295. sub vcl_deliver {  
  296. return (deliver);  
  297.   // add cache hit data  
  298. // 增加缓存命中数据  
  299.   if (obj.hits > 0) {  
  300.     // if hit add hit count  
  301. // 如果命中,增加命中数  
  302.     set resp.http.x-cache = "hit";  
  303.     set resp.http.x-cache-hits = obj.hits;  
  304.     // set resp.http.x-cache-served-url = "served " obj.http.x-request-url; // http headers are apparently not accessible in vcl_deliver  
  305. //在vcl_deliver中http头明显不可访问  
  306.     // set resp.http.x-cache-ttl = obj.ttl; // string representation not implemented yet (currently on 2.0.5)  
  307.   }  
  308.   else {  
  309.     set resp.http.x-cache = "miss";  
  310.   }  
  311. }  
  312. /* custom error subroutine - to be a little friendlier to our users */  
  313. //指定error子程序,对用户有好些  
  314. sub vcl_error {  
  315. if(obj.status == 503) {  
  316.     set obj.http.content-type = "text/html; charset=utf-8";  
  317.     synthetic {"  
  318. <?xml version="1.0" encoding="utf-8"?>  
  319. <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en"  
  320. "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">  
  321. <html>  
  322.   <head>  
  323.     <title>"} obj.status " " obj.response {"</title>  
  324. </head>  
  325.   <body>  
  326.     <h1>error "} obj.status " " obj.response {"</h1>  
  327.     <p>"} obj.response {"</p>  
  328. <p>sorry we missed you!</p>  
  329. <p>we are currently upgrading our websites to serve you better. we should be up again soon.</p>  
  330. <p>if you still receive this message 30 minutes from now, please email webmaster@techmission.org.</p>  
  331.     <h3>guru meditation:</h3>  
  332.     <p>xid: "} req.xid {"</p>  
  333.     <hr>  
  334.     <address>  
  335.        served by <a href="http://www.varnish-cache.org/">varnish cache server</a>  
  336.     </address>  
  337.   </body>  
  338. </html>  
  339. "};  
  340.     return (deliver);  
  341. }  
  342. elsif(obj.status == 403) {  
  343.     set obj.http.content-type = "text/html; charset=utf-8";  
  344.     synthetic {"  
  345. <?xml version="1.0" encoding="utf-8"?>  
  346. <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en"  
  347. "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">  
  348. <html>  
  349.   <head>  
  350.     <title>"} obj.status " " obj.response {"</title>  
  351. </head>  
  352.   <body>  
  353.     <h1>error "} obj.status " " obj.response {"</h1>  
  354.     <p>"} obj.response {"</p>  
  355. <h2>techmission developer access only</h2>  
  356. <p>this page is only accessible to our staff. please visit our main websites www.techmission.org,www.urbanministry.org, and " title="www.christianvolunteering.org.  
  357. " style="color: rgb(2, 122, 198); font-weight: bold; text-decoration: none; ">www.christianvolunteering.org.</p>  
  358. <!-- (if you should have access to this page, edit the vcl file to grant yourself access (by adding your current ip to the acl) and then reload the vcl.) -->  
  359.     <h3>guru meditation:</h3>  
  360.     <p>xid: "} req.xid {"</p>  
  361.     <hr>  
  362.     <address>  
  363.        served by <a href="http://www.varnish-cache.org/">varnish cache server</a>  
  364.     </address>  
  365.   </body>  
  366. </html>  
  367. "};   
  368.     return (deliver);  
  369. }  
  370. else {  
  371.     set obj.http.content-type = "text/html; charset=utf-8";  
  372.     synthetic {"  
  373. <?xml version="1.0" encoding="utf-8"?>  
  374. <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en"  
  375. "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">  
  376. <html>  
  377.   <head>  
  378.     <title>"} obj.status " " obj.response {"</title>  
  379. </head>  
  380.   <body>  
  381.     <h1>error "} obj.status " " obj.response {"</h1>  
  382.     <p>"} obj.response {"</p>  
  383.     <h3>guru meditation:</h3>  
  384.     <p>xid: "} req.xid {"</p>  
  385.     <hr>  
  386.     <address>  
  387.        served by <a href="http://www.varnish-cache.org/">varnish cache server</a>  
  388.     </address>  
  389.   </body>  
  390. </html>  
  391. "};   
  392.     return (deliver);  
  393. }  

在这里感谢 sens杨 同学对配置文档的解释。

下面小编特整理的没有前面数字的文件方法大家使用

backend default { 
 .host = "127.0.0.1"; 
 .port = "8008"; 
 .connect_timeout = 600s; 
 .first_byte_timeout = 600s; 
 .between_bytes_timeout = 600s; 
} 
 
backend lighttpd { 
 .host = "127.0.0.1"; 
 .port = "81"; 
 .connect_timeout = 600s; 
 .first_byte_timeout = 600s; 
 .between_bytes_timeout = 600s; 
} 
 
acl techmission_internal { 
 "localhost"; 
 "127.0.0.1"; 
} 
sub vcl_recv { 
 // allow a grace period for offering "stale" data in case backend lags (http://varnish-cache.org/wiki/vclexamplegrace) 
// 如果backend数据滞后,允许为“过时”数据提供一个宽松期 
 set req.grace = 5m; 
 // block outside world from our test sites 
// 阻止非自己说测试网站(的数据访问) 
 if ((req.http.host ~ "www.domain1.org|www.domain2.org") && !(client.ip ~ techmission_internal) && !(req.url ~ "^/ad|^/files")) { 
  error 403 "forbidden"; 
 } 
 if((req.url ~ "/server-status" || req.url ~ "/whm-server-status") && !(client.ip ~ techmission_internal)) { 
 error 404 "not found"; 
 } 
 // add ping url to test varnish status 
// 增加ping url测试varnish状态(这个功能使用大部分vcl都没配置,可以增加一个监控状态) 
 if (req.request == "get" && req.url ~ "/varnish-ping") { 
 error 200 "ok"; 
 } 
/* normalize host header to reduce variation in cache */ 
// 使host头规范化,以减少在cache中变化(这个为什么会说变化呢?) 
if (req.http.host == "domain.org" && req.url !~ "^/blogs") { 
 set req.http.host = "www.domain.org"; 
} 
 
/* normalize accept-encoding to reduce effects of vary: accept-encoding  
  (cf. http://varnish-cache.org/wiki/faq/compression) 
  also note that vary: user-agent is considered harmful  
  (cf. http://www.mail-archive.com/varnish-misc@projects.linpro.no/msg03296.html) */ 
//规范化accept-encoding以减少vary:accept-encoding影响(cf),也要注意vary: user-agent认为是有害的 
 
 if (req.http.accept-encoding) { 
//if先判断是否可以存在,是为了缩小处理范围? 
//看到的其他大部分配置直接就下面了,没有先判断accept-encoding是否存在,这点可以再考虑考虑 
//现在有req.can_gzip参数了,判断客户端是否接受压缩代码传输 
  if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") { 
   // don't compress already-compressed files 
   remove req.http.accept-encoding; 
  }  
  elsif (req.http.accept-encoding ~ "gzip") { 
    set req.http.accept-encoding = "gzip"; 
  }  
  elsif (req.http.accept-encoding ~ "deflate") { 
    set req.http.accept-encoding = "deflate"; 
  }  
  else { 
   // unknown algorithm 
// 不了解运算 
   remove req.http.accept-encoding; 
  } 
 } 
 
 // remove has_js and google analytics __* cookies. also remove collapsiblock cookies. 
//删除has_js和谷歌统计__*的cookies,同时删除collapsiblock cookies 
 set req.http.cookie = regsuball(req.http.cookie, "(^|;\s*)(__[a-z]+|__utma_a2a|has_js|collapsiblock)=[^;]*", ""); 
 // remove jsessionid cookie from christianvolunteering.org static files and pages that are same for all users 
//从christianvolunteering.org静态文件和网页中删除jsessionid cookie,所有的用户是一样(处理)的 
  if (req.http.host ~ "christianvolunteering.org" && 
     (req.url ~ "^/$" || 
     req.url ~ "(searchform|advancedsearch|shorttermmissions|recruitvolunteers|volunteergettingstarted|virtualvolunteer|organizationsearch|abs-bible-outreach|ab*ecutive-volunteers|abs-traveling-engagement-center|churchinstructions|sitemap|city|virtual|organizationlistings|orglistings7407|technology|volunteerlistings|forgotpassword|churchvolunteer|churchvolunteering|servicetrip|region|citysitemap|searchformadv|personalitytest|groupvolunteering|disasterreliefvolunteering|disasterrelief|internships|christiangapyear|about|faqs|bookrecommendations|contact|pressrelease|training|volunteerstart|volunteerstories|articles)\.jsp$" || 
     req.url ~ "org/org[0-9]+\.jsp$" || 
     req.url ~ "org/opp[0-9]+\.jsp$" || 
     req.url ~ "orglistings[0-9]+\.jsp$" || 
     req.url ~ "org/[^/]+\.jsp$" || 
     req.url ~ "volunteer/[^/]+\.jsp$") 
    ) {  
  set req.http.cookie = regsuball(req.http.cookie, "(^|;\s*)(jsessionid)=[^;]*", ""); 
 } 
 // remove a ";" prefix, if present. 
//如果有”;”前缀,则删除 
 set req.http.cookie = regsub(req.http.cookie, "^;\s*", ""); 
 // remove empty cookies. 
// 删除空cookies 
 if (req.http.cookie ~ "^\s*$") { 
  unset req.http.cookie; 
 }  
 
 // exclude umjobs and gospelpedia test sites 
// 排除umjos和gospelpedia测试站点 
 if (req.http.host ~ "domain1.org" || req.http.host ~ "domain2.org") { 
  return (pass); 
 } 
 
 // exclude the cron and supercron pages 
// 排除cron和supercron网页 
 if (req.url ~ "cron.php") { 
  return (pass); 
 } 
 // exclude dynamic pages (as did boost) 
// 排除动态网页(也是提高(处理)) 
 if (req.http.host ~ "domain.org" && req.url ~ "^/(user/login|user/password|user/register|logout|cart|post-blog|site-feedback|cgi-bin/webscr|redirect-home|cv-enroll|recommended-content|node/8755|node/8830|node/8351|node/8757|node/8831|cart/(.*)|uc_paypal/(.*)|civicrm/(.*)|admin/(.*)|recommended-content/(.*)|comment/reply/(.*)|node/add/(.*))" ) { 
  return (pass); 
 } 
 
 // exclude in case of referer theme 
// 排除referer的一些主题 
 if (req.http.host ~ "domain.org" && req.http.referer ~ "www.christianvolunteering.org|worldvision.christianvolunteering.org|ccda.christianvolunteering.org|www.ccda.org|www.urbanresource.net|mobile.urbanministry.org|www.ministeriourbano.com") { 
  return (pass); 
 } 
 /* rules to fix moodle (thanks, gchaix!) */ 
// 修复moodle规则 
  // cache moodle theme files 
//缓存moodle主题文件 
  if (req.url ~ "/pix/.*\.gif$") { 
   return (lookup); 
  } 
 
  // moodle doesn't like to be cached, passing 
//moodle主题不能缓存的就pass 
  if (req.http.cookie ~ "(moodlesession|moodlesessiontest)") { 
   return (pass); 
  } 
  if (req.http.host == "www.domain.edu" && req.url ~ "^/courses") { 
   return (pass); 
  } 
  if (req.url ~ "file.php") { 
   return (pass); 
  } 
 
  // wpmu themes are not playing well with static file caching 
//wpmu主题使用静态文件缓存运行的不太好 
  if (req.http.host == "domain.org" && req.url ~ "/blogs/(.*)/wp-content/themes") { 
   return (pass); 
  } 
 /* rules for static file caching */ 
 /* 静态文件缓存规则 
 // static files get served by lighttpd 
// 使用lightted服务静态文件 
 if (req.http.host != "server2.techmission.org" && req.url ~ "\.(gif|jpg|swf|css|js|png|jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|doc|ppt|pps|xls|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw)$") { 
   // lighttpd does not require cookies 
   unset req.http.cookie; 
   unset req.http.authorization; 
   set req.backend = lighttpd; 
 } 
 
 // large media files get piped directly to lighttpd, to avoid overfilling cache 
// 大媒体文件直接使用pipe方式到lightted,避免缓存溢出 
 if (req.url ~ "\.(mp3|mp4|m4a|ogg|mov|avi|wmv)$" && req.url !~ "audio/download") { 
  set req.backend = lighttpd; 
  pipe; 
 } 
 // pipe large media files that come from drupal, also, but they can't go to lighty 
// pipe从drupal过来的大媒体文件,同时不去lighty 
 if (req.url ~ "audio/play" || req.url ~ "audio/download") { 
  pipe; 
 } 
 
} 
sub vcl_hash { 
 if (req.http.cookie) { 
  set req.hash += req.http.cookie; 
 } 
 /* have a separate object cache for mobile site based on user-agent */ 
/* 基于user-agent的移动网站有一个单独的对象缓存 
 if (req.http.host == "www.domain.org" && req.http.user-agent ~ "(iphone|ipod)") { 
  set req.hash += "mobile"; 
 } 
} 
sub vcl_fetch { 
 // grace to allow varnish to serve content if backend is lagged 
// 如果backend滞后,允许varnish服务内容有一个缓冲期 
 set obj.grace = 5m; 
 
 // add line showing what cookie is once stripped by regex in vcl_recv 
//在vcl_recv中通过regex增加展示cookie一次被剥夺的行 
 set obj.http.x-stripped-cookie = req.http.cookie; 
 set obj.http.x-request-url = req.url; 
 
 /* removing set-cookie headers that prevent caching */ 
 //删除那些阻止缓存的set-cookie头 
 // don't have cookies on static files (gchaix says may cause loss of session; i haven't observed that) 
  if (req.url ~ "\.(gif|jpg|swf|css|js|png|jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|doc|ppt|pps|xls|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw)$") { 
  remove obj.http.set-cookie; 
 } 
 
 // don't set session cookie on christianvolunteering.org static files or pages that are same for all users 
//对于(头)christianvolunteering.org的静态文件和网页,对所有用户不设置session cookie 
  if (req.http.host ~ "christianvolunteering.org" && 
     (req.url ~ "^/$" || 
     req.url ~ "(searchform|advancedsearch|shorttermmissions|recruitvolunteers|volunteergettingstarted|virtualvolunteer|organizationsearch|abs-bible-outreach|ab*ecutive-volunteers|abs-traveling-engagement-center|churchinstructions|sitemap|city|virtual|organizationlistings|orglistings7407|technology|volunteerlistings|forgotpassword|churchvolunteer|churchvolunteering|servicetrip|region|citysitemap|searchformadv|personalitytest|groupvolunteering|disasterreliefvolunteering|disasterrelief|internships|christiangapyear|about|faqs|bookrecommendations|contact|pressrelease|training|volunteerstart|volunteerstories|articles)\.jsp$" || 
     req.url ~ "org/org[0-9]+\.jsp$" || 
     req.url ~ "org/opp[0-9]+\.jsp$" || 
     req.url ~ "orglistings[0-9]+\.jsp$" || 
 req.url ~ "org/[^/]+\.jsp$" || 
     req.url ~ "volunteer/[^/]+\.jsp$") 
    ) {   
  set obj.http.set-cookie = regsuball(req.http.cookie, "(^|;\s*)(jsessionid)=[^;]*", ""); 
  // remove empty set-cookie. 
  if (obj.http.set-cookie ~ "^\s*$") { 
   unset obj.http.set-cookie; 
  } 
 } 
 
/* ttl extensions */ 
/* ttl 扩展 */ 
// if on www.urbanministry.org or static.urbanministry.org, extend ttl by default (pt. 1) 
//对于www.urbanministry.org或static.urbanministry.org,默认情况下扩展ttl 
 if (req.http.host == "www.domain.org" || req.http.host == "static.domain.org") { 
  set obj.http.x-ttl-extend = "yes"; 
 } 
 
 // if on cityvision.edu, but not in moodle, or if on blazinggrace.org, but not in forums, or if on techmission.org, change obj.ttl 
//如果主机头是cityvision.edu但是不在moodle中,或者主机头是blazinggrace.org,但不在forums中,或者主机头是techmission.org,更改obj.ttl 
 if ((req.http.host ~ "domain.edu" && req.url !~ "/courses") || (req.http.host ~ "blazinggrace.org" && req.url !~ "/forums") || (req.http.host ~ "techmission.org")) { 
  set obj.ttl = 7d; 
  set obj.http.x-extended-ttl = "7d"; 
} 
 
if (obj.status == 404) {  
  set obj.ttl = 1s;  
if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
}  
 
 /* debugging of why a page was not cacheable */ 
/* debug为什么页面没有缓存 */ 
 if (!obj.cacheable) { 
  set obj.http.x-cacheable = "no: varnish says not cacheable " obj.http.x-cacheable; 
  if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
 } 
 
 
 // exclude umjobs and gospelpedia test sites 
// 排除umjobs和gospelpedia测试站点 
 if (req.http.host ~ "domain1.org" || req.http.host ~ "domain2.org") { 
  set obj.http.x-cacheable = "no: test domain " obj.http.x-cacheable; 
  if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
 } 
 
 if (obj.http.set-cookie) { 
  set obj.http.x-cacheable = "no: set-cookie " obj.http.x-cacheable; 
  if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
 } 
 
 if (req.http.cookie ~ "drupal_uid|sess") { 
  set obj.http.x-cacheable = "no: got session " obj.http.x-cacheable; 
  if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
 } 
 
 if (obj.http.cache-control ~ "private" || obj.http.cache-control ~ "no-cache") { 
  set obj.http.x-cacheable = "no: cache-control set to not cache " obj.http.x-cacheable; 
  if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
 } 
 
 if (req.url ~ "cron.php") { 
  set obj.http.x-cacheable = "no: cron job " obj.http.x-cacheable; 
  if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
 } 
 
 if (req.http.host ~ "domain.org" && req.url ~ "^/(user/login|user/password|user/register|logout|cart|post-blog|site-feedback|cgi-bin/webscr|redirect-home|cv-enroll|recommended-content|node/8755|node/8830|node/8351|node/8757|node/8831|cart/(.*)|uc_paypal/(.*)|civicrm/(.*)|admin/(.*)|recommended-content/(.*)|comment/reply/(.*)|node/add/(.*))" ) { 
  set obj.http.x-cacheable = "no: drupal un-cacheable path " obj.http.x-cacheable; 
  if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
 } 
 
 if (req.http.host ~ "domain.org" && req.http.referer ~ "www.christianvolunteering.org|worldvision.christianvolunteering.org|ccda.christianvolunteering.org|www.ccda.org|www.urbanresource.net|mobile.urbanministry.org|www.ministeriourbano.com") { 
  set obj.http.x-cacheable = "no: referer theme " obj.http.x-cacheable; 
  if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
 } 
 
 if (req.request == "post") { 
  set obj.http.x-cacheable = "no: post request " obj.http.x-cacheable; 
  if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
 } 
 
 if (req.http.authorization) { 
  set obj.http.x-cacheable = "no: http authentication " obj.http.x-cacheable; 
  if (obj.http.x-ttl-extend) { unset obj.http.x-ttl-extend; } 
 } 
 
 // extend ttl for urbanministry.org objects (but not panels, views, or quicktabs); invalidation thru varnish.module + um_common.module 
//为urbaministry.org对象扩展ttl(不是面板、视图,或者quicktabs);经过varnish.module + um_common.module失效 
 if((req.http.host == "www.domain.org" || req.http.host == "static.domain.org") && obj.http.x-ttl-extend == "yes" && !obj.http.x-cache-type) { 
  set obj.ttl = 7d; 
  set obj.http.x-extended-ttl = "7d"; 
 } 
 
} 
sub vcl_deliver { 
# return (deliver); 
 // add cache hit data 
// 增加缓存命中数据 
 if (obj.hits > 0) { 
  // if hit add hit count 
// 如果命中,增加命中数 
  set resp.http.x-cache = "hit"; 
  set resp.http.x-cache-hits = obj.hits; 
  // set resp.http.x-cache-served-url = "served " obj.http.x-request-url; // http headers are apparently not accessible in vcl_deliver 
//在vcl_deliver中http头明显不可访问 
  // set resp.http.x-cache-ttl = obj.ttl; // string representation not implemented yet (currently on 2.0.5) 
 } 
 else { 
  set resp.http.x-cache = "miss"; 
 } 
} 
/* custom error subroutine - to be a little friendlier to our users */ 
//指定error子程序,对用户有好些 
sub vcl_error { 
if(obj.status == 503) { 
  set obj.http.content-type = "text/html; charset=utf-8"; 
  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> 
<p>sorry we missed you!</p> 
<p>we are currently upgrading our websites to serve you better. we should be up again soon.</p> 
<p>if you still receive this message 30 minutes from now, please email webmaster@techmission.org.</p> 
  <h3>guru meditation:</h3> 
  <p>xid: "} req.xid {"</p> 
  <hr> 
  <address> 
    served by <a href="http://www.varnish-cache.org/">varnish cache server</a> 
  </address> 
 </body> 
</html> 
"}; 
  return (deliver); 
} 
elsif(obj.status == 403) { 
  set obj.http.content-type = "text/html; charset=utf-8"; 
  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> 
<h2>techmission developer access only</h2> 
<p>this page is only accessible to our staff. please visit our main websites www.techmission.org,www.urbanministry.org, and " title="www.christianvolunteering.org. 
" style="color: rgb(2, 122, 198); font-weight: bold; text-decoration: none; ">www.christianvolunteering.org.</p> 
<!-- (if you should have access to this page, edit the vcl file to grant yourself access (by adding your current ip to the acl) and then reload the vcl.) --> 
  <h3>guru meditation:</h3> 
  <p>xid: "} req.xid {"</p> 
  <hr> 
  <address> 
    served by <a href="http://www.varnish-cache.org/">varnish cache server</a> 
  </address> 
 </body> 
</html> 
"};  
  return (deliver); 
} 
else { 
  set obj.http.content-type = "text/html; charset=utf-8"; 
  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> 
  <address> 
    served by <a href="http://www.varnish-cache.org/">varnish cache server</a> 
  </address> 
 </body> 
</html> 
"};  
  return (deliver); 
} 
}