V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wico77
V2EX  ›  站长

varnish 缓存规则 求助

  •  
  •   wico77 · 2015-02-26 20:28:11 +08:00 · 2064 次点击
    这是一个创建于 3559 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请问我的varnish能不能全站缓存?

    This is a basic VCL configuration file for varnish. See the vcl(7)

    man page for details on VCL syntax and semantics.

    Default backend definition. Set this to point to your content

    server.

    backend default {
    .host = "127.0.0.1";
    .port = "8080";
    }
    acl purge {
    "localhost";
    "127.0.0.1";
    }

    Below is a commented-out copy of the default VCL logic. If you

    redefine any of these subroutines, the built-in logic will be

    appended to your code.

    sub vcl_recv {
    # Only cache the following site
    if (req.http.host ~ "(mydomain.com)") {
    set req.backend = default;
    } else {
    return (pass);
    }
    if (req.request == "PURGE") {
    if (!client.ip ~ purge) {
    error 405 "Not allowed.";
    }
    return (lookup);
    }

    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;
    }
     }
     if (req.request != "GET" &&
       req.request != "HEAD" &&
       req.request != "PUT" &&
       req.request != "POST" &&
       req.request != "TRACE" &&
       req.request != "OPTIONS" &&
       req.request != "DELETE") {
         /* Non-RFC2616 or CONNECT which is weird. */
         return (pipe);
     }
     if (req.request != "GET" && req.request != "HEAD") {
         /* We only deal with GET and HEAD by default */
         return (pass);
     }
    
    
     return (lookup);
    

    }

    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.
    return (pipe);
    }

    sub vcl_pass {
    return (pass);
    }

    sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
    hash_data(req.http.host);
    } else {
    hash_data(server.ip);
    }
    return (hash);
    }

    sub vcl_hit {
    if (req.request == "PURGE") {
    purge;
    error 200 "Purged.";
    }
    return (deliver);
    }

    sub vcl_miss {
    if (req.request == "PURGE") {
    purge;
    error 200 "Purged.";
    }
    return (fetch);
    }

    sub vcl_fetch {

    if (beresp.ttl <= 0s ||
         beresp.http.Set-Cookie ||
         beresp.http.Vary == "*") {
        /*
         * Mark as "Hit-For-Pass" for the next 2 minutes
         */
        set beresp.ttl = 120 s;
        return (hit_for_pass);
     }
    

    set beresp.ttl = 7d;
    return (deliver);
    }

    sub vcl_deliver {
    return (deliver);
    }

    sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    set obj.http.Retry-After = "5";
    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>
    <hr>
    <p>Varnish cache server</p>
    </body>
    </html>
    "};
    return (deliver);
    }

    sub vcl_init {
    return (ok);
    }

    sub vcl_fini {
    return (ok);
    }

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3586 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 04:58 · PVG 12:58 · LAX 20:58 · JFK 23:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.