首页 > 解决方案 > HaProxy:如何记录响应正文

问题描述

HaProxy:如何记录响应正文

我能够捕获请求正文,但我无法记录响应正文

我尝试了多个选项,但无法捕获响应正文。

  1. 有没有办法记录响应正文?
  2. 另外,它只能用于 POST 请求吗?

HaProxy.cfg

global
    log 127.0.0.1 local0
 debug
    maxconn 2000
  user haproxy
  group haproxy

defaults
    log global
    mode    http
    option  httplog
    option  dontlognull

  option  http-keep-alive
    timeout http-keep-alive 5m
    timeout http-request 5s
    timeout connect 10s
    timeout client  300s
    timeout server  300s
    timeout check   2s
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http
   balance roundrobin
   option httpchk

frontend LB
   bind *:80
   option httpclose
   option forwardfor
   option http-buffer-request
  declare capture request len 400000
  #declare capture response len 400000
  #http-response capture res.header id 0
  http-request capture req.body id 0
  log-format "%ci:%cp-[%t]-%ft-%b/%s-[%Tw/%Tc/%Tt]-%B-%ts-%ac/%fc/%bc/%sc/%rc-%sq/%bq  body:%[capture.req.hdr(0)]/ Response: %[capture.res(0)]"
   monitor-uri /
   #default_backend LB
   # BEGIN YPS ACLs
    acl is_yps path_beg /ems
    use_backend LB if is_yps
   # END YPS ACLs


backend LB
   option httpchk GET /ems/login.html

   server 10.164.29.225 10.164.30.50:8080 maxconn 300 check
   server 10.164.27.31 10.164.30.50:8080 maxconn 300 check backup

标签: load-balancinghaproxy

解决方案


您可以通过在 cfg 文件中添加以下内容来记录正文

  1. 在前端需要添加以下两行

声明捕获请求行 400000

HTTP-request 捕获 req.body id 0

  1. 由于默认日志行长度为 1024,因此要记录完整请求需要指定最大长度

日志 127.0.0.1 len 65335 local0

无需更改日志格式,使用默认的日志格式


推荐阅读