haproxy 7层负载均衡代理转发实战讲解

系统 Linux
haproxy7层模式下,HAProxy会分析协议,并且能通过允许、拒绝、交换、增加、修改或者删除请求(request)或者回应(response)里指定内容来控制协议,做L7的整体表现和F5/netscaler等硬件负载均衡比,毫不逊色。
█ 1、rs web server机测试环境准备:

●1.1 在/var下分别建立php、nginx、pic三个站点目录,并增加index.htms文件及增加内容
假定 php nginx pic分别代表解析不同的服务。

[root@ha2 ~]#for name in php nginx pic ;do mkdir -p /var/$name;echo $name >/var/$name/index.html;done
●1.2 检查结果
[root@ha2 ~]# for name in php nginx pic ;do echo -n "/var/$name/index.html → :";cat /var/$name/index.html; done
/var/php/index.html → :php
/var/nginx/index.html → :nginx
/var/pic/index.html → :pic

●1.3 安装http服务
[root@ha2 ~]# yum install httpd -y

●1.4 配置http服务

先做配置文件备份
[root@ha2 conf]# cd /etc/httpd/conf
[root@ha2 conf]# cp httpd.conf httpd.conf.oldboy.110625
[root@ha2 conf]# ls -l
total 88
-rw-r--r-- 1 root root 34399 Jun 26 16:40 httpd.conf
-rw-r--r-- 1 root root 34399 Jun 26 16:49 httpd.conf.oldboy.110625
-rw-r--r-- 1 root root 13139 May  4 18:54 magic

编辑httpd.conf 最下面加
<Directory "/var">
    Options FollowSymLinks
    AllowOverride none
    Order allow,deny
    Allow from all
</Directory>

NameVirtualHost *:80
<VirtualHost *:80>
        ServerAdmin
49000448@qq.com
        ServerName nginx.etiantian.org
        ServerAlias etiantian.org
        DocumentRoot "/var/nginx"
</VirtualHost>
<VirtualHost *:80>
        ServerAdmin
49000448@qq.com
        ServerName php.etiantian.org
        DocumentRoot "/var/php"
</VirtualHost>
<VirtualHost *:80>
        ServerAdmin
49000448@qq.com

        ServerName pic.etiantian.org
        DocumentRoot "/var/pic"
</VirtualHost>

提示:配置完成记得重起http服务。

●1.5 http服务器本地增加host内容如下
echo '10.0.0.162 nginx.etiantian.org' >>/etc/hosts
echo '10.0.0.162 php.etiantian.org' >>/etc/hosts
echo '10.0.0.162 pic.etiantian.org' >>/etc/hosts
echo '10.0.0.162 etiantian.org' >>/etc/hosts

●1.6 在我们的笔记本电脑上
C:\WINDOWS\system32\drivers\etc\hosts增加如下hosts内容

10.0.0.162 nginx.etiantian.org
10.0.0.162 php.etiantian.org
10.0.0.162 pic.etiantian.org
10.0.0.162 etiantian.org
严重提示:
1.这里解析的IP 为http server的IP
2.这里的host相当于模拟DNS的解析

●1.7测试增加http虚拟主机的配置
访问:http://nginx.etiantian.org 结果应该为nginx,其它类推。
访问:
http://php.etiantian.org 结果应该为php,其它类推。
访问:
http://pic.etiantian.org
结果应该为pic,其它类推。


█ 2 配置haproxy L7负载均衡
●2.1 haproxy.conf配置
#______________________________________________________________________

 

defaults
        log     global
        mode    http
        retries 3
        option redispatch
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
        stats enable
        stats hide-version
        stats uri /admin?stats
        stats auth proxy:123456
        option httpclose
 

●2.2 更改hosts
在我们的笔记本电脑上
C:\WINDOWS\system32\drivers\etc\hosts增加如下hosts内容

10.0.0.162 nginx.etiantian.org
10.0.0.162 php.etiantian.org
10.0.0.162 pic.etiantian.org
10.0.0.162 etiantian.org
严重提示:这里解析的IP 为haproxy server的IP

●2.3 测试haproxy的转发应用
确认host文件配置正常后,可以浏览 etiantian.org
看是否能跳转到nginx.etiantian.org 检查点:url和内容显示
提示:个别浏览器,有可能看不到URL跳转,只要是内容显示正确就对了。

可以修改配置在测试下:
        acl short_dom hdr(Host) -i etiantian.org
        redirect prefix
http://php.etiantian.org code 301 if short_dom
目的:使访问
http://etiantian.org 跳转到http://php.etiantian.org

重起haproxy服务后,进行访问查看。

#########################################################

注意以上 已经应用到正式环境N久 大家可放心使用。

●2.4 更多7层的技术

比如 根据后缀进行过滤转发
acl url_static  path_end         .gif .png .jpg .css .js

在比如根据目录进行过滤转发
acl oldboy_java path_beg /java/
acl static_ryan path_beg /images/
acl static_ryan path_beg /css/

配置haproxy L7负载均衡-之根据URL地址目录转发

准备RS SERVER测试环境

建立测试目录:
for name in php nginx pic ;do mkdir -p /var/$name/$name;echo $name >/var/$name/$name/index.html;done

