Nginx 日志接入 Elastic Stack 系统

本文介绍如何将Nginx日志接入ElasticStack日志系统,并通过Kibana展示。主要内容包括配置Nginx日志为JSON格式、Filebeat采集日志、配置输出到Elasticsearch及Kibana上的索引创建。

文章《搭建 Elastic Stack 日志系统》 描述了如何利用 Kibana,Elasticsearch,Filebeat 来搭建日志系统。本文在上一篇 Elastic Stack 文章的基础上,说明如何将 Nginx 的日志接入 Elastic Stack 日志系统。

为了更新在 Kibana 上展示 Nginx 的日志,可以先将 Nginx 的日志改成 json 格式,如何更改 Nginx 的日志格式,可以参考文章 《Nginx 日志改成 JSON 格式》

假设我们的 Nginx 访问日志路径为 /usr/local/nginx/logs/access.log ,错误日志路径为 /usr/local/nginx/logs/error.log,那么我们的 Filebeat 配置可以这样写:

filebeat.prospectors:

- input_type: log 
  paths:
    - /usr/local/nginx/logs/access.log
  json.keys_under_root: true
  json.message_key:

- input_type: log
  paths:
    - /usr/local/nginx/logs/error.log

由于我们只对 Nginx 的访问日志以 json 的格式打印,错误日志并没有以 json 格式打印,因此我们使用了两个 input_type 的配置项。

Filebeat 提供对 json 格式日志的处理功能。我们设置json.keys_under_root以打开 Filebeat 解析 json 日志功能。有关 json 日志解析的配置,可以参考官网的介绍,此处不再贅述,参考链接:https://www.elastic.co/guide/en/beats/filebeat/current/configuration-filebeat-options.html#config-json

配置完 Filebeat 的输入后,我们接下来配置 Filebeat 的输出。

output.elasticsearch:
  hosts: ["10.88.102.179:9200"]
  index: "nginx-%{+yyyy.MM.dd}"

由于使用 Elasticsearch 来接收日志,所以需要配置 Elasticsearch 的地址 hosts。在 Kibana 中可能需要展示多个 index 的日志,为了区分,需要配置 index 的值,这里我们使用 nginx-%{+yyyy.MM.dd},在 Kibana 中显示的 index 实际为 nginx-2017.12.12 这种形式。读者可以根据需要自行配置 index。

Filebeat 配置好后,重启 Filebeat 进程以让配置生效。最后我们转到 Kibana 配置新的 index。
打开 Kibana 页面,进入 Management - Index Patterns - Create Index Pattern,以创建一个新的 index。在 Index pattern 输入框输入 nginx-*,创建上面我们的 Nginx 日志索引。
创建好 index 后,在 Discover 中选择 nginx-* 索引,便可以看到 Nginx 的日志。

这里写图片描述

参考资料

  1. http://www.jianshu.com/p/39453a671ce8
  2. http://www.voidcn.com/article/p-apavxvnu-bqt.html
  3. http://www.iyunw.cn/archives/filebeat-shou-ji-json-ge-shi-de-nginx-ri-zhi-fa-song-gei-elasticsearch/
  4. https://www.elastic.co/guide/en/beats/filebeat/current/configuration-filebeat-options.html#config-json
  5. https://discuss.elastic.co/t/filebeat-config-for-nginx-logs/84540/2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值