Varnish behind load balancer not caching content
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|MN开发者_如何学运维O_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?
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 '$&'\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'
精彩评论