作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
我们之前学习过在Nginx上可以修改客户端的报文信息,在haproxy中也是支持该功能的,接下来我们一起来学习一下吧。
一.修改报文首部概述
在请求报文尾部添加指定报文(使用较少):
reqadd<string> [{if | unless} <cond>]
支持条件判断
在响应报文尾部添加指定报文:
rspadd<string> [{if | unless} <cond>]
示例:
rspadd X-Via:\ HAPorxy
从请求报文中删除匹配正则表达式的首部
reqdel<search> [{if | unless} <cond>]
reqidel<search> [{if | unless} <cond>]
不分大小写
从响应报文中删除匹配正则表达式的首部
rspdel<search> [{if | unless} <cond>]
rspidel<search> [{if | unless} <cond>]
示例:
rspidel server.*
从相应报文删除server信息
rspidel X-Powered-By:.*
从响应报文删除X-Powered-By信息
二.试验环境准备
1>.安装httpd服务
安装apache httpd服务,并准备测试数据。
参考连接:
https://www.cnblogs.com/yinzhengjie/p/12114195.html
2>.配置haproxy服务器
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info
defaults
option http-keep-alive
option forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen status_page
bind 172.30.1.102:8888
stats enable
stats uri /haproxy-status
stats auth admin:yinzhengjie
stats realm "Welcome to the haproxy load balancer status page of YinZhengjie"
stats hide-version
stats admin if TRUE
stats refresh 5s
listen WEB_PORT_80
bind 172.30.1.102:80
balance roundrobin
cookie HAPROXY-COOKIE insert indirect nocache
server web01 172.30.1.106:80 cookie httpd-106 check inter 3000 fall 3 rise 5
server web02 172.30.1.107:80 cookie httpd-107 check inter 3000 fall 3 rise 5
server web03 172.30.1.108:80 cookie httpd-107 check inter 3000 fall 3 rise 5 backup
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]#
3>.客户端访问haproxy服务器(http://node102.yinzhengjie.org.cn/)并查看响应报文,如下图所示。
三.为响应报文添加指定字段实战案例
1>.编辑haproxy服务器的配置文件并重启haproxy服务
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info
defaults
option http-keep-alive
option forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen status_page
bind 172.30.1.102:8888
stats enable
stats uri /haproxy-status
stats auth admin:yinzhengjie
stats realm "Welcome to the haproxy load balancer status page of YinZhengjie"
stats hide-version
stats admin if TRUE
stats refresh 5s
listen WEB_PORT_80
bind 172.30.1.102:80
#添加响应报文头部信息
rspadd HAProxy-Version:\ HAPorxy-1.8.20
balance roundrobin
cookie HAPROXY-COOKIE insert indirect nocache
server web01 172.30.1.106:80 cookie httpd-106 check inter 3000 fall 3 rise 5
server web02 172.30.1.107:80 cookie httpd-107 check inter 3000 fall 3 rise 5
server web03 172.30.1.108:80 cookie httpd-107 check inter 3000 fall 3 rise 5 backup
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]#
2>.客户端访问haproxy服务器(http://node102.yinzhengjie.org.cn/)并查看响应报文,如下图所示。****
三.为响应报文删除指定字段实战案例
1>.编辑haproxy服务器的配置文件并重启haproxy服务
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info
defaults
option http-keep-alive
option forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen status_page
bind 172.30.1.102:8888
stats enable
stats uri /haproxy-status
stats auth admin:yinzhengjie
stats realm "Welcome to the haproxy load balancer status page of YinZhengjie"
stats hide-version
stats admin if TRUE
stats refresh 5s
listen WEB_PORT_80
bind 172.30.1.102:80
#添加响应报文头部信息
rspadd HAProxy-Version:\ HAPorxy-1.8.20
#删除响应报文头部信息
rspdel ^Server:.*
balance roundrobin
cookie HAPROXY-COOKIE insert indirect nocache
server web01 172.30.1.106:80 cookie httpd-106 check inter 3000 fall 3 rise 5
server web02 172.30.1.107:80 cookie httpd-107 check inter 3000 fall 3 rise 5
server web03 172.30.1.108:80 cookie httpd-107 check inter 3000 fall 3 rise 5 backup
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]#
2>.客户端访问haproxy服务器(http://node102.yinzhengjie.org.cn/)并查看响应报文,如下图所示。