0
点赞
收藏
分享

微信扫一扫

六月学习之Haproxy高级功能(自定义HTTP报文)

6、Haproxy高级功能

6.8、自定义HTTP报文

6.8.1、reqadd

reqadd <string> [{if | unless} <crond>]在请求报文中添加指定首部
实现原理:client-->haproxy-->reqadd(添加header)-->web

1、在frontend中使用reqadd,将发往后端集群的请求中添加一个header
cat /etc/haproxy/haproxy.cfg
frontend web
    bind *:88
    mode http
    reqadd X-via:\ Haproxy-Node1
    use_backend webcluster

systemctl reload haproxy

2、修改后端nginx.conf中logformat,添加"$http_x_via"
vim /etc/nginx/nginx.conf
http {
    ...
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for" "$http_x_via"';
    ...
}

3、观察nginx日志
172.16.1.5 - - [25/Dec/2022:11:18:36] "GET / HTTP/1.1" 200 18 "-" "curl/7.29.0" "Haproxy-Node1"

6.8.2、rspadd

rspadd在返回给客户端响应报文中添加指定首部
实现原理:web-->haproxy-->rsqadd(添加header)-->client

1、在frontend中使用rsqadd,通过在响应Header中添加字段,告诉客户端资源经过了哪个代理服务
cat /etc/haproxy/haproxy.cfg
frontend web
    bind *:88
    mode http
    rsqadd via:\ Haproxy-1.18
    use_backend webcluster

systemctl reload haproxy

2、客户端访问测试,然后检查响应Header

六月学习之Haproxy高级功能(自定义HTTP报文)_nginx

6.8.3、rspdel

删除返回给客户端的Header字段
frontend web
    bind *:80
    mode http
    default_backend webservers
    
    # 自定义添加Header给后端的Nginx节点
    http-request add-header X-via Haproxy-Node1
    
    # 自定义添加Header给CLient返回   在浏览器端查看
    http-response add-header Res-Server Haproxy2.2-lb
    
    # 删除返回给客户端的Header字段
    http-response del-header server

举报

相关推荐

0 条评论