美文网首页
Varnish系列之三:动静分离架构与实现

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

作者: 平平淡淡的猪 | 来源:发表于2018-04-03 15:57 被阅读0次

    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系列之三:动静分离架构与实现

          本文链接:https://www.haomeiwen.com/subject/wnqchftx.html