使用命名Vhosts进行清理循环负载平衡

时间:2013-08-30 05:40:34

标签: varnish varnish-vcl

实际上我的设置看起来像这样。

cluster.com - 192.168.0.200 (varnish/port 80)
example.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 = "example.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 example.com, but what i need to do is first request go to example.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 - example.com
second request - examplee.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

这与主机标头转发到正确的后端有关。我应该如何将该功能添加到vcl_recv? 感谢您对此的帮助,这与其他服务器(不同的服务器,而不是基于名称的虚拟主机)完美配合

1 个答案:

答案 0 :(得分:1)

您无需担心主机标头,因为varnish后端选择不使用它。

因此,您只需要192.168.0.100:8080的后端声明(因为Apache将负责命名的虚拟主机)。 注意:请求中的主机标头应包含已定义的Apache ServerName/ServerAlias

因此,如果192.168.0.111可以同时解析example.com和yyy.com但192.168.0.100无法解析zzz.com,则只需要处理后端选择:

# As both your defined backends resolve to the same IP and port,
#you only need to define ONE backend instead of two
backend website_and_api {
  # Which resolves both example.com and yyy.com
  .host = "192.168.0.100";
  .port = "8080";
}
# The server you add later on
backend third {
  # Which resolves all example.com, yyy.com and zzz.com
  .host = "192.168.0.111";
  .port = "8080";
}

director clust round-robin {
  #Backends that resolve both example.com and yyy.com
  { .backend = website_and_api; }
  { .backend = third; }
}

sub vcl_rec {
  # Set the default backend, I'll choose two since it resolves most domains
  set req.backend = third;
  # Choose clust if the domain is appropiate
  if ( req.http.host ~ "example.com"
    || req.http.host ~ "yyy.com") {
    set req.backend = clust;
  }
  # Any return must be done below here
  # ...
}

PS:VCL编辑和扩展试图澄清一点

这可以很好地给出:

  1. 客户端在请求中传递正确的主机标头(example.com | yyy.com | zzz.com)
  2. 正确设置服务器192.168.0.100以处理命名虚拟主机:
    • Apache解析example.com:8080
    • Apache解析yyy.com:8080
    • Apache为192.168.0.100:8080
    • 提供合理的默认值
  3. 正确设置服务器192.168.0.111以处理命名虚拟主机:
    • Apache解析example.com:8080
    • Apache解析yyy.com:8080
    • Apache解析zzz.com:8080
    • Apache为192.168.0.100:8080
    • 提供合理的默认值
  4. 您的VCL代码不会收到主机标题(不要将其设置为其他内容)