nginx访问日志转化为json格式

Nginx日志JSON化
本文介绍如何通过修改Nginx配置文件将访问日志转换为JSON格式,包括具体的配置指令及输出样例。

一、将nginx访问日志转化为json格式
1、修改nginx的配置文件nginx.conf

    log_format  json  '{"@timestamp":"$time_iso8601",'
                      '"@version":"1",'
                      '"client":"$remote_addr",'
                      '"url":"$uri",'
                      '"status":"$status",'
                      '"domain":"$host",'
                      '"host":"$server_addr",'
                      '"size":"$body_bytes_sent",'
                      '"responsentime":"$request_time",'
                      '"referer":"$http_referer",'
                      '"useragent":"$http_user_agent",'
                      '"upstreampstatus":"$upstream_status",'
                      '"upstreamaddr":"$upstream_addr",'
                      '"upstreamresponsetime":"$upstream_response_time"'
                       '}';

    access_log  logs/access_json.log  json;
#将以上内容添加到nginx配置文件的http区块

2、输入的日志格式如下:

{"@timestamp":"2018-08-18T00:44:07+08:00","@version":"1","client":"192.168.1.X","url":"-","status":"400","domain":"localhost","host":"192.168.1.x","size":"166","responsentime":"0.286","referer":"-","useragent":"-","upstreampstatus":"-","upstreamaddr":"-","upstreamresponsetime":"-"}
{"@timestamp":"2018-08-18T22:02:29+08:00","@version":"1","client":"192.168.1.x","url":"-","status":"400","domain":"localhost","host":"192.168.1.x","size":"166","responsentime":"0.205","referer":"-","useragent":"-","upstreampstatus":"-","upstreamaddr":"-","upstreamresponsetime":"-"}
{"@timestamp":"2018-08-18T22:02:29+08:00","@version":"1","client":"192.168.1.x","url":"-","status":"400","domain":"localhost","host":"192.168.1.x","size":"166","responsentime":"0.218","referer":"-","useragent":"-","upstreampstatus":"-","upstreamaddr":"-","upstreamresponsetime":"-"}

3、输出的json格式为:

{
    "@timestamp": "2018-08-18T22:02:29+08:00",
    "@version": "1",
    "client": "192.168.1.x",
    "url": "-",
    "status": "400",
    "domain": "localhost",
    "host": "192.168.1.x",
    "size": "166",
    "responsentime": "0.218",
    "referer": "-",
    "useragent": "-",
    "upstreampstatus": "-",
    "upstreamaddr": "-",
    "upstreamresponsetime": "-"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维那些事~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值