varnish加速网站

可能现在很多人还是用Squid来做代理。我测试了一下,使用Varnish来加速的效果比Squid的更理想。现在只是把服务跑了起来,还没有把日志分析程序以及报警相关的程序处理好。这几天处理好之后,我会把相关的资料更新。

版权所有,引用请先通知我!

下面是FreeBSD的优化配置文件。
/etc/sysctl.conf

  1. kern.ipc.nmbclusters=65536
  2. kern.ipc.somaxconn=16384
  3. kern.maxfiles=131072
  4. kern.maxfilesperproc=104856
  5. kern.threads.max_threads_per_proc=4096
  6. vfs.ufs.dirhash_maxmem=67108864
  7. kern.ipc.shmmax=134217728
  8. net.inet.tcp.msl=5000

/boot/loader.conf

  1. kern.ipc.maxsockets="131072"
  2. kern.ipc.maxpipekva="104857600"

内核文件里的SCHED_4BSD改为SCHED_ULE重新编译内核

上面的事情都处理好之后,接下来就是安装Varnish了。

  1. cd /usr/ports/www/varnish/
  2. make install clean

配置启动文件
/etc/rc.conf

  1. varnishd_enable="YES"
  2. varnishd_listen=:80
  3. varnishncsa_enable="YES"

/usr/local/etc/varnish/default.vcl

  1. #This is a basic VCL configuration file for varnish.  See the vcl(7)
  2. #man page for details on VCL syntax and semantics.
  3. #
  4. #Default backend definition.  Set this to point to your content
  5. #server.
  6. #
  7. backend jianblog {
  8. .host = "203.184.129.212";
  9. .port = "80";
  10. }
  11. #
  12. #Below is a commented-out copy of the default VCL logic.  If you
  13. #redefine any of these subroutines, the built-in logic will be
  14. #appended to your code.
  15. #
  16. sub vcl_recv {
  17.         set req.grace = 30s;
  18.     if (req.request != "GET" &&
  19.       req.request != "HEAD" &&
  20.       req.request != "PUT" &&
  21.       req.request != "POST" &&
  22.       req.request != "TRACE" &&
  23.       req.request != "OPTIONS" &&
  24.       req.request != "DELETE") {
  25.         /* Non-RFC2616 or CONNECT which is weird. */
  26.         pipe;
  27.     }
  28.  
  29.         if (req.http.host ~"^www.jianblog.com") {
  30.                 set req.backend = jianblog;
  31.         } elsif (req.http.host ~"^jianblog.com") {
  32.                 set req.backend = jianblog;
  33.         } elsif (req.http.host ~"^www.chinado.net") {
  34.                 set req.backend = jianblog;
  35.         } elsif (req.http.host ~"^chinado.net") {
  36.                 set req.backend = jianblog;
  37.         } else {
  38.                 error 404 "Unknown virtual host";
  39.         }
  40.  
  41.         /* Always cache images and multimedia */
  42.         if (req.url ~ "\.(jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|mp3|mp4|m4a|ogg|mov|avi|wmv)$") {
  43.                 lookup;
  44.         }
  45.  
  46.          /* Always cache CSS and javascript */
  47.         if (req.url ~ "\.(css|js)$") {
  48.                 lookup;
  49.         }
  50.  
  51.         /* Always cache static files */
  52.         if (req.url ~ "\.(pdf|xls|vsd|doc|ppt|pps|vsd|doc|ppt|pps|xls|pdf|sxw|zip|gz|bz2|tgz|tar|rar|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|dmg|torrent|deb|msi|iso|rpm)$") {
  53.                 lookup;
  54.         }
  55.  
  56.     if (req.request != "GET" && req.request != "HEAD") {
  57.         /* We only deal with GET and HEAD by default */
  58.         pipe;
  59.     }
  60.     if (req.http.Authorization || req.http.Cookie) {
  61.         /* Not cacheable by default */
  62.         pass;
  63.     }
  64.     lookup;
  65. }
  66.  
  67. sub vcl_pipe {
  68.     pipe;
  69. }
  70.  
  71. sub vcl_pass {
  72.     pass;
  73. }
  74.  
  75. sub vcl_hash {
  76.     set req.hash += req.url;
  77.     if (req.http.host) {
  78.         set req.hash += req.http.host;
  79.     } else {
  80.         set req.hash += server.ip;
  81.     }
  82.     hash;
  83. }
  84.  
  85. sub vcl_hit {
  86.         if (req.request == "PURGE") {
  87.                 set obj.ttl = 0s;
  88.                 error 404 "Not in cache.";
  89.         }
  90.  
  91.     if (!obj.cacheable) {
  92.         pass;
  93.     }
  94.     deliver;
  95. }
  96.  
  97. sub vcl_miss {
  98.     fetch;
  99. }
  100.  
  101. sub vcl_fetch {
  102.         if (req.request == "GET" && req.url ~ "\.(txt|js)$") {
  103.                 set obj.ttl = 3600s;
  104.         } else {
  105.                 set obj.ttl = 30d;
  106.         }
  107.     if (!obj.cacheable) {
  108.         pass;
  109.     }
  110.     if (obj.http.Set-Cookie) {
  111.         pass;
  112.     }
  113.         set obj.grace = 30s;
  114. #    set obj.prefetch =  -30s;
  115. #    deliver;
  116. }
  117.  
  118. sub vcl_deliver {
  119. #    deliver;
  120. }
  121.  
  122. sub vcl_discard {
  123.     /* XXX: Do not redefine vcl_discard{}, it is not yet supported */
  124. #    discard;
  125. }
  126.  
  127. sub vcl_prefetch {
  128.     /* XXX: Do not redefine vcl_prefetch{}, it is not yet supported */
  129. #    fetch;
  130. }
  131.  
  132. sub vcl_timeout {
  133.     /* XXX: Do not redefine vcl_timeout{}, it is not yet supported */
  134. #    discard;
  135. }
  136.  
  137. sub vcl_error {
  138.     set obj.http.Content-Type = "text/html; charset=utf-8";
  139.     synthetic {"
  140. <?xml version="1.0" encoding="utf-8"?>
  141. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  142.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  143. <html>
  144.   <head>
  145.     <title>"} obj.status " " obj.response {"</title>
  146.   </head>
  147.   <body>
  148.     <h1>Error "} obj.status " " obj.response {"</h1>
  149.     <p>"} obj.response {"</p>
  150.     <h3>Guru Meditation:</h3>
  151.     <p>XID: "} req.xid {"</p>
  152.     <address><a href="http://www.jianblog.com/">Jian</a></address>
  153.   </body>
  154. </html>
  155. "};
  156.     deliver;
  157. }

这两天,我会把日志分析的更新过来。

一条评论 »

  1. freeke said,

    十二月 10, 2008 at 9:00 上午

    Hey 博主,不好意思我拿了你的配置本地测试一下,可是我在访问页面时超级慢,不知道如何分析呢,我加了你MSN,估计你应该比较忙,就没敢打扰你啊!我在CU发了个贴,描述了一下我的情况,要是有空帮我分析分析吧?在此感谢啦!

    http://bbs.chinaunix.net/thread-1329583-1-1.html

RSS feed for comments on this post · TrackBack URL

发表评论

You must be logged in to post a comment.