ELK日志分析之——Filebeat收集nginx的json格式日志

ELK日志分析之——Filebeat收集nginx的json格式日志

原创 运维小弟 2020-05-27 16:57:55
在安装好nginx 之后,默认的日志格式是这样的

log_format combined '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';

现在我们需要把nginx 的日志格式 转换成 json日志格式

1、修改nginx.conf配置文件,在http { } 里新增加如下内容,并且把原先的日志格式注释掉

[root@fxkjnj  / ]# vim /usr/local/nginx/conf/nginx.conf

log_format json '{ "time_local": "$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",'
'"upstream_time": "$upstream_response_time",'
'"request_time": "$request_time"'
' }';

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

修改如下图

ELK日志分析之——Filebeat收集nginx的json格式日志

2、清除日志记录

[root@fxkjnj  / ]#  > /var/log/nginx/access.log

3、检查并重启nginx

[root@fxkjnj  / ]# /usr/local/nginx/sbin/nginx -t
[root@fxkjnj  / ]# /usr/local/nginx/sbin/nginx -s reload

4、通过查看发现,虽然nginx日志变成了json,但是elasticsearch 里还是存储在message里仍然不能拆分

通过查询elk官网得知,需要添加二行参数:

修改filebeat配置文件支持json解析

[root@fxkjnj  / ]# vim /usr/local/filebeat-6.6.0-linux-x86_64/filebeat.yml
 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /usr/local/nginx/logs/access.log
  json.keys_under_root: true
  json.overwrite_keys: true

output.elasticsearch:
    hosts: ["192.168.2.181:9200"]

5.删除elasticsearch 里之前的索引

ELK日志分析之——Filebeat收集nginx的json格式日志

6.重启filebeat

7、模拟网页请求

[root@fxkjnj  / ]# ab -n 100 -c 100 http://192.168.2.110

8、登录到kibana 上查看数据

ELK日志分析之——Filebeat收集nginx的json格式日志
相关文件下载地址
*该资源需回复评论后下载,马上去发表评论?
©下载资源版权归作者所有;本站所有资源均来源于网络,仅供学习使用,请支持正版!
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 共2条

请登录后发表评论