높 은 명중률 의 varnish 캐 시 설정 공유

저 처럼 못 생 긴 블 로그 도 명중률 75%에 달 할 수 있 는 varnish 설정 을 드 리 겠 습 니 다.여러분 이 자신의 사이트 상황 에 따라 최적화 하면 90%에 이 를 수 있 습 니 다.이것 도 불가능 한 일이 아 닙 니 다.
시스템:centos 5.x
소프트웨어:varnish-3.0.5
1.varnish 설치
어떻게 설치 하 는 지 는 말 하지 않 겠 습 니 다.직접 찾 아 보 겠 습 니 다.
2.varnish 설정

  backend slogra {
     .host = "172.0.0.1";
     .port = "80";
     .connect_timeout = 20s;
     .first_byte_timeout = 20s;
     .between_bytes_timeout = 20s;
 }
 
#
acl purgeAllow {
#    
     "localhost";
}
 
# Below is a commented-out copy of the default VCL logic.  If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
 
sub vcl_recv {
    # ,
    if(req.http.host ~ "^(.*)(slogra.com)")
    {
        set req.backend=slogra;
    }else{
        error 408 "Hostname not found"; 
    }
    
    #grace
    # backend , grace 5s, backend , grace 1m。
    # ,5s ;
    # 1m ,backend , , backend 。。。
    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, varnish
         unset req.http.cookie;
    }
 
   #Accept-Encoding ,
   # Accept-Encoding
   # 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 , 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 Cookie,
     #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 {
     # Note that only the first request to the backend will have
     # X-Forwarded-For set.  If you 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 not set by default as it might break some broken web
     # applications, like IIS with NTLM authentication.
     return (pipe);
 }   
# ,
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);
     }
     # ,
     if(req.http.Accept-Encoding){
          hash_data(req.http.Accept-Encoding);
     }
     return (hash);
 }
 
# lookup :hit
 sub vcl_hit {
     # , TTL 0,
     if (req.request == "PURGE") {
          set obj.ttl = 0s;
          error 200 "Purged.";
      }  
     # ( )
     return (deliver);
 }   
# lookup :miss
sub vcl_miss {
    # ,
    #if (req.request == "PURGE") {
    #    error 404 "Not in cache.";
    #}  
    # ( )
     return (fetch);
 }  
# ,
sub vcl_fetch {
    #
    # ,
    if (req.url ~ "^(.*)\.(php|jsp|do|aspx|asmx|ashx)($|.*)") {
        set beresp.http.Cache-Control="no-cache, no-store";
        unset beresp.http.Expires;
        return (deliver);
    }  
    # , beresp.grace, , 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 = 120 s;
            # lookup, 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 , ,
    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 , 。
    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">
 <html>
   <head>
     <title>"} + obj.status + " " + obj.response + {"</title>
   </head>
   <body>
     <h1>Error "} + obj.status + " " + obj.response + {"</h1>
     <p>"} + obj.response + {"</p>
     <h3>Guru Meditation:</h3>
     <p>XID: "} + req.xid + {"</p>
     <hr>
     <p>cache server</p>
   </body>
 </html>
 "};
     return (deliver);
 }   
 sub vcl_init {
    return (ok);
 }  
 sub vcl_fini {
    return (ok);
 }
3.효과 도

자,여러분 이 관심 이 있 으 면 직접 하 셔 도 됩 니 다.

좋은 웹페이지 즐겨찾기