【问题标题】:HaProxy : How to log Response BodyHaProxy:如何记录响应正文
【发布时间】:2019-03-14 22:00:06
【问题描述】:

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

【问题讨论】:

  • 你得到答案了吗?
  • @Muhunthan 参考我的答案

标签: load-balancing haproxy


【解决方案1】:

答案有点过时,但对我来说仍然很重要。我在其他任何地方都没有找到答案。您可以捕获并记录响应正文和请求正文。

棘手的是您必须在后端部分定义捕获响应。看起来应该是:

frontend
    ...
    declare capture request len 80000
    declare capture response len 80000
    http-request capture req.body id
    log /path/to/some/unix/socket format raw daemon debug debug
    log-format Request\ %[capture.req.hdr(0)]\nResponse\ %[capture.res.hdr(0)]

backend
    ...
    http-response capture res.body id 0

它适用于我的 2.2 版

【讨论】:

    【解决方案2】:

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

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

    声明捕获请求行 400000

    HTTP-request 捕获 req.body id 0

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

    log 127.0.0.1 len 65335 local0

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

    【讨论】:

    • 您的回答中有两个语法错误:“declare capture' 需要捕获长度 ('len')”和“'frontend' 部分中的未知关键字 'HTTP-request'”。然而,这对我来说是有效的:“declare capture request len 400000 http-request capture req.body id 0”。但不幸的是,尸体并没有出现在日志中
    • 当问题是关于响应正文时,此地址不记录请求正文吗?
    【解决方案3】:

    您无法记录请求/响应正文。看看你可以记录的值:https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#8.2.4

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 2013-10-13
      • 2019-05-17
      • 2016-11-24
      • 2016-10-17
      • 2021-07-21
      • 2021-05-21
      • 2014-03-20
      • 2011-03-15
      相关资源
      最近更新 更多