0

其实我的设置是这样的。

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 ?感谢您对此的帮助,这与其他服务器(不同的服务器,而不是基于名称的虚拟主机)完美配合

4

1 回答 1

1

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

因此,您只需要一个后端声明192.168.0.100:8080(因为 Apache 将处理命名虚拟主机)。注意:请求中的主机标头包含已定义的 ApacheServerName/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. 客户端在请求中传递正确的 Host 标头(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 代码不会弄乱收到的 Host 标头(不要将其设置为其他内容)
于 2013-08-30T13:56:50.030 回答