清漆:缓存 Set-Cookie header

Varnish: Cache Set-Cookie header

我想为某些特定的 cookie 缓存 set-cookie header。我要缓存的 cookie 对所有用户都是相同的,并且特定于页面。 Varnish 可以吗?

如果所有用户的 cookie 都相同,您可以重写 the built-in VCL behavior for Set-Cookie headers in the vcl_backend_response 子例程,如下所示:

sub vcl_backend_response {
    if(bereq.url ~ "^/your-page" && beresp.http.Set-Cookie ~ "^yourCookieName=") {
        return(deliver);
    }
}

我添加了 yourCookieName 检查,您应该将其替换为实际 cookie 的名称,以确保不会缓存每个 Set-Cookie header。

我还添加了额外的检查以确保匹配正确的 URL 模式。请用实际的 URL 模式替换 ^/your-page 正则表达式模式,或者如果您只想匹配单个页面,请执行精确的字符串匹配。