【问题标题】:Varnish round-robin load balancing with Named Vhosts使用命名 Vhosts 进行 Varnish 循环负载平衡
【发布时间】:2014-05-18 06:37:45
【问题描述】:

其实我的设置是这样的。

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

【问题讨论】:

    标签: varnish varnish-vcl


    【解决方案1】:

    您无需担心 Host 标头,因为 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. 客户端在请求中传递了正确的 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 标头(不要将其设置为其他内容)

    【讨论】:

    • 感谢您的回复,但我的问题是两个后端都连接到同一个 IP 地址。如果您解析 xxx.com 和 yyy.com,两者都将解析为 192.168.0.100。不确定这在清漆中是否可行。干杯,
    • 正如我所说,除非得到指示,否则 Varnish 不关心主机名。因此 varnish 会将收到的“Host”标头传递给 Apache,假设 Varnish 中设置的后端能够处理相应的主机,它会正常工作。我将编辑我的回复,试图澄清一下。
    猜你喜欢
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2018-03-12
    • 2019-10-20
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多