Varnish round-robin loadbalancing with Virtualhost (Namebased)

MAGNIEN, Thierry thierry.magnien at sfr.com
Fri Aug 30 14:46:22 CEST 2013


Hi,

I think I understood the point :
- you have 2 backends hosted under the same IP address : www.backend1.com and www.backend2.com
- your varnish server gets requests (for example) for www.frontend.com
- you want requests to be load-balanced between your 2 backends BUT this would need the bereq.host to be rewritten to either www.backend1.com or www.backend2.com 
- BUT you can't do this in your VCL because you don't know which backend will be selected.

In fact, this would need the director to rewrite the host header once the real backend has been selected, which I don't think is possible.

Anyway, I still wonder why the hell you need varnish for this, where a HAProxy would certainly do the trick ;-)

Regards,
Thierry


De : varnish-misc-bounces+thierry.magnien=sfr.com at varnish-cache.org [mailto:varnish-misc-bounces+thierry.magnien=sfr.com at varnish-cache.org] De la part de Per Buer
Envoyé : vendredi 30 août 2013 13:36
À : Tharanga Abeyseela
Cc : Varnish misc
Objet : Re: Varnish round-robin loadbalancing with Virtualhost (Namebased)

Hi Tharanga,

You seem not to understand. Varnish will just pass the host header in the request to the backend you specify. Looking at the VCL you provided you put both the backends into one director. This is probably not what you want to do.

Try removing the director. Name the directors xxx and yyy and have the VCL look like.

sub vcl_recv {
    if (req.http.host ~ "xxx.com") {
        req.backend = xxx;
    } else if (..

}
Also the next bit doesn't make sense:
if (req.request)
    {
    return(pass);
    }

}

On Fri, Aug 30, 2013 at 10:42 AM, Tharanga Abeyseela <tharanga.abeyseela at gmail.com> wrote:
Hi,

Actually i tried that link, still it is not working. it should work for a ip based virtual host, but namebased virtual host is not working.
becuase main request coming to varnish/80 and varnish should redirect the traffic to the backend server based on my round-robin director. when you resolve dns for those two hosts (xxx.com and yyy.com) it reuturns the same IP. that is 192.168.0.100. But there should be a way to send the host-header to apache. Not sure how it works here. But when i use different server (192,168.0.200/ zzz.com) it works without any issue, it roundrobin the traffic. This is an issue with namebased virtualhost. 
varnish recevie the traffic (cluster.com is my  varnish server), then it use it vcl to redirect based on my round-robin setting. but how it can send the traffic to the correct virtualhost. because it gets only the same IP address from the DNS. I managed to change the default behaviour by forcefully setting the host-header as follows to my other virtualhost(yyy..com)
set.req.host="yyy.com" ;
then all request went to yyy.com instead of xxx.com. but what i want is  a round-robin fashio...to send traffic. first req to xxx.com/ 2nd req to yyy.com and so on..

I was trying to find some resourses on the net, stackoverflow etc..but no luck yet. 

Thanks again for your help 
Cheers,
Tharanga

On Fri, Aug 30, 2013 at 5:05 PM, Per Buer <perbu at varnish-software.com> wrote:
Hi Tharanga,

You seem to be a bit confused about how the directors and backends work in Varnish. Please read this:

https://www.varnish-cache.org/docs/trunk/users-guide/vcl-backends.html#backends-and-virtual-hosts-in-varnish

Note that the .host property of the backend has NOTHING to do with the virtual host.

On Fri, Aug 30, 2013 at 2:02 AM, Tharanga Abeyseela <tharanga.abeyseela at gmail.com> wrote:
Hi Per,
Thanks for the reply. Actually my set up looks like this.

cluster.com - 192.168.0.200 (varnish/port 80)
xxx.com - 192.168.0.100  (apache,namebased vhost/8080 - backendname - website)
yyy.com - 192.168.0.100  (apache,namebased vhost/8080 -backendname - api)
cluster.com is the varnish server and front-end connections coming to this and rewrite to other defined back-ends (round-robin based balancing)



backend website {
    .host = "xxx.com";
    .port = "8080";

}
backend api {
    .host = "yyy.com";
    .port = "8080";

}

director clust round-robin {
{ .backend = api;  }
{ .backend = website;  }

}

sub vcl_recv {
set req.backend = clust;
if (req.request)
    {
    return(pass);
    }

}

when i hit the cluster.com , it is always going to xxx.com, but what i need to do is first request go to xxx.com second request yyy.com and so on...when i add another server (different host/different IP say 192.168.0.111/zzz.com, and a different backend) , it goes like this
first request - xxx.com
second request - xxxx.com
third request - zzz.com

but i can change the default behavior by setting up set req.host = yyy.com and then it will goes to 
first request - yyy.com
second request - yyy.com
third request - zzz.com
this is something to do with the host-header forwarding to the correct back-end. how should i add that functionality to the vcl_recv ?
appreciate your help on this, this is working perfectly with other servers (different servers, not with namebased vhosts)
cheers,
Tharanga  

  










On Thu, Aug 29, 2013 at 11:58 PM, Per Buer <perbu at varnish-software.com> wrote:
Hi mate,

On Thu, Aug 29, 2013 at 3:17 PM, Tharanga Abeyseela <tharanga.abeyseela at gmail.com> wrote:
(..)

But how should i send host-headers to varnish to redirect to the correct server, This is always going to xxx.com.

You don't. :-)

You need to dispatch the request to the right backend in vcl_recv and have the backend handle that host. You could start rewriting the host in vcl_recv, but I would advise against it. 

So, if you have two vhosts handled by "website" you just set that as the backend in vcl_recv.

-- 

Per Buer
CTO | Varnish Software AS
Phone: +47 958 39 117 | Skype: per.buer
We Make Websites Fly!

Winner of the Red Herring Top 100 Europe Award 2013







-- 

Per Buer
CTO | Varnish Software AS
Phone: +47 958 39 117 | Skype: per.buer
We Make Websites Fly!

Winner of the Red Herring Top 100 Europe Award 2013







-- 

Per Buer
CTO | Varnish Software AS
Phone: +47 958 39 117 | Skype: per.buer
We Make Websites Fly!

Winner of the Red Herring Top 100 Europe Award 2013






More information about the varnish-misc mailing list