我需要在具有不同主機名稱的多個正在運行的伺服器之間進行負載平衡。我無法在每台虛擬主機上設定相同的虛擬主機。
是否可以只有一個偵聽配置與多個伺服器並使運行狀況檢查應用該http-send-name-header Host
指令?我使用的是 HAProxy 1.5。
我想出了這個工作 haproxy.cfg,如您所見,我必須為每個運行狀況檢查設定不同的主機名,因為運行狀況檢查會忽略http-send-name-header Host
.我更願意使用變數或其他方法並使事情更加簡潔。
global
log 127.0.0.1 local0 notice
maxconn 2000
user haproxy
group haproxy
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
timeout connect 5000
timeout client 10000
timeout server 10000
stats enable
stats uri /haproxy?stats
stats refresh 5s
balance roundrobin
option httpclose
listen inbound :80
option httpchk HEAD / HTTP/1.1\r\n
server instance1 127.0.0.101 check inter 3000 fall 1 rise 1
server instance2 127.0.0.102 check inter 3000 fall 1 rise 1
listen instance1 127.0.0.101:80
option forwardfor
http-send-name-header Host
option httpchk HEAD / HTTP/1.1\r\nHost:\ www.example.com
server www.example.com www.example.com:80 check inter 5000 fall 3 rise 2
listen instance2 127.0.0.102:80
option forwardfor
http-send-name-header Host
option httpchk HEAD / HTTP/1.1\r\nHost:\ www.bing.com
server www.bing.com www.bing.com:80 check inter 5000 fall 3 rise 2
答案1
defaults
log global
retries 2
timeout connect 3000
timeout server 5000
timeout client 5000
listen any-name-1
bind IP-Address:port
mode tcp or http
option user-check user haproxy_check
balance roundrobin
server hostname IpAddress:port check
server hostname IpAddress:port check
listen any-name-2
bind IP-Address:port
mode tcp or http
option user-check user haproxy_check
balance roundrobin
server hostname IpAddress:port check
server hostanme IpAddress:port check
listen any-name-3
bind IP-Address:port
mode tcp or http
option user-check user haproxy_check
balance roundrobin
server hostname IpAddress:port check
server hostname IpAddress:port check
listen any-name-4
bind IP-Address:port
mode tcp or http
option user-check user haproxy_check
balance roundrobin
server hostname IpAddress:port check
server hostname IpAddress:port check
listen any-name-5
bind IP-Address:port
mode tcp or http
option user-check user haproxy_check
balance roundrobin
server hostname IpAddress:port check
server hostname IpAddress:port check
listen haproxyadmin
bind HAproxyServerIP:HaproxyPort
mode http
stats enable
stats uri /haproxy
stats realm Strictly\ Private
stats auth username:password
答案2
更新:在您描述的情況下,您需要 HTTP/1.1 檢查,這需要硬編碼主機名稱。鑑於 1.5 版本的文檔,似乎沒有辦法避免這種情況,除非您有能力放棄 http 檢查(當然通常不建議這樣做)。
原答案:雖然我不熟悉 haproxy 1.5 的變化,但我在 1.4 中要做的事情(並且我相當確定它仍然適用於 1.5)如下。請注意,前後端分離只是為了個人方便,您可以只使用listen。
defaults
mode http
option httplog
timeout connect 5000
timeout client 10000
timeout server 10000
frontend inbound
bind 127.0.0.1:8000
default_backend webservers
backend webservers
option forwardfor
option httpchk HEAD / HTTP/1.0
http-send-name-header Host
server google www.google.com:80 check inter 5000 fall 3 rise 2
server bing www.bing.com:80 check inter 5000 fall 3 rise 2
結果:
$ curl -i localhost:8000
HTTP/1.1 301 Moved Permanently
Cache-Control: no-cache
Content-Length: 0
Location: http://www.bing.com/
Server: Microsoft-IIS/8.0
P3P: CP="NON UNI COM NAV STA LOC CURa DEVa PSAa PSDa OUR IND"
Set-Cookie: _HOP=I=1&TS=1399981378; path=/
Edge-control: no-store
X-MSEdge-Ref: Ref A: 26CEE14531BF45EFAC91FAC3D1945EDF Ref B: 42CE8D142D427C30F7851B56F38837A6 Ref C: Tue May 13 04:42:58 2014 PST
Date: Tue, 13 May 2014 11:42:57 GMT
$ curl -i localhost:8000
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
Date: Tue, 13 May 2014 11:43:00 GMT
Expires: Thu, 12 Jun 2014 11:43:00 GMT
Cache-Control: public, max-age=2592000
Server: sffe
Content-Length: 219
X-XSS-Protection: 1; mode=block
Alternate-Protocol: 80:quic
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
$