Skip to content

Instantly share code, notes, and snippets.

@holys
Last active January 4, 2016 02:09
Show Gist options
  • Save holys/8553248 to your computer and use it in GitHub Desktop.
Save holys/8553248 to your computer and use it in GitHub Desktop.
python regular pattern for nginx access.log
14.145.146.20 - - [02/Dec/2013:00:01:01 +0800] "GET /api/groups HTTP/1.1" 200 28 "https://qing.wps.cn/" "qing-8.1.17.1 AppleWebKit(2.2.3)" "14.145.146.20" 0.002 -
----
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_time $upstream_response_time';
nginx_pattern = re.compile(r"""(?P<clientip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - - \[(?P<datetime>\d{2}\/[a-z]{3}\/\d{4}:\d{2}:\d{2}:\d{2} (\+|\-)\d{4})\] ((\"(?P<method>\w+?) )(?P<url>.+)(http\/1\.1")) (?P<status>\d{3}) (?P<bytes>\d+) (["](?P<refferer>(\-)|(.+?))["]) (["](?P<useragent>(\-)|(.+?))["]) (["](?P<x_forwarded>(\-)|(.+?))["]) (?P<request_time>(\-)|(.+?)) (?P<upstream_time>(\-)|(.+?))""", re.I)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment