清漆缓存:如果会话处于活动状态,则重写ESI包括url

时间:2014-08-26 21:14:22

标签: varnish varnish-vcl

我希望在Varnish中重写一个ESI include url。

在我的模板中,我有:

<esi:include src="/esi/user.html" />

仅包含静态内容,即“#34; Welcome Guest&#34;。

如果他们登录我将会话添加到我的.vcl。

我想要做的是将include重写为:

<esi:include src="/esi/user.active.html" /> 

我将执行db查询。

目前,在我的子vcl_recv中,我有:

if (req.http.Cookie) {
        if (req.url ~ "^/esi/(.*)\.html") { 
        set req.url = regsub("^/esi/(.*)\.html", "$0", ".active"); 
        }
}

它会在前端导致503错误。我如何更新这个以重写URL并使其有效?

1 个答案:

答案 0 :(得分:0)

好的,搞定了。如果有人有兴趣,这是我的.VCL文件

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

sub vcl_recv {

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

# Remove cookie for static files
if (req.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|woff|ttf|eot|svg)(\?[a-zA-Z0-9\=\.\-]+)?$") {
    remove req.http.cookie;
}

# Quick hack for now to clear the cache on post
if (req.request == "POST") {
    ban("req.http.host == "+ req.http.Host);
    return(pass);
}

# If CraftSessionId isn't active, remove cookies altogether
if (req.http.Cookie) {
    set req.http.Cookie = ";" + req.http.Cookie;
    set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
    set req.http.Cookie = regsuball(req.http.Cookie, ";(CraftSessionId)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

    if (req.http.Cookie == "") {
        remove req.http.Cookie;
    }
}


# If user is logged in/has cookies enabled, rewrite esi files to include *.active.html
if (req.http.cookie ~ "CraftSessionId=") {
    if (req.url ~ "^/esi") {
        set req.url = regsub(req.url, "^/esi/(.*).html", "/esi/\1-active.html");
    }
}

# Grace period for falldown
set req.grace = 1h;
}

sub vcl_fetch {

set beresp.ttl = 24h; # Make cache last 24 hours

#  Allow cookies in admin and account areas
    if (req.url !~ "(admin/login|account/login|account/register)") {
        unset beresp.http.set-cookie;
    }

set beresp.do_gzip = true;
set beresp.do_esi = true; # Allow ESI

# Grace period for falldown
set beresp.grace = 1h;

}