Abhinav Garg
Abhinav Garg

Reputation: 1692

HaProxy : How to log Response Body

HaProxy: How to log Response Body

I am able to capture Request Body, but I am unable to log response Body

I tried multiple options but I am unable to capture Response body.

  1. is there any way to log Response Body?
  2. Also, can it be done only for POST request?

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

Upvotes: 1

Views: 8976

Answers (3)

Valera Dubrava
Valera Dubrava

Reputation: 254

The answer is little bit outdated but it is still important for me. And I did not find the answer anywhere else. You can capture and log response body and request body.

The tricky thing is that you have to define capture response in the backend section. It should looks that:

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

It works for me in version 2.2

Upvotes: 2

Abhinav Garg
Abhinav Garg

Reputation: 1692

You can log body by adding below in cfg file

  1. In Frontend need to add below two lines

declare capture request line 400000

HTTP-request capture req.body id 0

  1. As Default Log line length is 1024, So to log full request need to specify max length

log 127.0.0.1 len 65335 local0

No need to change the log format, use the default log format

Upvotes: 3

jmoney
jmoney

Reputation: 528

You cannot log the request/response body. Take a look at the values you can log: https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#8.2.4

Upvotes: 0

Related Questions