HAProxy负载均衡及IP透传功能测试 源地址保持 原地址四层七层保持

七层IP透传测试

原理同nginx通过配置将客户端ip添加到 xforwordefor 请求头中,具体如下:

global
    log    127.0.0.1 local3 info  
    chroot  /usr/local/haproxy
    pidfile  /var/run/haproxy.pid 
    maxconn  65530               
    user   haproxy
    group   haproxy
    daemon                      
defaults
    log     global     
    log     127.0.0.1 local3 info 
    mode    http     
    option  httplog    
    option  dontlognull  
    option  httpclose
#    option  forwardfor
    retries   3  
    maxconn 65530              
    timeout http-request    10s
    timeout queue           1m
    timeout connect         60s  
    timeout client          2m   
    timeout server          2m   
    timeout http-keep-alive 10s
    timeout check           10s

frontend frontend_http
    bind *:80
    option tcplog
    option forwardfor
    mode http
    default_backend backend_http
backend backend_http
    mode http
    balance roundrobin
    server emqx_node_1 192.168.47.11:8080 check
    server emqx_node_2 192.168.47.12:8080 check

frontend frontend_https
    bind *:443
    option tcplog
    mode tcp
    default_backend backend_https
backend backend_https
    mode tcp
    balance roundrobin
    server emqx_node_3 192.168.47.11:8443 check
    server emqx_node_4 192.168.47.12:8443 check
listen haproxy_status
    bind *:9800
    mode http
    option httplog
    maxconn 200
    stats refresh 120s
    log 127.0.0.1 local0 err
    stats uri /haproxy-status
    stats realm welcome login\haproxy
    stats auth admin:123456
    stats hide-version
    stats admin if TRUE

# 测试 针对 http 的请求 增加了 option forwardfor 支持 forwardfor 发现 客户端ip被添加到了 xforwardfor 请求头中

在这里插入图片描述

# 四层  

IP透传测试

haproxy四层透传ip实现方式 是通过支持proxy-protocal 协议来实现,拿到四层流量后将客户端ip信息写入到数据包中,后端服务也必须支持proxy-protocal ,否则会报错。测试如下:
haproxy 配置如下:

global
    log    127.0.0.1 local3 info  
    chroot  /usr/local/haproxy
    pidfile  /var/run/haproxy.pid 
    maxconn  65530               
    user   haproxy
    group   haproxy
    daemon                      
defaults
    log     global     
    log     127.0.0.1 local3 info 
    mode    http     
    option  httplog    
    option  dontlognull  
    option  httpclose
#    option  forwardfor
    retries   3  
    maxconn 65530              
    timeout http-request    10s
    timeout queue           1m
    timeout connect         60s  
    timeout client          2m   
    timeout server          2m   
    timeout http-keep-alive 10s
    timeout check           10s

frontend frontend_http
    bind *:80
    option tcplog
    option forwardfor   
    mode http
    default_backend backend_http
backend backend_http
    mode http
    balance roundrobin
    server emqx_node_1 192.168.47.11:8080 check
    server emqx_node_2 192.168.47.12:8080 check

frontend frontend_https
    bind *:443
    option tcplog
    mode tcp
    default_backend backend_https
backend backend_https
    mode tcp
    balance roundrobin
   # server emqx_node_3 192.168.47.11:8443 send-proxy  check
   #server emqx_node_4 192.168.47.12:8443  check 
   server emqx_node_5 192.168.47.10:808 send-proxy check    --四层在代理服务器后 增加 send-proxy 配置 

listen haproxy_status
    bind *:9800
    mode http
    option httplog
    maxconn 200
    stats refresh 120s
    log 127.0.0.1 local0 err
    stats uri /haproxy-status
    stats realm welcome login\haproxy
    stats auth admin:123456
    stats hide-version
    stats admin if TRUE

重新启动haproxy 服务

# 可以看到请求头中

在这里插入图片描述

# 备注 :

nginx 支持 proxy_protocal,因此后端服务器我们使用nginx,tomcat 不支持 如果后端服务器是tomcat 那么代理会不成功,tomcat读取请求包会报错。
nginx 安装 需要带有 realip等模块如下:–nginx版本 1.20
————————————————

原文链接:https://blog.csdn.net/zhangxm_qz/article/details/124117546

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
haproxy配置七层负载均衡的步骤如下: 1. 安装haproxy:使用命令"yum -y install haproxy"可以安装haproxy软件包。 2. 配置haproxyhaproxy的配置文件分为五个部分,分别是全局配置、默认配置、frontend配置、backend配置和listen配置。对于七层负载均衡,主要关注的是frontend和backend配置。 3. 配置frontend:在frontend配置中,需要指定监听的IP和端口,并设置使用的协议和负载均衡算法。此外,还需要定义ACL规则来根据用户请求的内容将请求转发到不同的后端服务器。 4. 配置backend:在backend配置中,需要定义后端服务器的列表和属性。可以指定服务器的IP地址和端口,并设置权重和最大连接数等属性。 5. 启动haproxy:在完成配置后,可以使用命令"systemctl start haproxy"启动haproxy服务。 需要注意的是,配置七层负载均衡还需要根据具体的需求进行调整和优化,例如设置健康检查、会话保持等功能。此外,还需要确保后端服务器已经配置好并正常工作。 总结:配置haproxy七层负载均衡主要包括安装haproxy、配置haproxy文件、设置frontend和backend配置、启动haproxy服务。配置过程中还需要根据需求进行调整和优化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [haproxy七层负载均衡](https://blog.csdn.net/weixin_46018506/article/details/122257643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值