当前位置: > Linux服务器 > 服务器设置 >

高命中率varnish缓存配置

时间:2014-12-12 00:22来源:linux.it.net.cn 作者:IT
  给大家一个连我这么丑的blog,命中率都可以达到75%的varnish配置,大家拿去根据自己网站情况再优化下的话,说不定也可以达到90%,这也不是不可能的事.
  系统:centos 5.x
  软件:varnish-3.0.5
 1.安装varnish
 怎么安装我就不说了吧,自己搜去.

 2.varnish配置
001 backend slogra { 
002      .host = "172.0.0.1"
003      .port = "80"
004      .connect_timeout = 20s; 
005      .first_byte_timeout = 20s; 
006      .between_bytes_timeout = 20s; 
007  
008   
009 #允许刷新缓存的规则 
010 acl purgeAllow { 
011 #     只能本机进行刷新 
012      "localhost"
013
014    
015 # Below is a commented-out copy of the default VCL logic.  If you 
016 # redefine any of these subroutines, the built-in logic will be 
017 # appended to your code. 
018   
019 sub vcl_recv { 
020     #判断请求主机,跳转到相应后端服务器 
021     if(req.http.host ~ "^(.*)(slogra.com)"
022     
023         set req.backend=slogra; 
024     }else
025         error 408 "Hostname not found";  
026     
027       
028     #grace缓存过期仍存放 
029     # 若backend是健康的,则仅grace 5s,如果backend不健康,则grace 1m。 
030     # 这里,5s的目的是为了提高高并发时的吞吐率; 
031     # 1m的目的是,backend挂了之后,还能继续服务一段时间,期望backend挂的不要太久。。。 
032     if (req.backend.healthy) { 
033         set req.grace = 5s; 
034     } else
035         set req.grace = 1m; 
036     
037   
038     #刷新缓存的处理 
039     if (req.request == "PURGE"){ 
040         if(!client.ip ~ purgeAllow) { 
041                 error 405 "Not allowed."
042         
043     #    #转到hit或者miss处理 
044         return (lookup); 
045     
046   
047     #移除一些特定格式的cookie 
048     if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm)($|\?)" ) { 
049          #移除cookie,以便能缓存到varnish 
050          unset req.http.cookie; 
051     
052   
053    #Accept-Encoding 是浏览器发给服务器,声明浏览器支持的编码类型的 
054    #修正客户端的Accept-Encoding头信息 
055    #防止个别浏览器发送类似 deflate, gzip 
056     if (req.http.Accept-Encoding) { 
057         if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz)($|\?)" ) { 
058             remove req.http.Accept-Encoding; 
059         }else if (req.http.Accept-Encoding ~ "gzip"){ 
060             set req.http.Accept-Encoding = "gzip"
061         } else if (req.http.Accept-Encoding ~ "deflate"){ 
062             set req.http.Accept-Encoding = "deflate"
063         } else if (req.http.Accept-Encoding ~ "sdch"){ 
064             #chrome新增加的压缩 
065             set req.http.Accept-Encoding = "sdch"
066         }else
067             remove req.http.Accept-Encoding; 
068         
069     }         
070     #首次访问增加X-Forwarded-For头信息,方便后端程序获取客户端ip 
071     if (req.restarts == 0) { 
072         if (req.http.x-forwarded-for) { 
073             set req.http.X-Forwarded-For = 
074             req.http.X-Forwarded-For + ", " + client.ip; 
075         } else
076             set req.http.X-Forwarded-For = client.ip; 
077         
078     
079   
080    if (req.request != "GET" && 
081        req.request != "HEAD" && 
082        req.request != "PUT" && 
083        req.request != "POST" && 
084        req.request != "TRACE" && 
085        req.request != "OPTIONS" && 
086        req.request != "DELETE") { 
087        return (pipe); 
088    
089      if (req.request != "GET" && req.request != "HEAD") { 
090          /* We only deal with GET and HEAD by default */ 
091          return (pass); 
092      
093      if (req.http.Authorization) { 
094          /* Not cacheable by default */ 
095          return (pass); 
096      
097      #js,css文件都有Cookie,不能每次都去后台服务器去取 
098      #if (req.http.Cookie) { 
099      #    /* Not cacheable by default */ 
100      #    return (pass); 
101      #} 
102       
103      #如果请求的是动态页面直接转发到后端服务器 
104      if (req.url ~ "^(.*)\.(php|jsp|do|aspx|asmx|ashx)($|.*)") { 
105           return (pass); 
106      
107      return (lookup); 
108  }    
109  sub vcl_pipe { 
110      # Note that only the first request to the backend will have 
111      # X-Forwarded-For set.  If you use X-Forwarded-For and want to 
112      # have it set for all requests, make sure to have: 
113      # set bereq.http.connection = "close"; 
114      # here.  It is not set by default as it might break some broken web 
115      # applications, like IIS with NTLM authentication. 
116      return (pipe); 
117  }    
118 #放过,让其直接去后台服务器请求数据 
119 sub vcl_pass { 
120      return (pass); 
121  }    
122 sub vcl_hash { 
123      hash_data(req.url); 
124      if (req.http.host) { 
125          hash_data(req.http.host); 
126      } else
127          hash_data(server.ip); 
128      
129      #支持压缩的要增加,防止发送给不支持压缩的浏览器压缩的内容 
130      if(req.http.Accept-Encoding){ 
131           hash_data(req.http.Accept-Encoding); 
132      
133      return (hash); 
134  
135    
136 #缓存服务器lookup查找命中:hit 
137  sub vcl_hit { 
138      #刷新缓存的请求操作,设置TTL为0,返回处理结果代码 
139      if (req.request == "PURGE") { 
140           set obj.ttl = 0s; 
141           error 200 "Purged."
142       }   
143      #缓存服务器命中后(查找到了) 
144      return (deliver); 
145  }    
146 #缓存服务器lookup查找没有命中:miss 
147 sub vcl_miss { 
148     #刷新缓存的请求操作, 
149     #if (req.request == "PURGE") { 
150     #    error 404 "Not in cache."; 
151     #}   
152     #缓存服务器没有命中(去后台服务器取) 
153      return (fetch); 
154  }   
155 #从后台服务器取回数据后,视情况是否进行缓存 
156 sub vcl_fetch { 
157     #如果请求的是动态页面直接发转发 
158     #动态请求回来的,一定要放在前面处理 
159     if (req.url ~ "^(.*)\.(php|jsp|do|aspx|asmx|ashx)($|.*)") { 
160         set beresp.http.Cache-Control="no-cache, no-store"
161         unset beresp.http.Expires; 
162         return (deliver); 
163     }   
164     # 仅当该请求可以缓存时,才设置beresp.grace,若该请求不能被缓存,则不设置beresp.grace 
165     if (beresp.ttl > 0s) { 
166         set beresp.grace = 1m; 
167     }     
168      if (beresp.ttl <= 0s || 
169          beresp.http.Set-Cookie || 
170          beresp.http.Vary == "*") { 
171             /* 
172              * Mark as "Hit-For-Pass" for the next 2 minutes 
173              */ 
174             set beresp.ttl = 120 s; 
175             #下次请求时不进行lookup,直接pass 
176             return (hit_for_pass); 
177      }   
178     #设置从后台服务器获得的特定格式文件的缓存TTL 
179     if (req.url ~ "^(.*)\.(pdf|xls|ppt|doc|docx|xlsx|pptx|chm|rar|zip)($|\?)")      
180     
181         #移除服务器发送的cookie  
182         unset beresp.http.Set-Cookie; 
183         #加上缓存时间 
184         set beresp.ttl = 30d; 
185         return (deliver); 
186     }else if(req.url ~ "^(.*)\.(bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm)($|\?)"){ 
187         #移除服务器发送的cookie  
188         unset beresp.http.Set-Cookie; 
189         #加上缓存时间 
190         set beresp.ttl = 15d; 
191         return (deliver); 
192     }else if(req.url ~ "^(.*)\.(mp3|wma|mp4|rmvb|ogg|mov|avi|wmv|mpeg|mpg|dat|3pg|swf|flv|asf)($|\?)"){ 
193         #移除服务器发送的cookie  
194         unset beresp.http.Set-Cookie; 
195         #加上缓存时间 
196         set beresp.ttl = 30d; 
197         return (deliver); 
198     }   
199     #从后台服务器返回的response信息中,没有缓存的,不缓存 
200     if (beresp.http.Pragma ~"no-cache" || beresp.http.Cache-Control ~"no-cache" || beresp.http.Cache-Control ~"private") { 
201             return (deliver); 
202     
203     return (deliver); 
204  }    
205 #缓存服务器发送到客户端前调用 
206  sub vcl_deliver { 
207     #下面是添加一个Header标识,以判断缓存是否命中。 
208     if (obj.hits > 0) { 
209         set resp.http.X-Cache = "HIT from cache"
210        #set resp.http.X-Varnish = "HIT from cache"; 
211     } else
212         set resp.http.X-Cache = "MISS from cache"
213        #set resp.http.X-Varnish = "MISS from cache"; 
214     
215     #去掉不是必须的header 
216     unset resp.http.Vary; 
217     unset resp.http.X-Powered-By; 
218     unset resp.http.X-AspNet-Version; 
219     return (deliver); 
220  
221    
222  sub vcl_error { 
223      set obj.http.Content-Type = "text/html; charset=utf-8"
224      set obj.http.Retry-After = "5"
225      synthetic {" 
226  <?xml version="1.0" encoding="utf-8"?> 
227  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
229  <html> 
230    <head
231      <title>"} + obj.status + " " + obj.response + {"</title> 
232    </head
233    <body> 
234      <h1>Error "} + obj.status + " " + obj.response + {"</h1> 
235      <p>"} + obj.response + {"</p> 
236      <h3>Guru Meditation:</h3> 
237      <p>XID: "} + req.xid + {"</p> 
238      <hr> 
239      <p>cache server</p> 
240    </body> 
241  </html> 
242  "}; 
243      return (deliver); 
244  }    
245  sub vcl_init { 
246     return (ok); 
247  }   
248  sub vcl_fini { 
249     return (ok); 
250  }


 3.效果图
点击查看原图
 好了,大家有兴趣的,可以自己去搞.
(责任编辑:IT)
------分隔线----------------------------