2014-02-28 35 views
0

我想启用ESI只为登录的用户。登出的用户应该通过缓存服务整个页面。清漆 - 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); 
} 

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

回答

0
  1. 首先,你必须检测Cookie,并得到其数值
  2. 然后,你必须通过在vcl_recv &请求vcl_fetch.When登录的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; 
        } 
    }