1. 程式人生 > >Varnish配置示例

Varnish配置示例

vcl .org RM mp3 o-c take import adapt ret

網上收集、整理的Varnish 4.0 高命中率配置示例:
[root@node1 varnish]# cat  web.vcl 
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

# Default backend definition. Set this to point to your content server.
probe backend_health_check {
    .url = "/";
    .interval = 10s;
    .window = 5 ;
    .threshold = 3;
}
backend web1 {
    .host = "172.16.1.51";
    .port = "80";
    .probe = backend_health_check ;
}
backend web2 {
    .host = "172.16.1.52";
    .port = "80";
    .probe = backend_health_check ;
}

import directors;
sub vcl_init {
    new  web_cluster = directors.random();
    web_cluster.add_backend(web1,1);
    web_cluster.add_backend(web2,2);
}
acl purges {
    "localhost";
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don‘t need,
    # rewriting the request, etc.
    #判斷請求主機、轉發至後端服務器,若需要支持後端虛擬主機,也可在此設置
    if(req.http.host ~ "(?i)(.*)hurn\.com") {
    set req.backend_hint = web_cluster.backend();
    } else {
    return (synth( 408 ,"Host Not Found"));
        }
    #定義緩存修剪
    if(req.method == "PURGE") {
        if(!client.ip ~ purges) {
            return (synth(403,"Not Allow Purge"));
            }
        return(purge);
    }
    #定義到收到無法理解的請求時,如何處理
    if( req.method != "GET" && 
    req.method != "HEAD" &&
    req.method != "PUT" &&
    req.method != "POST" &&
    req.method != "TRACE" &&
    req.method != "OPTIONS" &&
    req.method != "DELETE" ) {
    return(pipe);
        }

    #定義緩存類型,如果不是GET、HEAD方式都不緩存
    if(req.method != "GET" || req.method != "HEAD" ) {
    return(pass);
    }
    #如果有認證頭部,也不緩存
    if(req.http.Authorization) {
    return(pass);
    }
    #清除客戶端發送至後端服務器時公共文件的cookie
    if (req.url ~ "(?i)(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm)($|\?)") {
    unset req.http.Cookie;
    }
    #不緩存帶Cookie的文件,js,css都帶Cookie,如果需要可緩存
    if(req.http.Cookie) {
    return(pass);
    }
    #定義特殊目錄不緩存:
    if(req.url ~ "^/admin" || req.url ~ "^/login" ){
    return(pass);
    }
    #動態內容不緩存,直接發往後端服務器
    if(req.url ~ "(?i)(.*)\.(php|jsp|aspx)($|.*)") {
    return(pass);
    }
    #設置X-Forward-For 記錄客戶端的IP,方便後端服務器記錄日誌
    if(req.http.X-Forward-For) {
    set req.http.X-Forward-For = req.http.X-Forward-For + " , " + client.ip ;
    } else {
    set req.http.X-Forward-For = client.ip ;
    }   
    #Accept-Encoding 是瀏覽器發給服務器,聲明瀏覽器支持的編碼類型的  
    #修正客戶端的Accept-Encoding頭信息 
    #防止個別瀏覽器發送類似 deflate, gzip 
    if (req.http.Accept-Encoding) { 
        if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz)($|\?)" ) { 
            unset 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 { 
            unset req.http.Accept-Encoding; 

        } 
    }         
    #剩余內容都去查緩存
    return(hash);
}
#定義清理緩存
sub vcl_purge {
    return(synth(200,"Purged"));
}
sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
    #如果請求的是動態頁面直接發轉發 
    #動態請求回來的,一定要放在前面處理 
    if (bereq.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 = 10m; 
    }
   #設置從後臺服務器獲得的特定格式文件的緩存TTL    
 if (bereq.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(bereq.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(bereq.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{
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
    #添加一個Header標識,以判斷緩存是否命中。 
    if (obj.hits > 0) { 
        set resp.http.X-Cache = "HIT from cache"; 
    } else { 
        set resp.http.X-Cache = "MISS from cache"; 
    } 
    #去掉頭部
    unset  resp.http.X-Varnish;
    unset  resp.http.Via;
    set    resp.http.Server = "Web Server";

}

Varnish配置示例