filebeat抓取nginx日志

目录

一、抓取普通的应用输出日志到elasticsearch

二、抓取nginx日志输出到ElasticSearch

2.1、nginx.conf设定日志输出为JSON格式

2.2、nginx.conf设定日志按天输出文件

[2.3、抓取Nginx JSON到ElasticSearch配置](#2.3、抓取Nginx JSON到ElasticSearch配置)


一、抓取普通的应用输出日志到elasticsearch

  • type: log

默认是 5s

scan_frequency: 5s

enabled: true

encoding: utf-8

paths:

  • /opt/mydomain.cn/log-file.log

multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}'

multiline.negate: true

multiline.match: after

tags: ["IP地址","业务名","myapi","dev"]

fields:

app_from: myapi-172

fields_under_root: true

二、抓取nginx日志输出到ElasticSearch

2.1、nginx.conf设定日志输出为JSON格式

复制代码
log_format main '{ "time_local": "$time_local",'
                        '"remote_addr": "$remote_addr",'
                        '"referer": "$http_referer",'
                        '"uri": "$host$uri",'
                        '"status": $status,'
                        '"bytes": $body_bytes_sent,'
                        '"up_addr": "$upstream_addr",'
                        '"upstream_time": "$upstream_response_time",'
                        '"request_time": "$request_time"'
                        '}';

2.2、nginx.conf设定日志按天输出文件

复制代码
map $time_iso8601 $logdate {
        '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
        default 'date-not-found';
        }
    access_log logs/access-$logdate.log main;

2.3、抓取Nginx JSON到ElasticSearch配置

bash 复制代码
- type: log
  # 默认是 5s
  scan_frequency: 10s
  enabled: true
  encoding: utf-8
  paths:
    - /usr/local/nginx/logs/*.log
  json.keys_under_root: true  # Flase会将json解析的格式存储至messages,改为true则不存储至
  json.overwrite_keys: true  #覆盖默认message字段,使用自定义json格式的key
  tags: ["11.111.11.111","nginx","dev"]
  fields:
    app_from: nginx-111
  fields_under_root:  true

这一么一点点配置,搞了一整天,才整明白。

相关推荐
kyle~几秒前
C++---value_type 解决泛型编程中的类型信息获取问题
java·开发语言·c++
CoderJia程序员甲4 小时前
GitHub 热榜项目 - 日榜(2025-12-3)
ai·开源·llm·github·ai教程
开心香辣派小星4 小时前
23种设计模式-15解释器模式
java·设计模式·解释器模式
Halo_tjn4 小时前
虚拟机相关实验概述
java·开发语言·windows·计算机
摆烂z5 小时前
Docker与Jib(maven插件版)实战
java
RainbowSea5 小时前
从 Spring Boot 2.x 到 3.5.x + JDK21:一次完整的生产环境迁移实战
java·spring boot·后端
笨手笨脚の5 小时前
Spring Core常见错误及解决方案
java·后端·spring
奶油松果5 小时前
Springboot自动装配 - redis和redission
java·spring boot·redis
霍夫曼5 小时前
UTC时间与本地时间转换问题
java·linux·服务器·前端·javascript
VX:Fegn08955 小时前
计算机毕业设计|基于Java人力资源管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端·课程设计