之前我没有指定自定义日志格式,捕获的请求标头打印得很好。我想记录unique-id-header
,但找不到任何方法。所以我复制粘贴了上面提到的日志格式http://haproxy.1wt.eu/download/1.5/doc/configuration.txt,并且唯一 ID 开始被记录。
不幸的是,现在捕获的请求标头没有被记录下来。它们%hrl and %hsl
以空字符串的形式输出。
非常奇怪。这是我的配置,请帮忙 :(
listen server
bind *:80
mode http
option httplog
option http-server-close
no option logasap # disable early logging of HTTP requests so that total transfer time is logged
option forwardfor
capture request header X-Forwarded-For len 500
capture request header Host len 500
capture request header X-Request-UID len 500
rspadd X-Haproxy-Backend:\ server
# Generate the X-Haproxy-Unique-ID and log it to make it easy to track requests
log-format %Ci:%Cp\ [id=%ID]\ [%t]\ %f\ %b/%s\ %Tq/%Tw/%Tc/%Tr/%Tt\ %st\ %B\ %cc\ %cs\ %tsc\ %ac/%fc/%bc/%sc/%rc\ %sq/%bq\ {%hrl}\ {%hsl}\ %{+Q}r
unique-id-format %{+X}o\ %Ci:%Cp_%Fi:%Fp_%Ts_%rt:%pid
unique-id-header X-Haproxy-Unique-ID
以下是我看到的日志:
Sep 13 17:28:57 localhost haproxy[11979]: 10.161.27.218:41592
[id=0AA11BDA:A278_0AA15B71:0050_505217C5_0014:2ECB] [13/Sep/2012:17:28:37.567]
server www-example-com-healthz/- 19998/0/2/1/+20001 200 +326
- - ---- 10/10/1/1/0 0/0 {} {}
"GET /testing/healthz?merchant=www.example.com&source=elb HTTP/1.1"
请注意,%hrl and %hsl
是空字符串:(我如何捕获请求标头??
答案1
默认情况下,这些字段在日志中显示为空。为了记录标头和 cookie,必须在前端设置捕获参数。
以下是我们如何捕获客户端在 HTTP 请求中发送的标头。
capture request header Referrer len 64
capture request header Content-Length len 10
capture request header User-Agent len 64
答案2
更新了配置,因为有些值已被弃用
capture request header X-Request-UID len 500
#Generate the X-Haproxy-Unique-ID and log it to make it easy to track requests
log-format %ci:%cp\ [id=%ID]\ [%t]\ %f\ %b/%s\ %Tq/%Tw/%Tc/%Tr/%Tt\ %ST\ %B\ %CC\ %CS\ %tsc\ %ac/%fc/%bc/%sc/%rc\ %sq/%bq\ {%hrl}\ {%hsl}\ %{+Q}r
unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%Ts_%rt:%pid
unique-id-header X-Haproxy-Unique-ID