32ew12 发表于 2015-3-27 09:56:46

HAProxy构建虚拟域名负载

本帖最后由 32ew12 于 2015-3-27 10:00 编辑






、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配置
开启路由转发:

1
2
3
#vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
#sysctl-p




HAProxy配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
global(略)
defaults(略)
frontendserver192.168.192.194:80
      acl www.test.comhdr(HOST)      #www.test.com的ACL
      acl www.try.comhdr(HOST)      # www.try.com的ACL
      use_backendtry.com if      #满足www.try.com则使用try.com调度
      use_backendtest.com if    # 满足则使用test.com调度
      default_backend default               #当不是www.test.com或者www.try.com则使用default节点调度

backendtry.com
      balance roundrobin
      server try.com192.168.112.130:80 check
backendtest.com
      balance source
      server test.com 192.168.112.131:80 check

backend default
      balanceroundrobin
      server default127.0.0.1:8008 check




开启本地default的HTTP服务器

1
2
3
4
5
6
7
# yum install-yhttpd
# cd /var/www/html/
# viindex.html
message from default
# vi /etc/httpd/conf/httpd.conf
Listen*:8008
# service httpd start





2.WEB--try.com配置


1
2
3
4
#yuminstallhttpd-y
#vi/var/www/html/index.html
   message from try.com
#service httpd start





3.WEB-test.com配置(同上)

4.测试结果
修改hosts文件





页: [1]
查看完整版本: HAProxy构建虚拟域名负载