我想将 GoAccess 与 Haproxy 一起使用,以用作反向代理和负载均衡器。
我的目标是创建一个自定义日志格式,它看起来与 apache2 完全一样(与默认的 goaccess 设置兼容)。
到目前为止我做到了
capture request header Referrer len 64
capture request header Content-Length len 10
capture request header User-Agent len 64
log-format %si:%sp\ %ci\ -\ -\ [%t]\ "%r"\ %ST\ %B\ "%%Referrer"\ "%%User-Agent"
我的haproxy版本是HA-Proxy版本1.5.14 2015/07/02
我不知道如何从捕获的请求中提取引荐来源和用户代理。我在手册中找不到有关自定义日志格式的任何有用信息。
编辑
我发现 %hrl 是捕获的请求标头的字符串列表。是否可以通过键或索引提取值?
答案1
我通过创建自己的日志格式解决了这个问题:
访问
time-format %H:%M:%S
date-format %d/%b/%Y
log-format %^ %^ %^ %^ %^ %^ %h [%d:%t.%^] "%r" %s %b "{%R|%u}"
# %^ - skipped token
# %h - user ip
# %d - date-format
# %t - time-format
# %r - request e.g. GET /something
# %s - server status code
# %b - data response length
# %R - referer - very important if you want to know where your users come from
# %u - user agent
# There is so many skipped tokens because my haproxy put some extra information in every line or rsyslog(?)
# Sample line:
#
# Mar 22 09:09:06 server haproxy[PID]: 10.60.10.50:80 1.2.3.4 [22/Mar/2016:09:08:56.989] "POST /UIDL/?v-uiId=0 HTTP/1.1" 200 334 "{https://www.referer.com/|Mozilla/5.0 (Linux; Android 4.4.4; GT-I9060I Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.83 Mobile Saf}"
哈普罗西
capture request header Referer len 128
capture request header User-Agent len 128
log-format %si:%sp\ %ci\ [%t]\ "%r"\ %ST\ %B\ "%hr"
# %si - your server ip - very usefull if you have multiple application
# %sp - your server port
# %ci - user ip
# %t - datetime in haproxy format
# %r - request
# %ST - status code
# %B - data reponse length
# %hr - captured headers separated by "|" (Referer|User-Agent)