elk-nginx輸出json格式的日志


把Nginx日志的格式輸出成JSON格式展示在Kibana面板,生產環境中基本都是這么使用。

1, 配置nginx

主要修改nginx的訪問日志格式,這里定義成json格式,以便后面logstash更好的處理,建議生產環境也這樣使用。在主配置/etc/nginx/nginx.conf文件中添加如下內容(注釋其他日志格式):

    log_format 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",'
                 '"url":"$uri",'
                 '"referer":"$http_referer",'
                 '"agent":"$http_user_agent",'
                 '"status":"$status"}';

在/etc/nginx/conf.d/default.conf中添加如下一行,定義nginx日志使用的格式,以及日志文件的位置。

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

重新啟動 nginx

nginx -s stop
nginx

2, 配置logstash

修改 indexer的角色配置文件  logstash_indexer.conf

input {
     redis {
       host => "localhost"
       data_type => "list"
       key => "filebeat"
       type => "redis-input"
    }
}
 
filter {
     json {
       source => "message"
       remove_field => "message"
     }
}
 
output {
    elasticsearch {
      hosts => ["localhost"]
      index => "logstash-nginx-%{+YYYY.MM.dd}"
      document_type => "nginx"
      # template => "/usr/local/logstash-2.3.2/etc/elasticsearch-template.json"
      workers => 1
      flush_size => 20000
      idle_flush_time => 10
   }
}

刪除老的 es數據

    
$ rm -fr /data/elasticsearch/*

然后重啟 logstash 和 elasticsearch, 繼續刷新 nginx日志

 

 

轉載自: http://www.ywnds.com/?p=9776


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM