nginx日志输出配置json格式

x33g5p2x  于2022-02-24 转载在 其他  
字(1.1k)|赞(0)|评价(0)|浏览(513)

1 介绍

在做大数据分析的时候,需要收集nginx日志,常用json格式,默认nginx日志不方便处理

2 修改nginx配置

nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
	
	# 原有日志格式
    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';
    # json日志格式
    log_format log_json '{"@timestamp": "$time_local", '
                        '"remote_addr": "$remote_addr", '
                        '"referer": "$http_referer", '
                        '"request": "$request", '
                        '"status": $status, '
                        '"bytes": $body_bytes_sent, '
                        '"agent": "$http_user_agent", '
                        '"x_forwarded": "$http_x_forwarded_for", '
                        '"up_addr": "$upstream_addr",'
                        '"up_host": "$upstream_http_host",'
                        '"up_resp_time": "$upstream_response_time",'
                        '"request_time": "$request_time"'
                        ' }';

    access_log  logs/access.log log_json; # 引用日志格式名称

	...

3 启动

删除原来的 access.log

重新启动nginx

/usr/local/nginx/sbin/nginx -s reload

相关文章