nginx日志输出json格式

nginx 默认输出acces日志格式是message格式。现在都做日志统一分析ELK了,message格式就不是很适用了。 所以输出json格式对于后期分析就很友好了。

其他就不说了,直接上配置了

修改 nginx.conf 配置文件, 注释掉之前 log_format 重新写一个 json格式的log_format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#log_format  main  '$hostname $server_name $remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr" '
# '"$upstream_response_time" "$request_time" "$http_cookie"';

log_format main '{"@timestamp":"$time_iso8601",'
'"@source":"$server_addr",'
'"hostname":"$hostname",'
'"remote_user":"$remote_user",'
'"ip":"$http_x_forwarded_for",'
'"client":"$remote_addr",'
'"request_method":"$request_method",'
'"scheme":"$scheme",'
'"domain":"$server_name",'
'"referer":"$http_referer",'
'"request":"$request_uri",'
'"requesturl":"$request",'
'"args":"$args",'
'"size":$body_bytes_sent,'
'"status": $status,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamaddr":"$upstream_addr",'
'"http_user_agent":"$http_user_agent",'
'"http_cookie":"$http_cookie",'
'"https":"$https"'
'}';

access_log /var/log/nginx/access.log main;

重新加载nginx,这样日志就是json格式了

感谢您的支持!