2013-08-30 27 views
0

其實我的設置看起來像這樣。使用命名虛擬主機進行Varnish循環負載均衡

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

您不需要擔心主機頭,因爲varnish backend選擇不使用它。

所以,你只需要一個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設置正確處理命名虛擬主機:
    • 阿帕奇解析示例.COM:8080
    • 阿帕奇解決yyy.com:8080
    • 阿帕奇給出192.168.0.100:8080
  3. 服務器192.168.0.111設置正確處理命名的虛擬主機一個合理的默認:
    • 阿帕奇解決example.com:8080
    • 阿帕奇解決yyy.com:8080
    • 阿帕奇解決zzz.com:8080
    • 阿帕奇給出192.168.0.100:8080
  4. 一個合理的默認
  5. 你的VCL代碼不要亂收到主機頭(不要把它設置爲另一件事)
+0

感謝您的回覆,但我的問題是後端連接到相同的IP地址。如果您解決xxx.com和yyy.com,則兩者都將解析爲192.168.0.100。不知道這是可能的清漆。歡呼聲, –

+0

正如我所說,除非指示,否則Varnish不關心主機名稱。因此,清漆會將「主機」標頭傳遞給Apache,因爲設置在Varnish中的後端能夠處理相應的主機,所以它會正常工作。我會編輯我的回覆,試圖澄清一點。 – NITEMAN