varnish - vcl规则,仅为登录用户启用esi

时间:2014-02-28 06:46:59

标签: varnish varnish-vcl

我想仅为登录用户启用esi。注销用户应该通过缓存整页提供。

我在下面写了vcl。问题在于,一旦缓存登录页面或登出页面,它就会向所有人显示相同内容。

vcl_recv {
      if (req.http.Cookie ~ "loggedIn") {
                if (req.url ~ "^/esi")
                {
                        return (pass);
                }
                if (req.url ~ "page"){
                        return (lookup);
                }
                return (pipe);
        }
        return (lookup);
}
sub vcl_pipe {
        set bereq.http.connection = "close";
}
sub vcl_fetch {
               if (req.url ~ "page"){
                        set beresp.do_esi = true;
                        set beresp.ttl = 1d;
                        return (deliver);
                }
                set beresp.ttl = 1d;
                return (deliver);
}

一个解决方案似乎是在url和LoggedIn cookie的组合上使用vcl_hash,它有两个值0或1.请建议。

1 个答案:

答案 0 :(得分:0)

  1. 首先,您必须检测cookie并获取其值
  2. 然后你必须在vcl_recv&中传递请求。 vcl_fetch。当登录的cookie存在时。
  3. 您可以通过两种方式检查此项,例如检查Cookie值或检查已登录的Cookie已设置
  4. 我使用下面的cookie值给出答案

    sub identify_cookie{
        #Call cookie based detection method in vcl_recv
        if (req.http.cookie ~ "loggedIn=") {
            set req.http.loggedIn = regsub(req.http.cookie, "(.*?)(loggedIn=)([^;]*)(.*)$", "\3");
        }
    }
    
    sub vcl_recv {
       call identify_cookie; #Used to get identify cookie and get its value
       if(req.http.loggedIn && req.url ~ "^/esi"){
        #used to get value using cookie value
        return (pass);
       }
       if (req.url !~ "wp-(login|admin)" && req.http.Cookie !~ "loggedIn"){
         #remove all the request cookie(s) except loggedIn and check for wordpress admin
         unset req.http.Cookie;
       }
    }
    
    sub vcl_fetch {
        if(req.http.loggedIn && req.url ~ "^/esi") {
           return (hit_for_pass);
        }
        if (req.url !~ "wp-(login|admin)" && beresp.http.set-Cookie !~ "loggedIn"){
             #remove all the response cookie(s) except loggedIn and check for wordpress admin
            unset beresp.http.Set-Cookie;
        }
    }