How to select a backend from a vmod?

J David j.david.lists at
Mon Dec 10 02:47:32 CET 2012

We currently use Squid as a reverse proxy.  We have a moderately
elaborate url_rewrite_program that maps a large set of frontend
hostnames to a medium-size set of backend hosts that are configured to
handle them.  The frontend hostnames are dynamically created by site
visitors, and the front-to-back mapping changes due to load balancing.
 Changes to both are frequent so there is no way to do this statically
or manually.

The Squid solution is good and works well, but we would like to switch
to Varnish to get better scalability (having 8-core machines sit 87.5%
idle is kinda sad) and good dual-stack IPv4/IPv6 support.

However, I am having trouble figuring out how to adapt our URL
rewriter.  I was able to do a proof-of-concept VCL that statically
does this in vcl_miss:

sub vcl_miss {
        if ( == "") {
                set req.backend = back_1;
        } elsif ( == "") {
                set req.backend = back_2;
        } elsif ( == "") {
                set req.backend = back_3;
        } else {
                set req.backend = default;

This seems to work, so what I'd like to do is code up a vmod that
allows this to be done dynamically.  I.e.

import magic;

sub vcl_miss {
        set req.backend =;
        #  Or: set =;

I wrote a rudimentary "magic" vmod from which returns a
string, but that's unacceptable because VCL wants a backend object
there, not a string.  And even this approach requires all the backends
to be predefined, which seems like a hassle as we add more to handle

Is there an efficient way to approach this?

Thanks in advance for any help or suggestions!

More information about the varnish-misc mailing list