Logstash 收集nginx日志

本文档介绍了如何配置Logstash来收集和解析Nginx日志,包括修改Logstash配置文件,设置文件输入源,定义过滤和格式,以及输出到Elasticsearch。同时,也详细讲解了在CentOS上安装Nginx,配置日志格式,并通过Kibana查看日志。整个流程确保了Nginx日志的有效收集和分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Logstash如何收集nginx日志
1.修改配置文件

[root@elk-2 ~]# cat /etc/logstash/conf.d/nginx.conf
input {
  file {
    path => "/tmp/elk_access.log"  ##指定文件从什么地方收集
    start_position => "beginning"
    type => "nginx"
  }
}
filter {
    grok {
        match => { "message" => "%{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}"}   
    }           ##指定过滤和格式
    geoip {
        source => "clientip"
    }
}
output {
    stdout { codec => rubydebug }
    elasticsearch {
        hosts => ["192.168.40.12:9200"]
        index => "nginx-test-%{+YYYY.MM.dd}"
 }
}

2.检测文件是否有错误

[root@elk-2 ~]# logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/nginx.conf --config.test_and_exit
Sending Logstash's logs to /var/log/logstash which is now configured via log4j2.properties
Configuration OK

3.配置nginx
(1)nginx可以在浏览器下载
在这里插入图片描述

安装依赖包

[root@elk-2 ~]# rpm -ivh openssl-libs-1.0.2k-21.el7_9.x86_64.rpm --force

安装nginx

[root@elk-2 ~]# rpm -ivh nginx-1.16.1-1.el7.ngx.x86_64.rpm` 

修改配置文件nginx

[root@elk-2 ~]# vi /etc/nginx/conf.d/elk.conf

server {
            listen 80;
            server_name elk.com;

            location / {
                proxy_pass      http://192.168.200.30:5601;
                proxy_set_header Host   $host;
                proxy_set_header X-Real-IP      $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
            access_log  /tmp/elk_access.log main2;
        }

配置日志格式 ,添加以下内容
[root@elk2 ~]# vim /etc/nginx/nginx.conf
在这里插入图片描述
检查配置文件是否出错

[root@elk-2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

启动nginx 重启nginx

[root@elk-2 ~]# systemctl start nginx
[root@elk-2 ~]#  systemctl restart logstash

修改windows的hosts文件添加以下内容,路径:C:\Windows\System32\drivers\etc

windows的hosts文件因为权限问题不能直接修改,小技巧直接把hosts文件复制到桌面,然后修改完成再复制回去即可。

在这里插入图片描述
用cmd查看是否可以ping通
在这里插入图片描述
用浏览器访问elk.com
在这里插入图片描述主节点curl以下看是否有日志生成

[root@elk-1 ~]# curl '192.168.200.30:9200/_cat/indices?v' 
health status index                 uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   nginx-test-2021.03.08 cQJKqJlwT96p39oGq7l21A   5   1       9044            0      2.3mb          1.1mb
green  open   .kibana               mHu99yleSkiYFUP9eGpJvg   1   1          2            0     15.1kb          7.5kb
green  open   system-syslog-2021.03 o22ZjGcwTHe9txyNzF9djQ   5   1       9113            0      2.5mb          1.2mb

浏览器直接输入elk.com进可进入kibana管理界面,添加nginx-test*
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值