插件窝 干货文章 nginx日志如何导入elasticsearch

nginx日志如何导入elasticsearch

Nginx Filebeat Logstash Elasticsearch 504    来源:    2025-04-26

将Nginx日志导入Elasticsearch的方法

将Nginx日志导入Elasticsearch可以通过多种方式实现,以下是几种常用的方法:

方法一:使用Filebeat

  1. 安装Filebeat:

    # Debian/Ubuntu
    sudo apt-get install filebeat
    
    # RHEL/CentOS
    sudo yum install filebeat
    
  2. 配置Filebeat: 编辑/etc/filebeat/filebeat.yml:

    filebeat.inputs:
    - type: log
     enabled: true
     paths:
       - /var/log/nginx/*.log
     fields:
       log_type: nginx
     fields_under_root: true
    
    output.elasticsearch:
     hosts: ["your-elasticsearch-host:9200"]
     # 如果启用了认证
     username: "elastic"
     password: "yourpassword"
    
  3. 启动Filebeat:

    sudo systemctl start filebeat
    sudo systemctl enable filebeat
    

方法二:使用Logstash

  1. 安装Logstash:

    # Debian/Ubuntu
    sudo apt-get install logstash
    
    # RHEL/CentOS
    sudo yum install logstash
    
  2. 创建Logstash配置文件 (如nginx.conf):

    input {
     file {
       path => "/var/log/nginx/*.log"
       start_position => "beginning"
       sincedb_path => "/dev/null"
       type => "nginx"
     }
    }
    
    filter {
     if [type] == "nginx" {
       grok {
         match => { "message" => "%{NGINXACCESS}" }
       }
       date {
         match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ]
         target => "@timestamp"
       }
     }
    }
    
    output {
     elasticsearch {
       hosts => ["http://localhost:9200"]
       index => "nginx-%{+YYYY.MM.dd}"
       # 如果启用了认证
       user => "elastic"
       password => "yourpassword"
     }
    }
    
  3. 启动Logstash:

    /usr/share/logstash/bin/logstash -f /path/to/nginx.conf
    

方法三:使用Fluentd

  1. 安装Fluentd:

    # 使用td-agent
    curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-bionic-td-agent3.sh | sh
    
  2. 配置Fluentd (/etc/td-agent/td-agent.conf):

    <source>
     @type tail
     path /var/log/nginx/access.log
     pos_file /var/log/td-agent/nginx-access.log.pos
     tag nginx.access
     <parse>
       @type nginx
     </parse>
    </source>
    
    <match nginx.access>
     @type elasticsearch
     host localhost
     port 9200
     logstash_format true
     logstash_prefix nginx
     flush_interval 10s
    </match>
    
  3. 启动Fluentd:

    sudo systemctl start td-agent
    

方法四:直接使用Elasticsearch Ingest Node

  1. 创建Ingest Pipeline:

    curl -X PUT "localhost:9200/_ingest/pipeline/nginx" -H 'Content-Type: application/json' -d'
    {
     "description": "Nginx pipeline",
     "processors": [
       {
         "grok": {
           "field": "message",
           "patterns": ["%{NGINXACCESS}"]
         }
       },
       {
         "date": {
           "field": "timestamp",
           "formats": ["dd/MMM/yyyy:HH:mm:ss Z"],
           "target_field": "@timestamp"
         }
       }
     ]
    }
    '
    
  2. 使用Bulk API导入日志:

    curl -H "Content-Type: application/x-ndjson" -XPOST "localhost:9200/nginx/_bulk?pipeline=nginx" --data-binary @nginx_logs.json
    

注意事项

  1. 日志格式:

    • 确保Nginx日志格式与解析模式匹配
    • 可以在Nginx配置中自定义日志格式: log_format json_combined escape=json '{' '"time_local":"$time_local",' '"remote_addr":"$remote_addr",' '"request":"$request",' '"status": "$status",' '"body_bytes_sent":"$body_bytes_sent",' '"http_referer":"$http_referer",' '"http_user_agent":"$http_user_agent"' '}';
  2. 索引管理:

    • 考虑使用索引生命周期管理(ILM)自动管理索引
    • 可以设置每日索引: nginx-YYYY.MM.dd
  3. 性能考虑:

    • 对于高流量网站,考虑批量处理日志
    • 监控资源使用情况,特别是内存和磁盘I/O
  4. 安全:

    • 如果Elasticsearch暴露在公网,确保启用认证和TLS
    • 考虑使用API密钥而不是用户名/密码

选择哪种方法取决于您的具体需求、现有基础设施和技术栈。Filebeat通常是最轻量级的选择,而Logstash提供了最强大的处理能力。