Varnish4.x配置文件详解

来源:互联网 发布:广州火舞软件 编辑:程序博客网 时间:2024/05/01 09:37
下面是Varnish配置文件:
  # vi /usr/local/varnish/etc/varnish/default.vcl
        backend web1 {
                .host = "192.168.1.11";
                .port = "80";
        }
        backend web2 {
                .host = "192.168.1.22";
                .port = "80";
        }
        backend web3 {
                .host = "192.168.1.33";
                .port = "80";
        }
        #定义一个名为web的director进行随机的负载分担。
        director web random {
                { .backend=web1; .weight=1; }
                { .backend=web2; .weight=1; }
                { .backend=web3; .weight=1; }
        }
 #        director wameide round-robin {
 #                { .backend=web11; }
 #                { .backend=web22; }
 #        }
 #        director wameide round-robin {
 #                { .backend={ .host="192.168.1.11"; .port="http";}}
 #                { .backend={ .host="192.168.1.22"; .port="http";}}
 #        }
        #定义访问控制列表,充许那些IP清除varnish 缓存。
        acl purge {
                "127.0.0.1";
                "192.168.1.0"/24;
        }
        #vcl_recv函数用于接收和处理请求。当请求到达并被成功接收后被调用,通过判断请求的数据来决定,如何处理请求。
        sub vcl_recv {
                #判断req.http.X-Forwarded-For,如果前端有多重反向代理,这样可以获取客户端IP地址。
                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;
                }
                #如果请求的类型不是GET、HEAD、PUT、POST、TRACE、OPTIONS或DELETE 时,则进入pipe模式。注意这里是“&&”关系。
                if (req.request != "GET" &&
                        req.request != "HEAD" &&
                        req.request != "PUT" &&
                        req.request != "POST" &&
                        req.request != "TRACE" &&
                        req.request != "OPTIONS" &&
                        req.request != "DELETE") {
                        return (pipe);   #进入pipe模式,把控制权交给vcl_pipe。
                }
                #如果请求的类型不是GET或HEAD,则进入pass模式。对以.jsp 和.php和aspx和cgi结尾以及带有?的URL,直接从后端服务器读取内容。
                if (req.request != "GET" && req.request != "HEAD") {
                        return (pass);
                }
                if (req.url ~ "\.(jsp|php|aspx|cgi)($|\?)") {
                        return (pass);  
                }
                if (req.http.Authenticate || req.http.Authorization) {
                        return (pass);
                }
                #不缓存session认证。
                if (req.http.Cookie && req.http.Cookie ~ "is_logged_in=") {
                        return (pipe);
                }
                #总是缓存以www开头或wameide.com结尾的域名。
                if (req.http.host ~"^(www.)?wameide.com$" || req.http.host ~"^(.*.)?wameide.com$") {
                        set req.backend = wameide;
                } else if (req.http.host ~"^(admin.)?wameide.com$") {
                        set req.backend = wameideadmin;
                } else {
                        error 404 "Unknown virtual host";
                }
                #如果请求的是purge,确认来源地址是purge定义的可访问控制列表。当请求是以.php和.cgi结尾时,则交给后端服务器去处理。
                if (req.request == "PURGE") {
                        if(!client.ip ~ purge) {
                                error 405 "Not Allowed";
                        }else if(req.url ~ "\.(php|cgi)($|\?)") {
                                return (pass);
                        }else {
                                return (lookup);
                        }
                }
                if (req.http.Accept-Encoding) { {
                        if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
                                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 {
                                remove req.http.Accept-Encoding;
                        }
                }
                #表示在缓存中查找被请求的对象,并且根据查找的结果把控制权交给函数vcl_hit或函数vcl_miss。
                return (lookup);
        }
        #vcl_pipe函数用于进入pipe模式时被调用。请求被直接发送到backend,在请求和返回的内容没有改变的情况下,将不变的内容返回给客户端,直到链接被关闭。
        sub vcl_pipe {
                return (pipe);
        }
        #vcl_pass函数用于进入pass模式时被调用。请求被送到后端主机,后端主机在应答数据后将应答数据发送给客户端,但不进行任何缓存,在当前连接下每次都返回最新的内容。
        sub vcl_pass {
                return (pass);
        }
        #vcl_hash函数用于处理压缩内容。
        sub vcl_hash {
                set req.hash += req.url;
                if (req.http.host) {
                        set req.hash += req.http.host;
                } else {
                        set req.hash += server.ip;
                }
                if (req.http.Accept-Encoding ~ "gzip") {
                        set req.hash += "gzip";
                }
                else if (req.http.Accept-Encoding ~ "deflate") {
                        set req.hash += "deflate";
                }
                return (hash);
        }
        #vcl_hit函数,在执行lookup指令后,在缓存中找到请求的内容后将自动调用该函数。
        sub vcl_hit {
            #当遇到PURGE方法时,Varnishd会设置obj.ttl=0s;使某个URL的缓存失效,从而达到刷新Varnish缓存的目的。
                if (req.request == "PURGE") {
                        set obj.ttl = 0s;
                        error 404 "Not in cache";
                }
                if (!obj.cacheable) {
                        return (pass);
                }
                return (deliver);
        }
        #vcl_miss函数用于在执行lookup后但没有找到缓存内容时调用,可以用于判断是否需要从后端服务器取内容。
        sub vcl_miss {
                return (fetch);        #从后端取得请求的内容,把控制权交给vcl_fetch。
        }
        #vcl_fetch函数在后端主机更新缓存并且获取内容后调用该方法,接着,通过判断获取的内容来决定将内容放入缓存,还是直接返回给客户端。
        sub vcl_fetch {
                if (!beresp.cacheable) {
                        return (pass);
                }
                if (beresp.http.Set-Cookie) {
                        return (pass);
                }
                #当url中包含servlet时,不进行缓存。
                if (req.url ~ "^/servlet/") {
                        return (pass);
                }
                #如果请求类型是GET,并且请求的URL中包含upload,那么就进行缓存,缓存的时间是300秒,即5分钟。
                if (req.request == "GET" && req.url ~ "^/upload(.*)$") {
                        set beresp.ttl = 300s;
                }
                #下面定义不缓存含有哪些HTTP头的请求。
                if (beresp.http.Pragma ~ "no-cache" || beresp.http.Cache-Control ~ "no-cache" ||
                        beresp.http.Cache-Control ~ "private") {
                        return (pass);
                }
                #当请求类型是GET,并且请求的URL是以下内容结尾时,进行缓存,缓存时间为分别不同。
                if (req.request == "GET" && req.url ~ "\.(css|js|html|htm|txt)$") {
                        set beresp.ttl = 300s;
                }
                if (req.request == "GET" && req.url ~ "\.(pdf|xls|xsl|vsd|doc|xml|ppt|vsd|docx|pps|gif|jpg|jpeg|bmp|png|tiff|tif|ico|img|bmp|wmf)$")
                {
                        set beresp.ttl = 3600s;
                }
                if (req.request == "GET" && req.url ~ "\.(sxw|zip|gz|bz2|tgz|tar|chm|rar|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|dmg|torrent|deb|msi|iso|rpm|tiff|tif|svg|swf|ico|mp3|mp4|m4a|wav|rmvb|avi|wmv|ogg|mov|wmv)$")
                {
                        set beresp.ttl = 10d;
                }
                return (deliver);
        }
        #vcl_deliver函数将在缓存中找到请求的内容发送给客户端前调用此方法。
        sub vcl_deliver {
                if (obj.hits > 0) {
                        set resp.http.X-Cache = "HIT from www.wameide.com";  #下面添加一个Header标识,以判断缓存是否命中。
                } else {
                        set resp.http.X-Cache = "MISS from www.wameide.com";
                }
                return (deliver);
        }
        #vcl_discard函数用于在缓存内容到期后或缓存空间不够时,自动调用该函数。
        sub vcl_discard {
                return (discard);
        }
        #vcl_timeout函数用于在缓存内容到期前调用。
        sub vcl_timeout {
                return (discard);
        }
        sub vcl_error {
                set obj.http.Content-Type = "text/html; charset=utf-8";
                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>
                                        <address><a href="http://www.wameide.com/">wameide</a></address>
                                </body>
                        </html>
                "};
                return (deliver);
        }
0 0
原创粉丝点击