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