High hit rate varnish cache configuration Sharing _ server other

Source: Internet
Author: User
Tags bz2 varnish

To everyone a even I so ugly blog, hit rate can reach 75% of the varnish configuration, we take to the situation under their own site optimization, maybe also can reach 90%, this is not impossible.

System: CentOS 5.x
Software: varnish-3.0.5

1. Install varnish

How to install I will not say it, search yourself.

2.varnish Configuration


Copy Code code as follows:

Backend Slogra {
. Host = "172.0.0.1";
. Port = "80";
. connect_timeout = 20s;
. first_byte_timeout = 20s;
. between_bytes_timeout = 20s;
}

#允许刷新缓存的规则
ACL Purgeallow {
# can only be refreshed on this machine
"LocalHost";
}

# Below is a commented-out copy of the default VCL logic. If You
# Redefine any of these subroutines, the built-in logic would be
# appended to your code.

Sub Vcl_recv {
#判断请求主机, jump to the appropriate back-end server
if (req.http.host ~ "^ (. *) (slogra.com)")
{
Set Req.backend=slogra;
}else{
Error 408 "Hostname not Found";
}

#grace缓存过期仍存放
# If the backend is healthy, then only Grace 5s, if backend is not healthy, then grace 1m.
# here, the purpose of 5s is to increase throughput at high concurrency;
# 1m purpose is, backend hang up, still can continue to serve a period of time, expect backend hang not too long ...
if (req.backend.healthy) {
Set req.grace = 5s;
} else {
Set req.grace = 1m;
}

#刷新缓存的处理
if (req.request = = "PURGE") {
if (!client.ip ~ purgeallow) {
Error 405 "not allowed."
}
# #转到hit或者miss处理
return (lookup);
}

#移除一些特定格式的cookie
if (req.url ~ "^ (. *) \. ( jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm) ($|\?) ") {
#移除cookie so that it can be cached to varnish
Unset Req.http.cookie;
}

#Accept-encoding is a browser-sent server that declares the type of encoding supported by the browser.
#修正客户端的Accept-encoding Header information
#防止个别浏览器发送类似 deflate, gzip
if (req.http.accept-encoding) {
if (req.url ~ "^ (. *) \. ( JPG|PNG|GIF|JPEG|FLV|BMP|GZ|TGZ|BZ2|TBZ) ($|\?) ") {
Remove req.http.accept-encoding;
}else if (req.http.accept-encoding ~ "gzip") {
Set req.http.accept-encoding = "gzip";
else if (req.http.accept-encoding ~ "deflate") {
Set req.http.accept-encoding = "deflate";
else if (req.http.accept-encoding ~ "Sdch") {
#chrome新增加的压缩
Set req.http.accept-encoding = "SDCH";
}else {
Remove req.http.accept-encoding;
}
}
#首次访问增加X-forwarded-for header information to facilitate back-end programs to obtain client IP
if (Req.restarts = = 0) {
if (req.http.x-forwarded-for) {
Set req.http.x-forwarded-for =
Req.http.x-forwarded-for + "," + client.ip;
} else {
Set req.http.x-forwarded-for = Client.ip;
}
}

if (req.request!= "Get" &&
Req.request!= "Head" &&
Req.request!= "put" &&
Req.request!= "POST" &&
Req.request!= "TRACE" &&
Req.request!= "Options" &&
Req.request!= "DELETE") {
return (pipe);
}
if (req.request!= "get" && req.request!= "Head") {
/* We only deal with get and head by default * *
return (pass);
}
if (req.http.Authorization) {
/* not cacheable by default * *
return (pass);
}
#js, CSS files have cookies, not every time to go to the backend server to fetch
#if (Req.http.Cookie) {
#/* Not cacheable by default * *
# return (pass);
#}

#如果请求的是动态页面直接转发到后端服务器
if (req.url ~ "^ (. *) \. ( PHP|JSP|DO|ASPX|ASMX|ASHX) ($|. *)") {
return (pass);
}
return (lookup);
}
Sub Vcl_pipe {
# That's only the ' the ' the ' backend'll have
# x-forwarded-for set. If use x-forwarded-for and want to
# have it set for all requests, make sure to have:
# set bereq.http.connection = ' close ';
# here. It is isn't set by default as it might break some broken web
# applications, like IIS with NTLM authentication.
return (pipe);
}
#放过, let it go directly to the background server requesting data
Sub Vcl_pass {
return (pass);
}
Sub Vcl_hash {
Hash_data (Req.url);
if (req.http.host) {
Hash_data (Req.http.host);
} else {
Hash_data (SERVER.IP);
}
#支持压缩的要增加 to prevent content from being sent to a browser that does not support compression
if (req.http.accept-encoding) {
Hash_data (req.http.accept-encoding);
}
return (hash);
}

#缓存服务器lookup查找命中: Hit
Sub Vcl_hit {
#刷新缓存的请求操作, set the TTL to 0 and return the processing result code
if (req.request = = "PURGE") {
set obj.ttl = 0s;
Error "purged."
}
#缓存服务器命中后 (found)
return (deliver);
}
#缓存服务器lookup查找没有命中: Miss
Sub Vcl_miss {
#刷新缓存的请求操作,
#if (Req.request = = "PURGE") {
# error 404 "not in cache.";
#}
#缓存服务器没有命中 (go to backend server to fetch)
return (fetch);
}
#从后台服务器取回数据后, Cache if appropriate
Sub Vcl_fetch {
#如果请求的是动态页面直接发转发
#动态请求回来的, be sure to put it in front of the handle
if (req.url ~ "^ (. *) \. ( PHP|JSP|DO|ASPX|ASMX|ASHX) ($|. *)") {
Set beresp.http.cache-control= "No-cache, No-store";
Unset beresp.http.Expires;
return (deliver);
}
# set Beresp.grace only if the request can be cached, and if the request cannot be cached, do not set Beresp.grace
if (beresp.ttl > 0s) {
Set beresp.grace = 1m;
}
if (beresp.ttl <= 0s | |
Beresp.http.set-cookie | |
Beresp.http.Vary = = "*") {
/*
* Mark as "Hit-for-pass" for the next 2 minutes
*/
Set Beresp.ttl = S;
#下次请求时不进行lookup, Direct Pass
return (Hit_for_pass);
}
#设置从后台服务器获得的特定格式文件的缓存TTL
if (req.url ~ "^ (. *) \. ( Pdf|xls|ppt|doc|docx|xlsx|pptx|chm|rar|zip) ($|\?) ")
{
#移除服务器发送的cookie
Unset Beresp.http.set-cookie;
#加上缓存时间
Set beresp.ttl = 30d;
return (deliver);
}else if (req.url ~ "^ (. *) \. ( bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm) ($|\?) ") {
#移除服务器发送的cookie
Unset Beresp.http.set-cookie;
#加上缓存时间
Set beresp.ttl = 15d;
return (deliver);
}else if (req.url ~ "^ (. *) \. ( MP3|WMA|MP4|RMVB|OGG|MOV|AVI|WMV|MPEG|MPG|DAT|3PG|SWF|FLV|ASF) ($|\?) ") {
#移除服务器发送的cookie
Unset Beresp.http.set-cookie;
#加上缓存时间
Set beresp.ttl = 30d;
return (deliver);
}
#从后台服务器返回的response信息中, not cached, not cached
if (beresp.http.Pragma ~ "No-cache" | | | Beresp.http.cache-control ~ "No-cache" | | Beresp.http.cache-control ~ "Private") {
return (deliver);
}
return (deliver);
}
#缓存服务器发送到客户端前调用
Sub Vcl_deliver {
#下面是添加一个Header标识 to determine if the cache hit.
if (Obj.hits > 0) {
Set Resp.http.x-cache = "HIT from Cache";
#set resp.http.x-varnish = "HIT from cache";
} else {
Set Resp.http.x-cache = "MISS from Cache";
#set resp.http.x-varnish = "MISS from cache";
}
#去掉不是必须的header
Unset resp.http.Vary;
Unset resp.http.x-powered-by;
Unset resp.http.x-aspnet-version;
return (deliver);
}

Sub Vcl_error {
Set obj.http.content-type = "text/html; Charset=utf-8 ";
Set obj.http.retry-after = "5";
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" >
<title> "} + Obj.status +" "+ Obj.response + {" </title>
<body>
<p> "} + obj.response + {" </p>
<p>xid: "} + Req.xid + {" </p>
<p>cache server</p>
</body>
"};
return (deliver);
}
Sub Vcl_init {
return (OK);
}
Sub Vcl_fini {
return (OK);
}

3. Effect drawing

OK, everyone is interested, you can do it yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.