Unable to add request headers to logs

Using HAProxy version: 2.8.7

I am trying to add request headers to log entries for testing purposes. However, I am apparently doing something incorrectly.

Here are what I consider to be the relevant parts of the config file:

global
  log stdout len 65535 format raw local0

defaults
  mode   http
  option httplog

frontend http-ingress
  log global

  http-request set-var(txn.host) req.hdr(Host)
  http-request set-var(txn.agent) req.hdr(User-Agent)
  log-format "host: %[var(txn.host)], agent: %[var(txn.agent)]"

The log output is:

host: !,(BADINDEX) agent: !(BADINDEX)

If I use req.hdrs instead:

  log-format "http_request_headers: %{+Q}[var(txn.req_hdrs)]"
  http-request set-var(txn.req_hdrs) req.hdrs

The log output is:

http_request_headers: !{(MISSING)+Q}[var(txn.req_hdrs)]

What am I doing wrong?

Hello,

your configuration-snippet…

  http-request set-var(txn.host) req.hdr(Host)
  http-request set-var(txn.agent) req.hdr(User-Agent)
  log-format "host: %[var(txn.host)], agent: %[var(txn.agent)]"

…works like a charm on my https-frontend. I also have 2.8.7 running.

Log-Output:

Mar  7 11:40:19 dmz-pxy-t1 haproxy[1645297]: host: ***********.net, agent: like Gecko) Chrome/122.0.0.0 Safari/537.36

Your log output looks a little bit strange:

host: !,(BADINDEX) agent: !(BADINDEX)

I dont understand, why the comma is written before the BADINDEX :confused:

best regards,
Markus

@MaEh, thanks for verifying that for me. I assumed the config was valid, since I got it from the HAProxy documentation. The strange output is what is confusing me as well.