【问题标题】:How do I get set cookie value when there are multiple set cookie request in varnish?当清漆中有多个设置 cookie 请求时,如何获取设置的 cookie 值?
【发布时间】:2021-05-22 05:13:36
【问题描述】:

如果我的 cookie 具有一定的价值,我会尝试取消设置 cookie。 我尝试使用beresp.http.Set-Cookie 获取值,但这似乎在响应时在 Set-Cookie 中设置了第一个值。有什么办法可以提取第二个 Set-Cookie 的值吗? 编辑

这是我的响应标头

HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Fri, 19 Feb 2021 09:47:53 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12297
Connection: keep-alive
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Encoding: br
Set-Cookie: VarnishCustomerIsGuest=True; path=/
Set-Cookie: .Nop.Customer=62a224b9-3b5b-4e74-8200-cf341df111af; expires=Sat, 19 Feb 2022 09:47:53 GMT; path=/; httponly
Set-Cookie: .Nop.TempData=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; samesite=lax; httponly
Vary: Accept-Encoding
X-MiniProfiler-Ids: ["4f9a095d-bbd1-4ffe-81e1-31761363af25"]
X-TMP1000: VarnishCustomerIsGuest=True; path=/
X-Varnish: 163854
Age: 0
Via: 1.1 varnish (Varnish/5.2)
X-Cache: MISS
Accept-Ranges: bytes

我要做的是在 vcl_backend_response 获取客户 cookie。但是

beresp.http.Set-Cookie

给了我 VarnishCustomerIsGuest 的第一个 set-cookie 值,我不确定是否可以获得客户 cookie .Nop.Customer。

我的应用程序后端总是为每个请求设置 cookie,所以我要做的是根据 .Nop.Customer 值取消设置 cookie,这样对于某些特定客户,页面总是从清漆缓存中提供。

【问题讨论】:

  • 请添加相关的 VCL 代码,您可以在其中从 beresp.http.Set-Cookie 中提取值。这将帮助我了解您的问题,并帮助我找到解决方案。
  • 感谢您的回复。我已经编辑了这个问题,并试图更具描述性。

标签: varnish varnish-vcl


【解决方案1】:

Varnish 的 vmod_std 具有 std.collect() 函数,可将标头折叠成单个标头。

在您的情况下,可以使用以下 VCL sn-p 将所有出现的 Set-Cookie 标头折叠成单个 Set-Cookie

vcl 4.0;

import std;

sub vcl_backend_response {
    std.collect(beresp.http.Set-Cookie);    
}

当然,您现有的 VCL 逻辑也应该是 VCL 文件的一部分。这个例子只是说明需要导入 VMOD 并在 vcl_backend_response 中调用 std.collect()

直接来自std.collect() documentation page的重要免责声明:

折叠标题时应小心。特别是折叠 Set-Cookie 会导致浏览器端出现意想不到的结果。

Set-Cookie 标头折叠后,您可以使用regsub() 逻辑提取必要的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2012-12-18
    相关资源
    最近更新 更多