如何在遇到nginx错误请求时使用流利的正则表达式

时间:2017-05-08 10:38:30

标签: regex fluentd

我使用fluentd替换logstash,我使用in-tail插件来拖尾nginx access日志,访问日志的格式如下:< / p>

log_format  main  '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_time';

fluentd conf就像

format /^(?<host>\S+)\s-\s(?<user>\S+)\s\[(?<time>[^\]]*)\]\s(?<method>\S+)\s(?<url>\S+)\s(?<http_version>\S+)\s"(?<status>[^\"]+)"\s(?<bytes>\d+)\s"(?<rfc>[^\"]+)"\s"(?<agent>[^\"]+)"\s"(?<x_forward>[^\"]+)"\s(?<time_spent>\S+).*$/

当请求正确时它可以正常工作,但是当请求错误时它会遇到错误,如下所示:

172.31.33.157 - - [08/May/2017:16:30:20 +0800] - "400" 0 "-" "-" "-" 0.000

错误请求错过了methodrfc字段,因此fluentd运行错误。如何修改format以便我不关心请求是否错误或正确?

任何答案都将不胜感激

遇到另一种情况,agentrfc字段为无,它会运行错误。就像

172.31.44.196 - - [08/May/2017:18:47:31 +0800] GET /click?mb_pl=ios&version=1.1 HTTP/1.1 "302" 5 "-" "" "100.38.38.149, 54.224.136.60" 0.004

172.31.44.196 - - [08/May/2017:18:47:31 +0800] GET /click?mb_pl=ios&version=1.1 HTTP/1.1 "302" 5 "" "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Mobile/14E304" "100.38.38.149, 54.224.136.60" 0.004

如何解决这种情况?

0 个答案:

没有答案