Varnish系列之三:动静分离架构与实现

Varnish状态引擎说明:

sub vcl_recv {

    if (req.method == "PRI") {

/* We do not support SPDY or HTTP/2.0 */

        return (synth(405));

    }

    if (req.method != "GET" &&

      req.method != "HEAD" &&

      req.method != "PUT" &&

      req.method != "POST" &&

      req.method != "TRACE" &&

      req.method != "OPTIONS" &&

      req.method != "DELETE") {

        /* Non-RFC2616 or CONNECT which is weird. */

        return (pipe);

    }

    if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm)($|\?)") {

        unset req.http.cookie;

    }


    if (req.url ~ "^(.*)\.(php|jsp|do|aspx|asmx|ashx)($|.*)") { 

        return (pass);

    }

    if (req.method != "GET" && req.method != "HEAD") {

        /* We only deal with GET and HEAD by default */

        return (pass);

    }

    if (req.http.Authorization || req.http.Cookie) {

        /* Not cacheable by default */

        return (pass);

    }

    return (hash);

}

sub vcl_backend_response {

    if (bereq.url ~ "^(.*)\.(pdf|xls|ppt|doc|docx|xlsx|pptx|chm|rar|zip)($|\?)") {   

        unset beresp.http.Set-Cookie;   

        set beresp.ttl = 2h; 

        return (deliver); 

    } else if(bereq.url ~ "^(.*)\.(bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm)($|\?)") { 

        unset beresp.http.Set-Cookie; 

        set beresp.ttl = 2h; 

        return (deliver); 

    } else if(bereq.url ~ "^(.*)\.(mp3|wma|mp4|rmvb|ogg|mov|avi|wmv|mpeg|mpg|dat|3pg|swf|flv|asf)($|\?)") {   

        unset beresp.http.Set-Cookie; 

        set beresp.ttl = 2h; 

        return (deliver); 

    }   

    if (beresp.ttl <= 0s ||

      beresp.http.Set-Cookie ||

      beresp.http.Surrogate-control ~ "no-store" ||

      (!beresp.http.Surrogate-Control &&

        beresp.http.Cache-Control ~ "no-cache|no-store|private") ||

      beresp.http.Vary == "*") {

        /*

        * Mark as "Hit-For-Pass" for the next 2 minutes

        */

        set beresp.ttl = 120s;

        set beresp.uncacheable = true;

    }

    return (deliver);

}

参考文档:

    https://varnish-cache.org/trac/wiki/VCLExamples

    https://varnish-cache.org/docs/4.1/reference/index.html

    https://varnish-cache.org/docs/4.1/users-guide/vcl-built-in-subs.html#vcl-built-in-subs

你可能感兴趣的:(Varnish系列之三:动静分离架构与实现)