HAProxy的高级配置选项-修改报文首部

简介: 这篇文章介绍了HAProxy的高级配置选项,特别是如何通过配置来修改HTTP报文首部,包括添加和删除特定的响应头信息,以及如何通过条件判断来控制这些修改。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

  我们之前学习过在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/)并查看响应报文,如下图所示。

目录
相关文章
|
17天前
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
文章介绍了如何配置HAProxy以支持HTTPS协议和实现服务器的动态上下线。
48 8
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
|
17天前
|
Apache 数据安全/隐私保护
HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例
这篇文章介绍了HAProxy的ACL(访问控制列表)功能,特别是如何基于用户代理(User-Agent)即浏览器类型进行匹配和流量分发的高级配置选项,并通过实战案例展示了如何配置ACL规则以实现基于不同浏览器的访问控制。
35 5
HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例
|
27天前
|
网络协议 Linux
在Linux中,如何使用iptables 写⼀条规则?把来源IP为192.168.1.101访问本机80端口的包直接拒绝.
在Linux中,如何使用iptables 写⼀条规则?把来源IP为192.168.1.101访问本机80端口的包直接拒绝.
|
4月前
|
监控 安全 网络协议
|
网络协议 安全 算法
动态主机配置协议(DHCPv4)的无类静态路由选项
IP 协议 [1] 使用路由器将数据包从连接到一个 IP 子网的主机传输到连接到不同 IP 子网的主机。当 IP 主机(源主机)希望将数据包传输到另一台 IP 主机(目的地)时,它会查阅其路由表以确定应该用于将数据包转发到目标主机的路由器的 IP 地址。
277 0
动态主机配置协议(DHCPv4)的无类静态路由选项
|
存储 缓存 网络协议
【先码后看】DHCP 扩展选项大全
动态主机配置协议 (Dynamic Host Configuration Protocol,DHCP) [1] 提供了一个框架,用于将配置消息传递给 TCP/IP 网络上的主机。配置参数和其他控制消息携带在标签数据项中,这些数据项存储在 DHCP 消息的“选项/options”字段中。数据项本身也称为“选项/options”。
1221 0
【先码后看】DHCP 扩展选项大全
|
监控 网络协议 网络安全
zabbix自定义TCP 11种状态监控模板(十四)
zabbix自定义TCP 11种状态监控模板 如果一直对多个主机同时增加监控项那么会非常麻烦,在实际生产中有很多时候我们都需要对一批机器同时增加多个相同的监控项,如果一个一个手动添加将会非常麻烦,因此就有了模板的诞生,我们只需要创建一个模板,让需要监控的主机链接模板即可
275 0
zabbix自定义TCP 11种状态监控模板(十四)