负载均衡器后面的 Varnish 不缓存内容

发布于 2024-11-29 20:39:46 字数 588 浏览 3 评论 0原文

我正在使用此行删除 default.vcl 中的 cookie 以允许 Varnish 缓存,

set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|__utma_a2a|has_js|Drupal.toolbar.collapsed|MNO_abc_qrst_\d+)=[^;]*", "");

但它似乎不起作用。

Varnish 位于负载均衡器后面,负载均衡器设置一个持久性 cookie,该 cookie 以这种格式出现在标头中:

Set-Cookie:MNO_abc_qrst_80=aaaaaaaaa2a5882045525d5a4a58455e445a4a423660;expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly

我在模拟器中运行了这个正则表达式,它捕获了上面的行(直到第一个“;”)。所以我认为这个正则表达式应该捕获它,但它似乎没有捕获它?当我绕过此负载均衡器时,内容会被缓存。

有什么想法吗?我缺少什么?

I am using this line to remove cookies in default.vcl to allow for Varnish caching

set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|__utma_a2a|has_js|Drupal.toolbar.collapsed|MNO_abc_qrst_\d+)=[^;]*", "");

but it does not appear to work.

Varnish is behind a load balancer, the load balancer sets a persistence cookie that appears in this format in the header:

Set-Cookie:MNO_abc_qrst_80=aaaaaaaaa2a5882045525d5a4a58455e445a4a423660;expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly

I ran this regex in an emulator and it catches the above line (until the first ";"). So I'd think this regex should catch it, but it doesn't appear to? When I bypass this load balancer, content gets cached.

Any ideas? What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离旧人 2024-12-06 20:39:46

有两种方法可以选择,要么验证需要删除整行,
或剪掉该线的有问题的部分。我不知道你使用的是哪种正则表达式引擎。为了获得更好的结果,至少需要进行负面的回顾和反向参考。

压缩验证:
/^(?=(Set-Cookie:\s*))(?:\1|.*?;\s*)(?:__[az]+|__utma_a2a|has_js|Drupal\.toolbar\ .collapsed|MNO_abc_qrst_\d+)=[^;]*(?:;|$).*$/s

压缩的全局部分替换:
s/(?:(?<=^Set-Cookie:)|(?<=;))\s*(?:__[az]+|__utma_a2a|has_js|Drupal\.toolbar\. Collapsed|MNO_abc_qrst_\d+)=[^;]*(?:;|$)//g

正则表达式的扩展视图(在 Perl 中):

my $str = 'Set-Cookie: MNO_abc_qrst_80=aaaaaaaaa2a5882045525d5a4a58455e445a4a423660;expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly';

## Validate part of a cookie, remove line

if ( $str =~ /
       ^ (?= (Set-Cookie: \s*))
       (?:  \1
          | .*? ; \s*
       )
       (?:   __[a-z]+
          |  __utma_a2a
          |  has_js
          |  Drupal\.toolbar\.collapsed
          |  MNO_abc_qrst_\d+
       )
       =
       [^;]*
       (?: ; | $)
       .* $
 /sx )
{
     print "Valid, remove line '
amp;'\n=============\n\n";
}

## Globally, replace many parts of the cookie

if ( $str =~ s/
       (?:   (?<= ^ Set-Cookie:)
          |  (?<= ;)
       )
       \s* 
       (?:    __[a-z]+
           |  __utma_a2a
           |  has_js
           |  Drupal\.toolbar\.collapsed
           |  MNO_abc_qrst_\d+
       )
       =
       [^;]*
       (?: ; | $)
 //xg )
{
     print "removed parts of cookie\n";
     print "new string = '$str'\n";
}

输出:

Valid, remove line 'Set-Cookie: MNO_abc_qrst_80=aaaaaaaaa2a5882045525d5a4a58455e
445a4a423660;expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly'
=============

removed parts of cookie
new string = 'Set-Cookie:expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly'

There are 2 ways to go, either validate the entire line needs to be removed,
or cut out the offending parts of the line. I don't know what kind of regex engine you are using. At least negative look behinds and back references are necessary for a better result.

Compressed validation:
/^(?=(Set-Cookie:\s*))(?:\1|.*?;\s*)(?:__[a-z]+|__utma_a2a|has_js|Drupal\.toolbar\.collapsed|MNO_abc_qrst_\d+)=[^;]*(?:;|$).*$/s

Compressed global part substitution:
s/(?:(?<=^Set-Cookie:)|(?<=;))\s*(?:__[a-z]+|__utma_a2a|has_js|Drupal\.toolbar\.collapsed|MNO_abc_qrst_\d+)=[^;]*(?:;|$)//g

An expanded look at the regex's (in Perl):

my $str = 'Set-Cookie: MNO_abc_qrst_80=aaaaaaaaa2a5882045525d5a4a58455e445a4a423660;expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly';

## Validate part of a cookie, remove line

if ( $str =~ /
       ^ (?= (Set-Cookie: \s*))
       (?:  \1
          | .*? ; \s*
       )
       (?:   __[a-z]+
          |  __utma_a2a
          |  has_js
          |  Drupal\.toolbar\.collapsed
          |  MNO_abc_qrst_\d+
       )
       =
       [^;]*
       (?: ; | $)
       .* $
 /sx )
{
     print "Valid, remove line '
amp;'\n=============\n\n";
}

## Globally, replace many parts of the cookie

if ( $str =~ s/
       (?:   (?<= ^ Set-Cookie:)
          |  (?<= ;)
       )
       \s* 
       (?:    __[a-z]+
           |  __utma_a2a
           |  has_js
           |  Drupal\.toolbar\.collapsed
           |  MNO_abc_qrst_\d+
       )
       =
       [^;]*
       (?: ; | $)
 //xg )
{
     print "removed parts of cookie\n";
     print "new string = '$str'\n";
}

Output:

Valid, remove line 'Set-Cookie: MNO_abc_qrst_80=aaaaaaaaa2a5882045525d5a4a58455e
445a4a423660;expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly'
=============

removed parts of cookie
new string = 'Set-Cookie:expires=Fri, 12-Aug-2011 17:23:23 GMT;path=/;httponly'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文