• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

nginx埋点,按天输出日志,filebeat收集日志到elasticsearch

武飞扬头像
晨宸辰成
帮助1

一、Nginx 按天生成日志

定义时间变量指定格式

    map $time_iso8601 $logdate {
        '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
        default    'date-not-found';
    }

自定义日志json格式

log_format  json_log '{"time":"$logdate","demo1":"$arg_demo1","demo2":"$arg_demo2"}';

$args $arg_name 是nginx预定义变量
$args 可以接收请求uri后面的参数
$arg_name 当前请求中名为 name 的参数的值,而且还是未解码的原始形式的值
例:http://192.168.31.112:80/web?demo1=aaaa&demo2=bbb
此时$args 的值为demo1=aaaa&demo2=bbb
$arg_demo1 的值为 aaaa
$arg_demo2 的值为 bbb
存储日志

access_log  logs/ng_$logdate.log  json_log;

二、埋点

前端发送一个请求,ng拦截该请求,返回一个空gif,并将本次请求存入日志

location ^~/web {
            access_log  logs/ng_$logdate.log  json_log;
            empty_gif;
}

之后通过filebeat收集日志,在es中做数据统计

完整nginx.conf


worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    #default_type  application/octet-stream;

    map $time_iso8601 $logdate {
        '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
        default    'date-not-found';
    }
    #默认日志
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    #自定义日志格式
    log_format  json_log '{"time":"$logdate","demo1":"$arg_demo1","demo2":"$arg_demo2"}';

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        #按天输出access日志
        access_log  logs/access_$logdate.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

       location ^~/demo {
            access_log  logs/ng_$logdate.log  json_log;
            empty_gif;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

测试

浏览器发送请求
(注:默认情况下访问nginx静态资源只能用get请求,post请求会报405)
http://192.168.31.112:80/web?demo1=aaaa&demo2=bbb

nginx的logs目录下会多出一个ng_2023-01-06.log的日志文件
查看日志:

{"time":"2023-01-06","demo1":"aaaa","demo2":"bbb"}

三、filebeat收集日志并以json保存在elasticsearch中

filebeat安装步骤 略
在filebeat的目录下创建文件filebeat-nginx.yml

filebeat.inputs:
- type: log
  enabled: true
  paths:
    #nginx日志所在绝对路径,用* 去匹配
    - /usr/local/nginx/logs/ng_*.log
  fields:
    index: nginx
  json.keys_under_root: true              
  json.overwrite_keys: true
#===================================
output.elasticsearch:
    #es连接地址,多个用逗号分隔
    hosts: ["192.168.31.112:9200"]
    username: "elastic"
    password: "123456"
    index: "elk-%{[fields.source]}-*"
    indices:
      - index: "elk-nginx-%{ yyyy.MM.dd}"
        when.equals:
          fields:
            index: "nginx"
#===================================
setup.kibana:
  host: "192.168.31.112:5601"
#===================================
# 允许自动生成index模板
setup.template.enabled: true
# 如果存在模块则覆盖
setup.template.overwrite: true
#和前面的index保持一致
setup.template.name: "elk-nginx"
setup.template.pattern: "elk-*"
setup.ilm.enabled: auto
# 这里一定要注意 会在alias后面自动添加-*
setup.ilm.rollover_alias: "park-ssm"
setup.ilm.pattern: "{now/d}"
# 当我们该属性为false时,就不再遵循ilm的管理,而是索引到我们自己指定的index中
setup.ilm.enabled: false
重要:一定要清空es之前收集的传统日志格式的数据,还要把nginx日志情况,如果有多台nginx服务器,则改完json格式的日志后,一定要把日志清空,否则将会在kibana中显示两种日志格式;删除原有(如果有)elk-nginx-* 的索引

DELETE /elk-nginx-*

启动filebeat:
前台指定配置文件启动:

./filebeat -e -c filebeat-nginx.yml -d "publish"

后台启动

nohup ./filebeat -c /usr/local/filebeat/filebeat-nginx.yml -e > /usr/local/filebeat/logs/filebeat.log 2>&1

敲重点:
此时你关闭终端,断开ssh连接,会导致这个nohup进程也同时终止。因为断开连接时,会发送SIGHUP信号给当前shell的作业列表的所有进程,nohup进程接收到SIGHUP信号后终止。也就是说后台启动的filebeat会停止运行。

解决办法
解决终端关闭导致后台的nohup进程停止的有两种办法。

第一种是,在关闭xshell终端之前,先用exit命令断开ssh连接,然后就可以关闭终端了。但该方法一定程度上仍可能存在直接关闭终端导致nohup进程终止的危险。

第二种是,在原来的命令之前加上disown参数,这个参数将会使启动的nohup进程从当前shell的作业列表中清除,从而避免nohup进程在关闭这个shell时接收到SIGHUP信号。

nohup ./filebeat -c /usr/local/filebeat/filebeat-nginx.yml -e > /usr/local/filebeat/logs/filebeat.log 2>&1 & disown

在浏览器中发送器请求
http://192.168.31.112:80/web?demo1=aaaa&demo2=bbb

在kibana中查询

GET /elk-nginx-2023.01.06/_search
{
  "query": {
    "match_all": {}
  }
}

会发现数据已经是根据自己定义的键值对存储

四、将filebeat做成服务

#进入对应目录
cd /etc/systemd/system
#创建文件
touch filebeat.service
#赋予可执行权限
chmod 777 filebeat.service
#编辑内容
vi filebeat.service

在文件中输入如下内容

[Unit]
Description=filebeat
Wants=network-online.target
After=network-online.target
[Service]
User=root
ExecStart=/usr/local/filebeat-7.17.8/filebeat -e -c /usr/local/filebeat-7.17.8/filebeat-nginx.yml
Restart=always
[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl enable filebeat
systemctl start filebeat
systemctl status filebeat
systemctl stop filebeat

ok

参考连接:filebeat收集json格式的nginx程序日志

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhakgfb
系列文章
更多 icon
同类精品
更多 icon
继续加载