Cookies 在Varnish中,如何读取Set Cookie响应头?

Cookies 在Varnish中,如何读取Set Cookie响应头?,cookies,http-headers,varnish,varnish-vcl,Cookies,Http Headers,Varnish,Varnish Vcl,我试图检测我的应用程序是否设置了一个cookie,该cookie在下一个页面上为用户保存一条“警报消息”,如果检测到,Javascript将在该页面上显示该消息 在我的vcl_fetch()中,我需要检测特定的cookie值“alert_message”是否出现在Set cookie头中的任何位置(可能出现在vcl变量beresp.http.Set cookie中)。如果检测到,则我不想缓存下一页(因为Varnish在默认情况下会删除Set Cookie标头,这会在警报消息返回浏览器之前将其删除

我试图检测我的应用程序是否设置了一个cookie,该cookie在下一个页面上为用户保存一条“警报消息”,如果检测到,Javascript将在该页面上显示该消息

在我的vcl_fetch()中,我需要检测特定的cookie值“alert_message”是否出现在Set cookie头中的任何位置(可能出现在vcl变量beresp.http.Set cookie中)。如果检测到,则我不想缓存下一页(因为Varnish在默认情况下会删除Set Cookie标头,这会在警报消息返回浏览器之前将其删除)

下面是我的简单测试:

if(beresp.http.Set-Cookie ~ "alert_message") {
    set req.http.no_cache = 1;
}
奇怪的是,它的评估结果并不是真的

因此,我将该变量放入服务器头以查看它的外观:

set beresp.http.Server = " MyApp Varnish implementation - test reading set-cookie: "+beresp.http.Set-Cookie;
但是由于某些原因,这只显示响应头中的第一个Set Cookie行

以下是相关的响应标题:

Server: MyApp Varnish implementation - test reading cookie: elstats_session=7d7279kjmsnkel31lre3s0vu24; expires=Wed, 10-Oct-2012 00:03:32 GMT; path=/; HttpOnly
Set-Cookie:app_session=7d7279kjmsnkel31lre3s0vu24; expires=Wed, 10-Oct-2012 00:03:32 GMT; path=/; HttpOnly
Set-Cookie:alert_message=Too+many+results.; expires=Tue, 09-Oct-2012 20:13:32 GMT; path=/; domain=.my.app.com
Set-Cookie:alert_key=flash_error; expires=Tue, 09-Oct-2012 20:13:32 GMT; path=/; domain=.my.app.com
Vary:Accept-Encoding

如何读取并在所有设置的Cookie头行上运行字符串检测?

您可以使用header.get函数从(Varnish version>=3)解决该问题

例如,我有一个简单的PHP脚本和多个集Cookie:

 <?php
 setcookie ("Foo", "test", time() + 3600);
 setcookie ("Bar", "test", time() + 3600);
 setcookie ("TestCookie", "test", time() + 3600);
 ?>
因此,我得到了第一个和下一个请求的响应头:

cookie-test:1
Set-Cookie:Foo=test; expires=Tue, 09-Oct-2012 22:33:37 GMT
Set-Cookie:Bar=test; expires=Tue, 09-Oct-2012 22:33:37 GMT
Set-Cookie:TestCookie=test; expires=Tue, 09-Oct-2012 22:33:37 GMT
X-Cache:MISS

如果我为cookie TestCookie注释掉setcookie,那么我将得到下一个请求的点击。

您必须在resp中设置http.Server,而不是在beresp中(在vcl_deliver中)
cookie-test:1
Set-Cookie:Foo=test; expires=Tue, 09-Oct-2012 22:33:37 GMT
Set-Cookie:Bar=test; expires=Tue, 09-Oct-2012 22:33:37 GMT
Set-Cookie:TestCookie=test; expires=Tue, 09-Oct-2012 22:33:37 GMT
X-Cache:MISS