nginx日志输出配置json格式

10 篇文章 0 订阅

nginx日志输出配置json格式

        nginx服务器日志相关指令主要有两条:一条是log_format,用来设置日志格式;另外一条是access_log,用来指定日志文件的存放路径、格式和缓存大小。
log_format指令用来设置日志的记录格式,它的语法如下:
log_format name format {format ...}
其中name表示定义的格式名称,format表示定义的格式样式。

网上统一方法: 

修改nginx.conf配置文件

http {
    include       mime.types;
    default_type  application/octet-stream;
    charset  utf-8;
    
    # 原有日志格式,不能注释或者去掉
    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; # 引用日志格式名称

    (省略内容)
}

        在 Nginx 的配置文件nginx.conf中,我们定义了两种的日志格式:main和log_json,其中,main为普通的文本格式,log_json为 json 格式。log_json其实就是手工构造一个 json 字符串。定义了 json 的日志格式后,便可以指定 access log 为 json 格式,修改 Nginx 的配置,重启 Nginx ,便可以看到 json 格式的日志,重启 Nginx。

附加:

可能遇到的问题:

背景:
    因为我这边是通过nginx去代理了很多域名,所以会有很多平台访问的日志,通过在nginx.conf主配置文件增加  include vhosts/*.conf; 导入主机配置,所以会存在一个server段中只能有一个名称的问题。

正确操作:

修改nginx.conf配置文件

[root@elk-nginx-01 conf]# cd /data/services/nginx/conf/
[root@elk-nginx-01 conf]# pwd
/data/services/nginx/conf
[root@elk-nginx-01 conf]# ll
总用量 72
-rw-r--r-- 1 root root 1077 9月   3 09:45 fastcgi.conf
-rw-r--r-- 1 root root 1077 9月   3 09:45 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 9月   3 09:45 fastcgi_params
-rw-r--r-- 1 root root 1007 9月   3 09:45 fastcgi_params.default
-rw-r--r-- 1 root root 2837 9月   3 09:45 koi-utf
-rw-r--r-- 1 root root 2223 9月   3 09:45 koi-win
-rw-r--r-- 1 root root 5231 9月   3 09:45 mime.types
-rw-r--r-- 1 root root 5231 9月   3 09:45 mime.types.default
-rw-r--r-- 1 root root 3729 9月   3 15:49 nginx.conf
-rw-r--r-- 1 root root 2656 9月   3 13:59 nginx.conf-bak0903
-rw-r--r-- 1 root root 2656 9月   3 09:45 nginx.conf.default
-rw-r--r-- 1 root root  636 9月   3 09:45 scgi_params
-rw-r--r-- 1 root root  636 9月   3 09:45 scgi_params.default
drwxr-xr-x 2 root root  134 9月   3 11:34 sslkeys
-rw-r--r-- 1 root root  664 9月   3 09:45 uwsgi_params
-rw-r--r-- 1 root root  664 9月   3 09:45 uwsgi_params.default
drwxr-xr-x 2 root root   87 9月   3 16:53 vhosts
-rw-r--r-- 1 root root 3610 9月   3 09:45 win-utf
[root@elk-nginx-01 conf]# vim nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    charset  utf-8;
    
    # 原有日志格式,不能注释
    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 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"'
                        ' }';

    #导入主机配置
    include vhosts/*.conf;

    (省略内容)
}
保存退出!

[root@elk-nginx-01 conf]# pwd
/data/services/nginx/conf
[root@elk-nginx-01 conf]# cd vhosts/
[root@elk-nginx-01 vhosts]# ll
总用量 12
-rw-r--r-- 1 yfbkf yfbkf  921 9月   3 15:37 gonggao.conf
-rw-r--r-- 1 yfbkf yfbkf 1482 9月   3 16:50 gongdan.conf
-rw-r--r-- 1 yfbkf yfbkf 1151 9月   3 14:42 gongmo.conf

 然后重启nginx

总结:

1、原有的日志格式不能注释或者去掉,只能新增一个log_format;
2、新增自定义一份日志记录格式,需要注意,log_format指令设置的名称在配置文件中是不能重复的(比如我json日志格式名称 json)

3、原有的日志格式如果调整了,需要在log后加上名称才生效;

好了,这就是nginx日志输出配置json格式的方法了,如有问题可与博主一起交流讨论!

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值