【问题标题】:How can I selectively strip/allow cookies with Varnish?如何使用 Varnish 选择性地去除/允许 cookie?
【发布时间】:2013-06-20 20:43:17
【问题描述】:

我设置了 Varnish 来删除所有 cookie:

sub vcl_fetch {
    unset beresp.http.Set-Cookie;
    #etc
}

但是,我想设置一个名为 first_visit 的 cookie,我不希望 Varnish 将其剥离。

我该怎么做?

【问题讨论】:

    标签: http cookies varnish


    【解决方案1】:

    您也可以在纯 VCL 中去除 cookie:

    sub vcl_fetch {
      # ...
      if ( beresp.http.Set-Cookie 
        && beresp.http.Set-Cookie == "first_visit=Y; path=/; domain=mydomain.tld" 
      ) {
        set beresp.http.first-visit = beresp.http.Set-Cookie;
        unset beresp.http.Set-Cookie;
      }
      # ...
    }
    
    sub vcl_deliver {
      # ...
      if (resp.http.first-visit) {
        set resp.http.Set-Cookie = resp.http.first-visit;
        unset resp.http.first-visit;
      }
      # ...
    }
    

    【讨论】:

    • 如果我在 Set-Cookie 标头中传输了其他 cookie 怎么办?
    • 这是一种保守的方法,我认为会保留它们,但您可以安全地将它们强制放到 vcl_deliver 中的固定字符串:set resp.http.Set-Cookie = "first_visit=Y; path=/; domain=mydomain.tld";
    【解决方案2】:

    您可以查看Header vmod,它允许使用 Set-Cookie 进行操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-09
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多