因为在网站里面有大些页面要频繁查询数据库,导致页面打开缓慢.本来是有做CDN,但是对于一些访问很少的页面,CDN会把缓存的内容清理掉.所以就只能在我们自己的服务器上进行缓存.

最后选择了varnish这个软件.安装是用yum进行安装的.没有使用源码编译.

在这里主要记录一下配置的记录.

最开始架构时没有想做专业的缓存服务器,所以现在要加缓存功能,就只能在WEB服务器上添加一个服务.

方法1:

使用varnish接管80端口,并代理到后端WEB服务器.

方法2:

使用WEB服务代理到varnish的端口,varnish的后端为提供WEB服务的站点.

因为我的服务器上有好几个WEB服务,所以就只选择了第二种方法.

将原先的站点端口改为其他端口,然后再写一个反向代理配置文件,代理到varnish上.

 

 
  
  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 default { 
  8.   .host = "127.0.0.1"
  9.   .port = "80"
  10. #  
  11. # Below is a commented-out copy of the default VCL logic.  If you 
  12. # redefine any of these subroutines, the built-in logic will be 
  13. # appended to your code. 
  14. sub vcl_recv {                             
  15.      if (req.restarts == 0) { 
  16.     if (req.http.x-forwarded-for) { 
  17.         set reqreq.http.X-Forwarded-For = 
  18.         req.http.X-Forwarded-For + ", " + client.ip; 
  19.     } else { 
  20.         set req.http.X-Forwarded-For = client.ip; 
  21.     } 
  22.      } 
  23.      if (req.request != "GET" && 
  24.        req.request != "HEAD" && 
  25.        req.request != "PUT" && 
  26.        req.request != "POST" && 
  27.        req.request != "TRACE" && 
  28.       req.request != "OPTIONS" && 
  29.        req.request != "DELETE") { 
  30.          /* Non-RFC2616 or CONNECT which is weird. */ 
  31.          return (pipe);                         
  32.      } 
  33.  
  34.      if (req.http.Authorization || req.http.Cookie) { 
  35.          /* Not cacheable by default */ 
  36.          return (pass);                     
  37.      } 
  38.      return (lookup); 
  39.  
  40.     if (req.http.Accept-Encoding) {              
  41.     if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") { 
  42.         # No point in compressing these 
  43.         remove req.http.Accept-Encoding; 
  44.     } elsif (req.http.Accept-Encoding ~ "gzip") { 
  45.         set req.http.Accept-Encoding = "gzip"
  46.     } elsif (req.http.Accept-Encoding ~ "deflate") { 
  47.         set req.http.Accept-Encoding = "deflate"
  48.     }else { 
  49.         # unkown algorithm 
  50.         remove req.http.Accept-Encoding; 
  51.     } 
  52.      set req.grace = 15s
  53.     if (req.url ~ "\.(css|jpg|jpeg|png|gif|swf)$") { 
  54.                 unset req.http.Cookie; 
  55.                 return (lookup);               
  56.         } 
  57.  } 
  58.   
  59.  sub vcl_pipe { 
  60.      # Note that only the first request to the backend will have 
  61.      # X-Forwarded-For set.  If you use X-Forwarded-For and want to 
  62.      # have it set for all requests, make sure to have: 
  63.      # set bereq.http.connection = "close"
  64.      # here.  It is not set by default as it might break some broken web 
  65.      # applications, like IIS with NTLM authentication. 
  66.      return (pipe); 
  67.  } 
  68.   
  69.  sub vcl_pass { 
  70.      return (pass); 
  71.  } 
  72.   
  73.  sub vcl_hash { 
  74.      hash_data(req.url); 
  75.      if (req.http.host) { 
  76.          hash_data(req.http.host); 
  77.      } else { 
  78.          hash_data(server.ip); 
  79.      } 
  80.      return (hash); 
  81.  } 
  82.   
  83.  sub vcl_hit { 
  84.      return (deliver); 
  85.  } 
  86.   
  87.  sub vcl_miss { 
  88.      return (fetch); 
  89.  } 
  90.   
  91.  sub vcl_fetch { 
  92.      if (beresp.ttl <= 0s || 
  93.          beresp.http.Set-Cookie || 
  94.          beresp.http.Vary == "*") { 
  95.         /* 
  96.          * Mark as "Hit-For-Pass" for the next 2 minutes 
  97.          */ 
  98.         set beresp.ttl = 120 s; 
  99.         return (hit_for_pass); 
  100.      } 
  101.      return (deliver); 
  102.       if (req.request == "GET" && req.url ~ "\.(jpg|png|gif|swf|flv|ico|jpeg)$") { 
  103.         set beresp.ttl = 1d;         
  104.      } 
  105.      if (req.url ~ "\.(js|css|jpg|jpeg|png|gif|swf)$") { 
  106.                 unset beresp.http.set-cookie; 
  107.             set beresp.ttl = 1d;       
  108.     } 
  109.      if (req.request == "GET" && req.url ~ "\.(htm|html)$") { 
  110.         set beresp.ttl = 1d;       
  111.      } 
  112.      if (beresp.http.Pragma ~ "no-cache" || 
  113.         beresp.http.Cache-Control ~ "no-cache" || 
  114.         beresp.http.Cache-Control ~ "private") { 
  115.         return(deliver); 
  116.      } 
  117.      if(beresp.status == 404 || beresp.status == 300) { 
  118.         error 404; 
  119.      } 
  120.     set beresp.grace = 30m
  121.  } 
  122.   
  123.  sub vcl_deliver { 
  124.      return (deliver); 
  125.      if (obj.hits > 0) { 
  126.  
  127.      set resp.http.X-Cache = "HIT"
  128.      set resp.http.X-Cache-Hits = obj.hits; 
  129.      }else { 
  130.      set resp.http.X-Cache = "MISS"
  131.      }  
  132.  
  133.  
  134.   
  135.  sub vcl_error { 
  136.      set obj.http.Content-Type = "text/html; charset=utf-8"
  137.      set obj.http.Retry-After = "5"
  138.      synthetic {" 
  139.  <?xml version="1.0" encoding="utf-8"?> 
  140.  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
  141.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  142.  <html> 
  143.    <head> 
  144.      <title>"} + obj.status + " " + obj.response + {"</title> 
  145.    </head> 
  146.    <body> 
  147.      <h1>Error "} + obj.status + " " + obj.response + {"</h1> 
  148.      <p>"} + obj.response + {"</p> 
  149.      <h3>Guru Meditation:</h3> 
  150.      <p>XID: "} + req.xid + {"</p> 
  151.      <hr> 
  152.      <p>Varnish cache server</p> 
  153.    </body> 
  154.  </html> 
  155.  "}; 
  156.      return (deliver); 
  157.  } 
  158.   
  159.  sub vcl_init { 
  160.     return (ok); 
  161.  } 
  162.   
  163.  sub vcl_fini { 
  164.     return (ok); 
  165.  } 

在网上参考大神们的文档修改的,各位牛人们帮着看看有没有什么问题.

本来想加上对一些规则的注释,但是发现写不好,怕误导别人,所以还是不写的好.