[Varnish] #536: Suggested regsub example for Plone VirtualHostBase replacement

Varnish varnish-bugs at varnish-cache.org
Mon Jan 10 09:49:05 CET 2011


#536: Suggested regsub example for Plone VirtualHostBase replacement
---------------------------+------------------------------------------------
 Reporter:  ned14          |       Owner:  kristian           
     Type:  documentation  |      Status:  assigned           
 Priority:  lowest         |   Milestone:  Varnish 2.1 release
Component:  documentation  |     Version:  trunk              
 Severity:  trivial        |    Keywords:                     
---------------------------+------------------------------------------------
Changes (by kristian):

  * owner:  => kristian
  * status:  new => assigned


Old description:

> 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

New description:

 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

--

Comment:

 If this is mentioned on our wiki, you should be able to edit it yourself
 (pending edit-bits). If it's mentioned elsewhere, I'd appreciate it if you
 could supply some links and I'll update the examples.

-- 
Ticket URL: <http://www.varnish-cache.org/trac/ticket/536#comment:5>
Varnish <http://varnish-cache.org/>
The Varnish HTTP Accelerator




More information about the varnish-bugs mailing list