如何在haproxy日志中捕获唯一的ID?

我试图捕获我的日志中的每个请求的唯一ID。 我以为我可以在capture request header X-Unique-ID len 16使用capture request header X-Unique-ID len 16 ,但它不工作。 有什么build议么?

 global log /dev/log local1 maxconn 4096 user haproxy group haproxy daemon stats socket /var/run/haproxy.sock mode 600 level admin spread-checks 5 defaults log global mode http option httplog option dontlog-normal option log-separate-errors option dontlognull retries 2 option redispatch maxconn 2000 timeout connect 5s timeout client 15s timeout server 10s #option forwardfor #option http-server-close frontend http-in bind *:8080 monitor-uri /haproxy-health unique-id-header X-Unique-ID unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%Ts_%rt:%pid capture request header Host len 32 capture request header User-Agent len 200 capture request header X-Unique-ID len 16 rspidel ^X-Powered-By:.* default_backend application-backend backend application-backend balance leastconn option httpclose option forwardfor option httpchk http-check send-state http-send-name-header Node-Name #http-check expect status 200 #cookie JSESSIONID prefix server test1 192.168.1.73:80 #server 10.0.1.102:80 cookie A check 

要logging唯一的ID,您将需要创build使用%ID标记的自定义日志logging格式。 没有必要使用“捕获请求头”来logging。

用自定义日志格式重写上面的示例:

 frontend http-in bind *:8080 monitor-uri /haproxy-health unique-id-header X-Unique-ID unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%Ts_%rt:%pid capture request header Host len 32 capture request header User-Agent len 200 rspidel ^X-Powered-By:.* log-format %{+Q}r\ %ST\ "%CC"\ "%hr"\ "%CS"\ "%hs"\ %ID 

用这样的configuration,你会得到一个如下所示的日志消息:

 Sep 10 17:37:55 lb2 haproxy[28497]: "GET /favicon.ico HTTP/1.1" 200 "-" "{172.20.0.20:8999|Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36}" "-" "" AC140001:C1B8_AC140014:2327_55F1BFF3_0003:6F51 

最后一个条目是唯一的ID。

您可以在http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#8.2.4中看到所有可能的日志logging