阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

ELK收集nginx access_log日志

228次阅读
没有评论

共计 1660 个字符,预计需要花费 5 分钟才能阅读完成。

一、案例分析

公司为了每天都能够随时看到公司 WEB 业务的实时运行情况,希望运维通过分析 access.log 日志信息,实时展示一下数据给公司的运营部门:

  • 统计不同返回值的数量
  • 统计访问前 5 名的 IP 地址
  • 统计每日 PV
  • 统计每日 UV
  • …….

二、nginx access_log 定义 json 格式日志

  • 部署 nginx
  • 设置 nginx 访问日志为 json 格式

a、部署 nginx 服务

[root@node4 ~]# tar xf nginx-*.rpm
[root@node4 ~]# cd nginx-*.rpm
[root@node4 ~]# yum -y install pcre-devel zlib-devel gcc-*
[root@node4 ~]# ./configure --prefix=/usr/local
[root@node4 ~]# make
[root@node4 ~]# make install

b、设置 nginx 访问日志为 json 格式

由于 ES 是基于 json 来处理数据的,所以给 ES 的数据就必须是 JSON 数据,只有这样才能通过 json 将数据进行分析、统计。为了能让 ES 能分析 access.log 日志,我们让 nginx 直接将该日志的格式设置为 json 格式。

[root@node4 ~]# vim /usr/local/nginx/conf/nginx.conf
log_format main_json '{"@timestamp":"$time_local",'
'"N_client_ip": "$remote_addr",'
'"N_request": "$request",'
'"N_request_time": "$request_time",'
'"N_status": "$status",'
'"N_bytes": "$body_bytes_sent",'
'"N_user_agent": "$http_user_agent",'
'"N_x_forwarded": "$http_x_forwarded_for",'
'"N_referer": "$http_referer"'
'}';
access_log logs/access.log main_json;

三、日志收集

filebeat 设置 - 修改配置文件定义日志收集

[root@node4 ~]# egrep -v "(#|^$)" /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /usr/local/nginx/logs/access.log
# 添加以下两行,定义收集的是 json 日志
json.keys_under_root: true
json.overwrite_keys: true
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 1
setup.kibana:
host: "192.168.98.200:5601"
output.logstash:
hosts: ["192.168.98.203:5044"]
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~

logstash 设置 - 配置业务文件,接收 Filebeat 发送的数据,然后将数据发送给 ES

[root@node3 conf.d]# cat f_to_e.conf
input {
beats {port => 5044
}
}
output {
elasticsearch {hosts => ["192.168.98.202:9200"]
index => "nginx-%{+YYYY.MM.dd}"
}
stdout {codec => rubydebug}
}

kibana

创建索引,添加 nginx 数据

四、kibana 展示

统计不同返回值的数量 饼图

统计访问前 5 名的 IP 地址 柱形图

统计每日 PV 仪表盘

统计每日 UV 计数
ELK 收集 nginx access_log 日志

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-06-09发表,共计1660字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中