zoukankan      html  css  js  c++  java
  • HAProxy的高级配置选项-修改报文首部

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

                                           作者:尹正杰

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

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

  • 相关阅读:
    微信小程序通过getPhoneNumber后台PHP解密获取用户手机号码
    设置API:wx.openSetting,wx.getSetting使用说明(示例:地图授权与取消授权后的重新授权)
    微信小程序之上传图片和图片预览
    微信小程序上传图片(前端+PHP后端)
    微信小程序拒绝授权后重新拉起授权窗口
    微信小程序如何使用 Git 实现版本管理和协作开发
    配置同时使用 Gitlab、Github、Gitee(码云) 共存的开发环境
    linux 系统下Anaconda的安装【安装python3.6环境首选】
    利用phpqrcode二维码生成类库合成带logo的二维码并且用合成的二维码生成海报(二)
    利用phpqrcode二维码生成类库和imagecopymerge函数制拼接图片的经验(一)
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/12148324.html
Copyright © 2011-2022 走看看