更改Nginx日志为json格式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更改Nginx日志为json格式相关的知识,希望对你有一定的参考价值。

  nginx是最常用的web服务器,其可以承受的并发访问量远远大于apache,通过将nginx保存为json格式,然后对日志进行分析展示,能够获取到很多更直观的信息,然后加以统计,可以进行更多的数据展示。

    log_format access_json ‘"@timestamp":"$time_iso8601",‘
        ‘"host":"$server_addr",‘
        ‘"clientip":"$remote_addr",‘
        ‘"size":$body_bytes_sent,‘
        ‘"responsetime":$request_time,‘
        ‘"upstreamtime":"$upstream_response_time",‘
        ‘"upstreamhost":"$upstream_addr",‘
        ‘"http_host":"$host",‘
        ‘"uri":"$uri",‘
        ‘"domain":"$host",‘
        ‘"xff":"$http_x_forwarded_for",‘
        ‘"referer":"$http_referer",‘
        ‘"tcp_xff":"$proxy_protocol_addr",‘
        ‘"http_user_agent":"$http_user_agent",‘
        ‘"status":"$status"‘;

    access_log  logs/access.log  access_json;

重启Nginx生效日志格式

[[email protected] ~]#/apps/nginx/sbin/nginx -s reload

[[email protected] ~]#tail -f /apps/nginx/logs/*.log
"@timestamp":"2019-06-04T15:40:15+08:00","host":"192.168.36.110","clientip":"192.168.36.104","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.36.104","uri":"/index.html","domain":"192.168.36.104","xff":"192.168.36.1","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:67.0) Gecko/20100101 Firefox/67.0","status":"304"

以上是关于更改Nginx日志为json格式的主要内容,如果未能解决你的问题,请参考以下文章

ELK之nginx日志使用json格式输出

Logstash动态模板映射收集Nginx的Json格式的日志

Nginx 日志记录post数据,并使用goaccess进行日志分析

K8S日志收集:容器日志输出JSON,自动采集至ES

ELK实战之NginxTomcatJava日志收集以及TCP收集日志使用

filebeat 获取nginx日志 发送给ElasticSearch