[Varnish] #536: Suggested regsub example for Plone VirtualHostBase replacement
Varnish
varnish-bugs at projects.linpro.no
Sun Aug 2 01:07:42 CEST 2009
#536: Suggested regsub example for Plone VirtualHostBase replacement
---------------------------------+------------------------------------------
Reporter: ned14 | Type: documentation
Status: new | Priority: lowest
Milestone: Varnish 2.1 release | Component: documentation
Version: trunk | Severity: trivial
Keywords: |
---------------------------------+------------------------------------------
Most of the varnish documentation on the web (including this wiki)
suggests something like this for mangling up Zope VirtualHostBase URLs:
{{{
if (req.http.host ~ "^(www.)?example.com") {
set req.http.host = "example.com";
set req.url = regsub(req.url, "^",
"/VirtualHostBase/http/example.com:80/Sites/example.com/VirtualHostRoot");
} elsif (req.http.host ~ "^(www.)?example.org") {
set req.http.host = "example.org";
set req.url = regsub(req.url, "^",
"/VirtualHostBase/http/example.org:80/Sites/example.org/VirtualHostRoot");
} else {
error 404 "Unknown virtual host";
}
}}}
The big problem with this is that every single virtual host must be
labouriously specified, and then doubly specified if you support HTTPS.
This rapidly becomes a royal PITA.
A far better solution is to use varnish's regsub support and generate it
entirely dynamically:
{{{
if (req.http.X-Forwarded-Proto == "https" ) {
set req.http.X-Forwarded-Port = "443";
} else {
set req.http.X-Forwarded-Port = "80";
}
if (req.http.host ~ "^(www\.|ipv6\.)?(.+)\.(.+)?$") {
set req.http.host = regsub(req.http.host,
"^(www\.|ipv6\.)?(.+)\.(.+)?$", "www.\2.\3");
set req.url = "/VirtualHostBase/" req.http.X-Forwarded-
Proto
regsub(req.http.host,
"^(www\.|ipv6\.)?(.+)\.(.+)$", "/www.\2.\3:")
req.http.X-Forwarded-Port
regsub(req.http.host,
"^(www\.|ipv6\.)?(.+)\.(.+)$", "/\2.\3/\2.\3/VirtualHostR$
req.url;
}
}}}
It also does a great job of demonstrating string concantation in varnish
as well as a few other useful tricks.
I have an optional ipv6. subdomain in there, but note that it doesn't
include the ipv6. redirect into Zope so all the links in the Plone site
will still point to www. instead. This doesn't bother me as I only use the
ipv6. subdomain to test my site's IPv6 connectivity.
I hope that you find this useful. I'd suggest sticking the above anywhere
where a search for VirtualHostBase on this site returns including the
example Plone .vcl file.
Cheers,[[BR]]
Niall Douglas
--
Ticket URL: <http://varnish.projects.linpro.no/ticket/536>
Varnish <http://varnish.projects.linpro.no/>
The Varnish HTTP Accelerator
More information about the varnish-bugs
mailing list