一、HAProxy虛擬域名負載原理
用戶發起對www.test.com的請求,在HTTP報文中的HOST字段會出現www.test.com,這是用戶在瀏覽器中輸入IP則HOST字段爲IP,如果爲域名則對應的HOST字段就是該域名。
例如: 以www.baidu.com和180.97.33.108 來訪問百度首頁。
因此HAProxy正是通過讀取HTTP報文當中的HOST字段來判斷調度給哪個節點,HAProxy通過配置文件關鍵字hdr(HOST)來獲取HOST字段的值。
二、配置
拓撲如下:
HAProxy配置
開啓路由轉發:
#vi /etc/sysctl.conf net.ipv4.ip_forward = 1 #sysctl -p
HAProxy配置:
global(略) defaults(略) frontend server 192.168.192.194:80 acl www.test.com hdr(HOST) #www.test.com的ACL acl www.try.com hdr(HOST) # www.try.com的ACL use_backend try.com if #滿足www.try.com則使用try.com調度 use_backend test.com if # 滿足則使用test.com調度 default_backend default #當不是www.test.com或者www.try.com則使用default節點調度 backend try.com balance roundrobin server try.com 192.168.112.130:80 check backend test.com balance source server test.com 192.168.112.131:80 check backend default balance roundrobin server default 127.0.0.1:8008 check
開啓本地default的HTTP服務器
# yum install -y httpd # cd /var/www/html/ # vi index.html message from default # vi /etc/httpd/conf/httpd.conf Listen *:8008 # service httpd start
2.WEB--try.com配置
# yum install httpd -y # vi /var/www/html/index.html message from try.com #service httpd start
3.WEB-test.com配置(同上)
4.測試結果
修改hosts文件