Haproxy-1.8.20 根据路径(URI)转发到后端不同集群

HAProxy根据不同的URI 转发到后端的服务器组

1 ) 实验内容说明:

 1.1 ) 根据不同的URI 转发到后端的服务器组.  /a   /b   和其他   默认使用其他。
 1.2 ) 使用IP介绍:
    haproxy  |    10.10.50.119  |    ha   |    入口LB  
    server-1 |    10.10.29.207  |   http  |    /a 路径转发到此
    server-2 |    10.10.4.209   |   http  |    /b  路径转发到此
    server-3 |    10.10.116.206 |   http  |    其他所有的转发到此

2 ) HAProxy-1.8.20 根据路径转发到不同后端组:

2.1 ) haproxy 配置
[root@ser haproxy]# grep -vE '^$|^#|^  #' haproxy.cfg 
global
maxconn 100000
chroot /data/soft/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 1000
gid 1000
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
pidfile /usr/local/haproxy/run/haproxy.pid
log 127.0.0.1 local3 info
defaults
  option http-keep-alive
  option  forwardfor # ip地址透传 针对http 协议有效
  maxconn 100000
  mode http
  timeout connect 300000ms
  timeout client  300000ms
  timeout server  300000ms
frontend web_prot_80
  bind 0.0.0.0:80
  mode http
  #########################重点部分开始
  acl web_port  path_beg -i /a   # 定义一个ACL,名web_port,模糊匹配路径/a 
  acl mob_port  path_beg -i /b   # 定义一个ACL,名web_port,模糊匹配路径/b  
  use_backend web_port_http_nodes if web_port  
  # 客户端访问HA路径http://10.10.50.119/a,匹配成功web_port就转发到web_port_http_nodes.如果没有成功继续往下匹配.都匹配不成功就转发到 defalt_backend.
  use_backend mob_port_http_nodes if mob_port
  # 客户端访问HA路径http://10.10.50.119/b,匹配成功mob_port就转发到mob_port_http_nodes.如果没有成功继续往下匹配.都匹配不成功就转发到 defalt_backend.
  default_backend backup_nodes   # 一定要有默认转发的这一条,否则会报错。
backend web_port_http_nodes 
  server server1  10.10.29.207:80 weight 1 check port 80 inter 3s fall 2 rise 5
backend mob_port_http_nodes 
  server server2  10.10.4.209:80  weight 1 check port 80 inter 3s fall 2 rise 5
backend backup_nodes 
  server server3  10.10.116.206:80 weight 1 check port 80 inter 3s fall 2 rise 5
  ###########################重点部分结束
listen stats
  mode http
  bind 0.0.0.0:9999
  stats enable
  log global
  bind-process 2
  stats uri     /s
  stats auth    admin:34343434

3 ) 后端机器做如下操作:

## 后端统一安装httpd
yum install httpd -y
systemctl restart httpd

## server-1  
mkdir -p  /var/www/html/a 
echo "this is server-1  29.207" >/var/www/html/a/index.html

## server-2 
server-2   
mkdir -p  /var/www/html/b
echo "this is server -2  4.209" >/var/www/html/a/index.html

## server-3 
server-3   
echo "backup redis-2   index.html" /var/www/html/index.html

4 ) 验证最终过程

[root@client1 haproxy]# curl -L http://10.10.50.119/a
this is server-1  29.207
[root@client1 haproxy]# curl -L http://10.10.50.119/b
this is server -2  4.209
[root@client1 haproxy]# curl -L http://10.10.50.119
backup redis-2   index.html
原文地址:https://www.cnblogs.com/zhenxing06/p/12775391.html