配置http服务
删除上面做的任何apache配置(或者还原httpd.conf
[root@ha2 conf]# cd /etc/httpd/conf
[root@ha2 conf]# /bin/cp httpd.conf.oldboy.110625  httpd.conf

然后编辑httpd.conf 最下面加
<Directory "/var">
    Options FollowSymLinks
    AllowOverride none
    Order allow,deny
    Allow from all
</Directory>

Listen 8080
Listen 8090

NameVirtualHost *:80
NameVirtualHost *:8080
NameVirtualHost *:8090
<VirtualHost *:80>
        ServerAdmin 49000448@qq.com
        ServerName www.etiantian.org
        ServerAlias etiantian.org
        DocumentRoot "/var/nginx"
</VirtualHost>
<VirtualHost *:8080>
        ServerAdmin 49000448@qq.com
        ServerName www.etiantian.org
        DocumentRoot "/var/php"
</VirtualHost>
<VirtualHost *:8090>
        ServerAdmin 49000448@qq.com
        ServerName www.etiantian.org
        DocumentRoot "/var/pic"
</VirtualHost>


● 3.2
基于目录转发的haproxy配置
frontend oldboy_test
        bind *:80
        acl short_domain hdr(Host) -i etiantian.org
        redirect prefix http://www.etiantian.org code 301 if short_domain

        #acl lang_domain hdr(Host) -i www.etiantian.org
        #use_backend staticpools if lang_domain

        default_backend staticpools

#http://www.etiantian.org/nginx/...,提供静态内容访问(htm,html,css,js
backend staticpools
        balance roundrobin
        server oldboy-1 10.0.0.151:80 check port 80 inter 3000 fall 3
        server oldboy-2 10.0.0.151:81 check port 81 inter 3000 fall 3
        server oldboy-3 10.0.0.151:82 check port 82 inter 3000 fall 3

#提示:后面两个rs是虚构的,只是告诉大家可以这样加server.有没有都不会影响访问。以下同。

#http://www.etiantian.org/php/... 提供动态内容访问(以*.php结尾)
backend dynamicpools
        balance roundrobin
 cookie SERVERID insert indirect
        server oldboy-4 10.0.0.151:8080 maxconn 2048 weight 50  cookie A check port 8080 inter 3000 fall 3
        server oldboy-5 10.0.0.151:8180 maxconn 2048 weight 100 cookie B check port 8180 inter 3000 fall 3
        server oldboy-6 10.0.0.151:8280 maxconn 2048 weight 100 cookie C check port 8280 inter 3000 fall 3
        #
列出更详细的参数,供大家参考使用。
#http://www.etiantian.org/pic/...
提供图片内容访问(以*.jpg,*.png,*.gif等结尾)
backend picpools
        balance roundrobin
        server oldboy-7 10.0.0.151:8090 check port 8090 inter 3000 fall 3
        server oldboy-8 10.0.0.151:8190 check port 8190 inter 3000 fall 3
        server oldboy-9 10.0.0.151:8290 check port 8290 inter 3000 fall 3
#
提示:server后面的名称是自己定义的,名称不要重复就好。

当多台服务器时,每个池子可配置成如下形式:
cookie SERVERID insert indirect
server oldboy-1 10.0.0.163:8080 cookie ett-1-1 check port 8080 inter 3000 fall 3

配置基于URL的健康检查
option httpchk HEAD /checkstatus.jsp HTTP/1.0

配置用户端hosts
10.0.0.162 www.etiantian.org
提示:10.0.0.162 haproxyIP,这里是模拟DNS的解析,实际配置时,在DNS里解析即可


●3.4
在用户端进行访问测试
  3.4.1
查看hosts解析是否正确
C:\Documents and Settings\hyran>ping www.etiantian.org

Pinging www.etiantian.org [10.0.0.162] with 32 bytes of data:

Reply from 10.0.0.162: bytes=32 time=5ms TTL=64
Reply from 10.0.0.162: bytes=32 time<1ms TTL=64
Reply from 10.0.0.162: bytes=32 time<1ms TTL=64
   3.4.2
测试URL地址 看是否符合预期要求
分别访问:
http://www.etiantian.org/nginx/
http://www.etiantian.org/php/
http://www.etiantian.org/pic/
如果能返回对应自己目录的内容,那恭喜你,搞定了。

 

责任编辑:庞桂玉 来源: 51cto.com
相关推荐

2010-05-05 22:58:46

2024-01-31 09:11:16

HaproxyHttpTCP

2012-02-15 00:15:48

2019-07-09 15:10:02

Nginx反向代理负载均衡

2012-05-07 10:17:48

2013-04-22 11:29:14

Nginx

2018-10-17 09:51:04

负载均衡服务器性能

2019-06-19 15:34:39

Nginx反向代理负载均衡

2012-02-14 00:01:22

2010-04-20 18:13:44

网络负载均衡设置

2010-04-26 13:34:43

DNS负载均衡

2010-04-20 10:27:57

什么是负载均衡

2014-07-31 10:55:08

域名NAT七层

2010-05-10 18:11:24

负载均衡机

2013-08-22 16:32:24

2019-09-27 08:18:13

负载均衡核心Key

2017-12-18 12:04:02

Nginx代理均衡

2019-11-04 15:35:53

Nginx反向代理负载均衡

2010-05-04 13:23:55

Tomcat负载均衡

2010-04-21 10:30:12

负载均衡
点赞
收藏

51CTO技术栈公众号