文档章节

Varnish 4.0 配置模板

j
 jims
发布于 2015/11/09 09:07
字数 1358
阅读 84
收藏 1

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>





varnish 发布了4.0的版本,很多配置的语法发生了变化,下面是我在github上看到的一个模板,可以作参考使用,进行适当的修改就能用了。


# This is a Varnish 4.x VCL file
vcl 4.0;


backend default {
    .host = "127.0.0.1";
    .port = "81";
    .probe = {
        .url = "/ping";
        .timeout   = 1s;
        .interval  = 10s;
        .window    = 5;
        .threshold = 2;
    }
    .first_byte_timeout     = 300s;   # How long to wait before we receive a first byte from our backend?
    .connect_timeout        = 5s;     # How long to wait for a backend connection?
    .between_bytes_timeout  = 2s;     # How long to wait between bytes received from our backend?
}


backend web1 {
    .host = "127.0.0.1";
    .port = "81";
}


backend web2 {
    .host = "127.0.0.1";
    .port = "81";
}


# Below is an example redirector based on round-robin requests
import directors;
sub vcl_init {
    new cluster1 = directors.round_robin();
    cluster1.add_backend(web1);    # Backend web1 defined above
    cluster1.add_backend(web2);    # Backend web2 defined above
}


# Below is an example redirector based on the client IP (sticky sessions)
#sub vcl_init {
#    new cluster2 = directors.hash();
#    cluster2.add_backend(web1);    # Backend web1 defined above
#    cluster2.add_backend(web2);    # Backend web2 defined above
#}


acl purge {
    # For now, I'll only allow purges coming from localhost
    "127.0.0.1";
    "localhost";
}


# Handle the HTTP request received by the client
sub vcl_recv {
    # Choose the round-robin backend
    set req.backend_hint = cluster1.backend();


    # Or chose the client-IP backend (sticky sessions)
    #set req.backend_hint = cluster2.backend();


    # shortcut for DFind requests
    if (req.url ~ "^/w00tw00t") {
        return (synth(404, "Not Found"));
    }


    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;
        }
    }


    # Normalize the header, remove the port (in case you're testing this on various TCP ports)
    set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");


    # Allow purging
    if (req.method == "PURGE") {
        if (!client.ip ~ purge) {
            # Not from an allowed IP? Then die with an error.
            return (synth(405, "This IP is not allowed to send PURGE requests."));
        }


        # If you got this stage (and didn't error out above), purge the cached result
        return (purge);
    }


    # Only deal with "normal" types
    if (req.method != "GET" &&
            req.method != "HEAD" &&
            req.method != "PUT" &&
            req.method != "POST" &&
            req.method != "TRACE" &&
            req.method != "OPTIONS" &&
            req.method != "PATCH" &&
            req.method != "DELETE") {
        /* Non-RFC2616 or CONNECT which is weird. */
        return (pipe);
    }


    # Only cache GET or HEAD requests. This makes sure the POST requests are always passed.
    if (req.method != "GET" && req.method != "HEAD") {
        return (pass);
    }


    # Configure grace period, in case the backend goes down. This allows otherwise "outdated"
    # cache entries to still be served to the user, because the backend is unavailable to refresh them.
    # This may not be desireable for you, but showing a Varnish Guru Meditation error probably isn't either.
    #set req.grace = 15s;
    #if (std.healthy(req.backend)) {
    #    set req.grace = 30s;
    #} else {
    #    unset req.http.Cookie;
    #    set req.grace = 6h;
    #}


    # Some generic URL manipulation, useful for all templates that follow
    # First remove the Google Analytics added parameters, useless for our backend
    if (req.url ~ "(\?|&)(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=") {
        set req.url = regsuball(req.url, "&(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "");
        set req.url = regsuball(req.url, "\?(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "?");
        set req.url = regsub(req.url, "\?&", "?");
        set req.url = regsub(req.url, "\?$", "");
    }


    # Strip hash, server doesn't need it.
    if (req.url ~ "\#") {
        set req.url = regsub(req.url, "\#.*$", "");
    }


    # Strip a trailing ? if it exists
    if (req.url ~ "\?$") {
        set req.url = regsub(req.url, "\?$", "");
    }


    # Some generic cookie manipulation, useful for all templates that follow
    # Remove the "has_js" cookie
    set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");


    # Remove any Google Analytics based cookies
    set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "_ga=[^;]+(; )?", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "utmctr=[^;]+(; )?", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "utmcmd.=[^;]+(; )?", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "utmccn.=[^;]+(; )?", "");


    # Remove the Quant Capital cookies (added by some plugin, all __qca)
    set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");


    # Remove the AddThis cookies
    set req.http.Cookie = regsuball(req.http.Cookie, "__atuvc=[^;]+(; )?", "");


    # Remove a ";" prefix in the cookie if present
    set req.http.Cookie = regsuball(req.http.Cookie, "^;\s*", "");


    # Are there cookies left with only spaces or that are empty?
    if (req.http.cookie ~ "^\s*$") {
        unset req.http.cookie;
    }


    # Normalize Accept-Encoding header
    # straight from the manual: https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
    if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
            # No point in compressing these
            unset req.http.Accept-Encoding;
        } elsif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
        } elsif (req.http.Accept-Encoding ~ "deflate") {
            set req.http.Accept-Encoding = "deflate";
        } else {
            # unkown algorithm
            unset req.http.Accept-Encoding;
        }
    }


    # Large static files should be piped, so they are delivered directly to the end-user without
    # waiting for Varnish to fully read the file first.
    # TODO: once the Varnish Streaming branch merges with the master branch, use streaming here to avoid locking.
    if (req.url ~ "^[^?]*\.(mp[34]|rar|tar|tgz|gz|wav|zip)(\?.*)?$") {
        unset req.http.Cookie;
        return (pipe);
    }


    # Remove all cookies for static files
    # A valid discussion could be held on this line: do you really need to cache static files that don't cause load? Only if you have memory left.
    # Sure, there's disk I/O, but chances are your OS will already have these files in their buffers (thus memory).
    # Before you blindly enable this, have a read here: http://mattiasgeniar.be/2012/11/28/stop-caching-static-files/
    if (req.url ~ "^[^?]*\.(bmp|bz2|css|doc|eot|flv|gif|gz|ico|jpeg|jpg|js|less|pdf|png|rtf|swf|txt|woff|xml)(\?.*)?$") {
        unset req.http.Cookie;
        return (hash);
    }


    # Send Surrogate-Capability headers to announce ESI support to backend
    set req.http.Surrogate-Capability = "key=ESI/1.0";


    if (req.http.Authorization) {
        # Not cacheable by default
        return (pass);
    }


    return (hash);
}


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.


    #set bereq.http.Connection = "Close";
    return (pipe);
}


