Varnish第二版配置

之前有写过一个Varnish,用起来感觉怪怪的。准确地说是有人反应会出现时快时慢的感觉。这几天参考官方文档重新配置过了Varnish。现在的Varnish主要是跟Apache配合来使用。暂时还没有以独立的服务器来跑。因为个人的BLog没有什么流量。呵呵。。

FreeBSD安装软件就是简单

  1. portmaster /usr/ports/www/varnish

  1. vi /etc/sysctl.conf
  2. kern.ipc.somaxconn=16384
  3. kern.maxfiles=131072
  4. kern.maxfilesperproc=104856
  5. kern.threads.max_threads_per_proc=4096
  6.  
  7. net.inet.tcp.blackhole=2
  8. net.inet.udp.blackhole=1
  9.  
  10. net.inet.icmp.icmplim_output=0
  1. vi /etc/rc.conf
  2. varnishd_enable="YES"
  3. varnishd_listen=":80"

将Apache监听的端口改为8080

  1. vi /usr/local/etc/apache22/http.conf
  2. #大概在第40行左右。使用:40可以直接到
  3. Listen 8080
  1. vi /usr/local/etc/varnish/default.vcl
  2. #-e This is a basic VCL configuration file for varnish.  See the vcl(7)
  3. #man page for details on VCL syntax and semantics.
  4. #
  5. #Default backend definition.  Set this to point to your content
  6. #server.
  7. #
  8. backend default {
  9. .host = "127.0.0.1";
  10. .port = "8080";
  11. }
  12. #
  13. #Below is a commented-out copy of the default VCL logic.  If you
  14. #redefine any of these subroutines, the built-in logic will be
  15. #appended to your code.
  16. #
  17. sub vcl_recv {
  18. #       remove req.http.X-Forwarded-For;
  19. #       set     req.http.X-Forwarded-For = client.ip;
  20.         set req.grace = 30s;
  21.     if (req.request != "GET" &&
  22.       req.request != "HEAD" &&
  23.       req.request != "PUT" &&
  24.       req.request != "POST" &&
  25.       req.request != "TRACE" &&
  26.       req.request != "OPTIONS" &&
  27.       req.request != "DELETE") {
  28.         /* Non-RFC2616 or CONNECT which is weird. */
  29.         return (pipe);
  30.     }
  31.         if (req.http.Cache-Control ~ "no-cache") {
  32.                 purge_url(req.url);
  33.         }
  34.     if (req.request != "GET" && req.request != "HEAD") {
  35.         /* We only deal with GET and HEAD by default */
  36.         return (pass);
  37.     }
  38.     if (req.http.Authorization || req.http.Cookie) {
  39.         /* Not cacheable by default */
  40.         return (pass);
  41.     }
  42.     return (lookup);
  43. }
  44. #
  45. sub vcl_pipe {
  46. #    # Note that only the first request to the backend will have
  47. #    # X-Forwarded-For set.  If you use X-Forwarded-For and want to
  48. #    # have it set for all requests, make sure to have:
  49. #    # set req.http.connection = "close";
  50. #    # here.  It is not set by default as it might break some broken web
  51. #    # applications, like IIS with NTLM authentication.
  52.         set bereq.http.connection = "close";
  53.     return (pipe);
  54. }
  55. #
  56. sub vcl_pass {
  57.     return (pass);
  58. }
  59. #
  60. sub vcl_hash {
  61.     set req.hash += req.url;
  62.         if (req.http.Accept-Encoding ~ "gzip") {
  63.                 set req.hash += "gzip";
  64.         } else if (req.http.Accept-Encoding ~ "deflate") {
  65.                 set req.hash += "deflate";
  66.         }
  67.     if (req.http.host) {
  68.         set req.hash += req.http.host;
  69.     } else {
  70.         set req.hash += server.ip;
  71.     }
  72.     return (hash);
  73. }
  74. #
  75. sub vcl_hit {
  76.         if (req.request == "PURGE") {
  77.                 set obj.ttl = 0s;
  78.                 error 200 "Purged.";
  79.         }
  80.     if (!obj.cacheable) {
  81.         return (pass);
  82.     }
  83.     return (deliver);
  84. }
  85. #
  86. sub vcl_miss {
  87.         if (req.request == "PURGE") {
  88.                 error 404 "Not in cache.";
  89.         }
  90.     return (fetch);
  91. }
  92. #
  93. sub vcl_fetch {
  94.         set obj.grace = 30s;
  95.     if (!obj.cacheable) {
  96.         return (pass);
  97.     }
  98.     if (obj.http.Set-Cookie) {
  99.         return (pass);
  100.     }
  101.     set obj.prefetch =  -30s;
  102.     return (deliver);
  103. }
  104. #
  105. sub vcl_deliver {
  106.         if (obj.hits > 0) {
  107.                 set resp.http.X-Cache = "HIT";
  108.         } else {
  109.                 set resp.http.X-Cache = "MISS";
  110.         }
  111.     return (deliver);
  112. }
  113. #
  114. sub vcl_discard {
  115. #    /* XXX: Do not redefine vcl_discard{}, it is not yet supported */
  116.     return (discard);
  117. }
  118. #
  119. sub vcl_prefetch {
  120. #    /* XXX: Do not redefine vcl_prefetch{}, it is not yet supported */
  121.     return (fetch);
  122. }
  123. #
  124. sub vcl_timeout {
  125. #    /* XXX: Do not redefine vcl_timeout{}, it is not yet supported */
  126.     return (discard);
  127. }
  128. #
  129. sub vcl_error {
  130.     set obj.http.Content-Type = "text/html; charset=utf-8";
  131.     synthetic {"
  132. <?xml version="1.0" encoding="utf-8"?>
  133. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  134.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  135. <html>
  136.   <head>
  137.     <title>"} obj.status " " obj.response {"</title>
  138.   </head>
  139.   <body>
  140.     <h1>Error "} obj.status " " obj.response {"</h1>
  141.     <p>"} obj.response {"</p>
  142.     <h3>Guru Meditation:</h3>
  143.     <p>XID: "} req.xid {"</p>
  144.     <address>
  145.        <a href="http://www.varnish-cache.org/">Varnish</a>
  146.     </address>
  147.   </body>
  148. </html>
  149. "};
  150.     return (deliver);
  151. }

欢迎各位看客指出我配置上的问题。我也是在学习使用中。

RSS feed for comments on this post · TrackBack URL

发表评论

You must be logged in to post a comment.