【问题标题】:Varnish always cache when certain cookie is present当某些 cookie 存在时,varnish 总是缓存
【发布时间】:2014-09-04 18:36:09
【问题描述】:

我想要在我的 vcl_recv 中检查是否存在某个 cookie 的规则,如果存在,则始终提供一个缓存文件(让我们将此 cookie 称为 VARNISH_USE_CACHE)

但是,我还有一条规则在此之后运行以检查会话 cookie(如果存在则始终传递给 apache),如果 VARNISH_USE_CACHE 不存在,则该规则仍应运行。

我不太确定如何检查一个 cookie,然后检查另一个。我试过将cookies分配给一个临时变量,并在那里检查。但我一定不明白 vcl 是如何工作的,因为当我这样做时,我最终得到了完整的 cookie,没有任何东西被剥离。

这是我在 vcl_rec 中的代码:

set req.http.tempCookie = ";" + req.http.Cookie;
set req.http.tempCookie = regsuball(req.http.tempCookie, ";(VARNISH_USE_CACHE)=", "; \1=");
set req.http.tempCookie = regsuball(req.http.tempCookie, ";[^ ][^;]*", "");
set req.http.tempCookie = regsuball(req.http.tempCookie, "^[; ]+|[; ]+$", "");

因此,如果没有 VARNISH_USE_CACHE,我希望 req.http.tempCookie 为空,相反,它始终包含来自 req.http.Cookie 的所有 cookie。

【问题讨论】:

    标签: varnish varnish-vcl


    【解决方案1】:

    啊解决了:

    忘记了这一行

    set req.http.tempCookie = regsuball(req.http.tempCookie, "; +", ";");
    

    所以代码看起来像:

    set req.http.tempCookie = ";" + req.http.Cookie;
    set req.http.tempCookie = regsuball(req.http.tempCookie, "; +", ";");
    set req.http.tempCookie = regsuball(req.http.tempCookie, ";(VARNISH_USE_CACHE)=", "; \1=");
    set req.http.tempCookie = regsuball(req.http.tempCookie, ";[^ ][^;]*", "");
    set req.http.tempCookie = regsuball(req.http.tempCookie, "^[; ]+|[; ]+$", "");
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      相关资源
      最近更新 更多