sub vcl_pass {
#    return (pass);
}


# The data on which the hashing will take place
sub vcl_hash {
    hash_data(req.url);


    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }


    # hash cookies for requests that have them
    if (req.http.Cookie) {
        hash_data(req.http.Cookie);
    }
}


sub vcl_hit {


    return (deliver);
}


sub vcl_miss {


    return (fetch);
}


# Handle the HTTP request coming from our backend
sub vcl_backend_response {
    # Pause ESI request and remove Surrogate-Control header
    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        unset beresp.http.Surrogate-Control;
        set beresp.do_esi = true;
    }


    # Enable cache for all static files
    # The same argument as the static caches from above: monitor your cache size, if you get data nuked out of it, consider giving up the static file cache.
    # Before you blindly enable this, have a read here: http://mattiasgeniar.be/2012/11/28/stop-caching-static-files/
    if (bereq.url ~ "^[^?]*\.(bmp|bz2|css|doc|eot|flv|gif|gz|ico|jpeg|jpg|js|less|mp[34]|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|woff|xml|zip)(\?.*)?$") {
        unset beresp.http.set-cookie;
    }


    # Sometimes, a 301 or 302 redirect formed via Apache's mod_rewrite can mess with the HTTP port that is being passed along.
    # This often happens with simple rewrite rules in a scenario where Varnish runs on :80 and Apache on :8080 on the same box.
    # A redirect can then often redirect the end-user to a URL on :8080, where it should be :80.
    # This may need finetuning on your setup.
    #
    # To prevent accidental replace, we only filter the 301/302 redirects for now.
    if (beresp.status == 301 || beresp.status == 302) {
        set beresp.http.Location = regsub(beresp.http.Location, ":[0-9]+", "");
    }


    # Set 2min cache if unset for static files
    if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
        return (deliver);
    }


    # Allow stale content, in case the backend goes down.
    set beresp.grace = 6h;


    return (deliver);
}


# The routine when we deliver the HTTP request to the user
# Last chance to modify headers that are sent to the client
sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "cached";
    } else {
        set resp.http.x-Cache = "uncached";
    }


    # Remove some headers: PHP version
    unset resp.http.X-Powered-By;


    # Remove some headers: Apache version & OS
    unset resp.http.Server;
    unset resp.http.X-Drupal-Cache;
    unset resp.http.X-Varnish;
    unset resp.http.Via;
    unset resp.http.Link;


    return (deliver);
}


