Skip to content

Commit

Permalink
Merge pull request #2171 from grachevko/bugfix/nginx-forwarded-for
Browse files Browse the repository at this point in the history
[Bugfix] Nginx Parser: http_x_forwarded_for can contain multiple IP divided by comma
  • Loading branch information
repeatedly authored Nov 26, 2018
2 parents 2be8387 + 32483a7 commit 2747ce2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fluent/plugin/parser_nginx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Plugin
class NginxParser < RegexpParser
Plugin.register_parser("nginx", self)

config_set_default :expression, /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)"(?:\s+(?<http_x_forwarded_for>[^ ]+))?)?$/
config_set_default :expression, /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)"(?:\s+\"?(?<http_x_forwarded_for>[^\"]*)\"?)?)?$/
config_set_default :time_format, "%d/%b/%Y:%H:%M:%S %z"
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/plugin/test_parser_nginx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def setup
'agent' => 'Opera/12.0',
'http_x_forwarded_for' => '-'
}
@expected_extended_multiple_ip = {
'remote' => '127.0.0.1',
'host' => '192.168.0.1',
'user' => '-',
'method' => 'GET',
'path' => '/',
'code' => '200',
'size' => '777',
'referer' => '-',
'agent' => 'Opera/12.0',
'http_x_forwarded_for' => '127.0.0.1, 192.168.0.1'
}
end

def create_driver
Expand Down Expand Up @@ -65,4 +77,12 @@ def test_parse_with_http_x_forwarded_for
assert_equal(@expected_extended, record)
}
end

def test_parse_with_http_x_forwarded_for_multiple_ip
d = create_driver
d.instance.parse('127.0.0.1 192.168.0.1 - [28/Feb/2013:12:00:00 +0900] "GET / HTTP/1.1" 200 777 "-" "Opera/12.0" "127.0.0.1, 192.168.0.1"') { |time, record|
assert_equal(event_time('28/Feb/2013:12:00:00 +0900', format: '%d/%b/%Y:%H:%M:%S %z'), time)
assert_equal(@expected_extended_multiple_ip, record)
}
end
end

0 comments on commit 2747ce2

Please sign in to comment.