HAProxy community

Rewriting Host tailing dots in logs

I have a haproxy 1.5.18 setup and some users came with tailing dot in hostname
for example “domain.com.”
i cut tailing dot with

http-request replace-value Host (.*). \1

it goes normal to backend servers
but in log file it’s still comes as “domain.com.”

i use

capture request header Host len 20
log-format %[capture.req.hdr(0),lower]\

I think you should issue http-request replace-value ... before capture request header Host...

BTW, the regular expression should escape the last dot. (Perhaps as in \\.?)

Yes, capture request placed under replace-value

...
  http-request replace-value Host (.*). \1
  http-request add-header X-Real-IP %[src]
  # redirect users if no SSL used
  redirect scheme https code 301 if !{ ssl_fc }
  default_backend bk_nginx
  option clitcpka

  # logging
  capture request  header Host len 200
  log-format %[capture.req.hdr(0),lower]\ 
...

Make sure that the new Host header value actually has the final . removed. As said earlier I would fiddle with the regular expression.

Based on the manual example bellow:

http-request replace-value X-Forwarded-For ^192\.168\.(.*)$ 172.16.\1

I would say that your regular expression should in fact be ^(.*)\.$

^(.*)\.$ works as well as my (.*).

tcpdumping to backend shows

...
User-Agent: rrr
Host: domain.com
Accept: */*
...

and that’s ok
but in log file appears original Host domain.com. with final dot

Try to use http-request capture req.fhdr(Host)

This command is unsupported under 1.5.18

Then unfortunately there isn’t much you can do…

Perhaps try to see if there isn’t regsub transformer support, in which case you could write log-format %[capture.req.hdr(0),lower,regsub(^(.*)\.$,\1)]