Varnish round-robin loadbalancing with Virtualhost (Namebased)

Per Buer perbu at varnish-software.com
Fri Aug 30 13:53:51 CEST 2013


You must set up one director for each set of vhosts that you are planning
to serve. You can't just chuck all the backends into one director.


Per.


On Fri, Aug 30, 2013 at 1:50 PM, Tharanga Abeyseela <
tharanga.abeyseela at gmail.com> wrote:

> Thanks for the info. But in this way you can't do the round-robin
> loadblancing right . actually i need to use the varnish (cluster.com/80)
> and redirect requests to both back-ends in round-robin fashion.
>
> request ---> varnish (cluster.com)  ---RR-loadblance---> xxx.com and
> yyyy.com
>
> I'm not looking to send the request to xxx.com or yyy.com directly. im
> sending the reuquest to cluter.com that is my vanrish server listing on
> port 80.
>
> Thanks,
> Tharanga
>
>
> On Fri, Aug 30, 2013 at 9:35 PM, Per Buer <perbu at varnish-software.com>wrote:
>
>> 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.comand 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.
>>>>>>
>>>>>> --
>>>>>>  <http://www.varnish-software.com/> *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
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>>  <http://www.varnish-software.com/> *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
>>>>
>>>>
>>>>
>>>
>>
>>
>> --
>>  <http://www.varnish-software.com/> *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
>>
>>
>>
>


-- 
 <http://www.varnish-software.com/> *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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20130830/ae7d92ba/attachment.html>


More information about the varnish-misc mailing list