sub vcl_synth {
    if (resp.status == 720) {
        # We use this special error status 720 to force redirects with 301 (permanent) redirects
        # To use this, call the following from anywhere in vcl_recv: error 720 "http://host/new.html"
        set resp.status = 301;
        set resp.http.Location = resp.reason;
        return (deliver);
    } elseif (resp.status == 721) {
        # And we use error status 721 to force redirects with a 302 (temporary) redirect
        # To use this, call the following from anywhere in vcl_recv: error 720 "http://host/new.html"
        set resp.status = 302;
        set resp.http.Location = resp.reason;
        return (deliver);
    }


    return (deliver);
}


sub vcl_init {
    return (ok);
}


sub vcl_fini {
    return (ok);
}


来源地址:varnish-4.0-configuration-templates


本文转载自:http://loftor.com/archives/varnish-4_0-vcl.html

j
粉丝 4
博文 172
码字总数 30033
作品 0
合肥
私信 提问
varnish 4.0 官方文档翻译1-管理员文档

varnish 管理员文档 varnish缓存是web应用加速器,同时也作为http反向缓存代理被人熟知。你可以安装varnish在任何http的前端,同时配置它缓存内容。varnish真的很快,单个代理的分发速度可以...

wild-life
2015/05/09
273
0
varnish 4.0 官方文档翻译3-简要教程

varnish 4.0简要教程 这部分教程包含了varnish基本原理。囊括了什么是varnish、它怎样工作,同时也包含了开始使用和运行varnish。这部分过后你可能想继续了解用户向导(varnish用户引导)。 如...

wild-life
2015/05/08
713
0
varnish 4.0 官方文档翻译22-Varnish Website Performan

Varnish and Website Performance 本节集中解决如何调优varnish server,同时如何使用varnish优化你的web站点. 一共有三小节.第一小节你应该想到varnish的各种工具和功能,下一小节如何从cac...

wild-life
2015/07/22
257
1
varnish-安装使用-干货

varnish 安装使用 以centos为例安装varnish4 具体可以参考官方链接 https://www.varnish-cache.org/installation/redhat # 安装varnish reporpm --nosignature -i https://repo.varnish-cac......

wild-life
2015/06/05
852
0
Varnish4.0缓存代理配置

一、varnish原理: 1)Varnish简介: varnish缓存是web应用加速器,同时也作为http反向缓存代理。你可以安装varnish在任何http的前端,同时配置它缓存内容。与传统的 squid 相比,varnish 具...

何小帅
2018/06/26
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Redis面试题详解:哨兵+复制+事务+集群+持久化等

Redis主要有哪些功能? 1.哨兵(Sentinel)和复制(Replication) Redis服务器毫无征兆的罢工是个麻烦事,如何保证备份的机器是原始服务器的完整备份呢?这时候就需要哨兵和复制。 Sentinel...

Java阿七
5分钟前
1
0
响应国家号召 1+X 证书 Web 前端开发考试模拟题

官方QQ群:736413913 1+x证书Web前端开发初级理论考试样题2019 http://blog.zh66.club/index.php/archives/149/ 1+x证书Web前端开发初级实操考试样题2019 http://blog.zh66.club/index.php/...

张帅个人博客
9分钟前
3
0
如何检查一个字符串在Objective-C中是否包含另一个字符串?

如何检查字符串( NSString )是否包含另一个较小的字符串? 我希望有这样的东西: NSString *string = @"hello bla bla";NSLog(@"%d",[string containsSubstring:@"hello"]); 但是我能找到......

javail
26分钟前
2
0
.NET Core开发的iNeuOS工业互联网平台,发布 iNeuDA 数据分析展示组件,快捷开发图形报表和数据大屏

目 录 1. 概述... 2 2. 演示信息... 2 3. 简单介绍... 3 4. 产品特点... 4 5. 价值体现... 5 1. 概述 经过一段时间的努力,iNeuDA产品组件已经开发和测试完成,现在正式上线。现在iNeuOS工业...

wxzz
28分钟前
3
0
在每个GROUP BY组中选择第一行?

顾名思义,我想选择以GROUP BY分组的每组行的第一行。 具体来说,如果我有一个如下的purchases表: SELECT * FROM purchases; 我的输出: id | customer | total---+----------+------ 1...

技术盛宴
42分钟前
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部