当前位置: 首页 > 科技观察

超级干货!通过filebeat、logstash、rsyslog收集nginx日志

时间:2023-03-21 18:46:35 科技观察

由于nginx强大的功能和突出的性能,越来越多的web应用使用nginx作为http和反向代理的web服务器。nginx访问日志无论是用户行为分析还是安全分析,都是非常重要的数据来源之一。如何有效方便的收集nginx日志进行有效分析成为大家关心的问题。本文通过几个例子来介绍如何通过filebeat、logstash、rsyslog收集nginx访问日志和错误日志。大家都知道ELK技术栈是一个强大的日志收集分析工具。所以这里收集了nginx到ES的日志。当然,至于日志收集,就留着看大家的需要吧。通过logstash,可以方便的配置日志输出的存储方式。一般来说,默认安装nginx后,日志文件在/usr/local/nginx/logs目录下。分别有access.log和error.log访问日志和错误日志。本例中Elasitcsearch是由三个节点172.28.65.22、172.28.65.23、172.28.65.24组成的集群,172.28.65.30是kibana的地址,172.28.65.32是安装了logstash、nginx、filebeat的数据采集服务器在上面。一般来说,采集服务器上有logstash,而采集目标上应该安装nginx和filebeat。1、直接通过filebeat采集日志到ES。在filebeat安装目录下找到filebeat.yml配置获取日志文件的路径和输出到ES的配置。具体来说:-type:log#Changetotruetoenablethisinputconfiguration.enabled:true#Pathsthatshouldbecrawledandfetched.Globbasedpaths.paths:#-/var/log/*.log-/usr/local/nginx/logs/*.log#-c:\programdata\elasticsearch\logs\*如果需要在kibana中友好显示,可以将kibana配置输出到es,在hosts中配置你的ES服务地址。如果单机只有一个节点,则只能配置一个ip和port。启动filebeat收集日志数据/filebeat-e-cfilebeat.yml-d"publish"通过elasticsearch-head插件查看es索引中的日志信息,可以看到access.log和error.lognginx的日志已经上传了。在kibana中通过filebeat-*过滤查看filebeat索引,可以看到通过filebeat收集的数据。这种直接通过filebeat连接ES采集日志的方式简单直接,但是无法对采集到的日志进行预处理等操作,不够灵活。可以在filebeat和ES之间加一层Logstash,filebeat可以和ES解耦,通过Logstash做一些预处理,也可以通过Logstash收集ES以外的数据存储。2、通过filebeat收集日志到logstash,然后发送给ES。首先,您必须安装logstash。安装完成后,在logstash安装目录下创建vifilebeat-pipeline.conf。filebeat-pipeline.conf具体配置如下:input{beats{port=>"5044"}}output{elasticsearch{hosts=>["172.28.65.24:9200"]}stdout{codec=>rubydebug}}输入配置是指通过5044端口接收beats数据。输出配置是指输出到elasticsearch,同时输出到标准输出,也就是控制台。然后通过使用命令bin/logstash-ffilebeat-pipeline.conf--config.reload.automatic应用filebeat-pipeline.conf来启动logstash。启动后可以看到5044端口的logstash启动日志服务已经启动,可以接受通过filebeat通过5044端口传输的数据。接下来配置filebeat:在filebeat安装目录下找到filebeat.yml,配置获取日志文件的路径和输出到logstash的配置。它不直接输出到ES。具体配置如下:屏蔽output.elasticsearch配置,配置output.logstash,配置正确的logstash服务主机和端口。启动filebeat收集日志数据/filebeat-e-cfilebeat.yml-d"publish"我们访问nginx服务提供的web服务http://172.28.65.32/,在控制台可以看到相应的访问权限的日志。同时在ES中的log日志中也可以看到对应的日志数据。3、直接通过rsyslog收集日志到logstash,发送给ES。很多时候,你需要采集的web服务器不在你的控制范围内,不是你想安装filebeat就能让你安装,然后你可以要求目标数据源通过syslog发送日志。然后我们可以通过logstash发送到ES或者其他日志存储和处理平台。通过syslog向日志服务器发送nginx日志有两种方式,一种是使用nginx的配置发送日志,另一种是通过配置linux的rsyslog配置向外发送日志。1、通过nginx配置发送syslog到logstash具体配置如下:在nginx配置文件nginx.conf中,配置服务器access_logsyslog下access_log和error_log的输出方式:server=172.28.65.32:514,facility=local7,tag=nginx_access_log,severity=info;error_logsyslog:server=172.28.65.32:514,facility=local7,tag=nginx_error_log,severity=info;配置完成后,执行./nginx-sreload使配置生效。这样nginx的日志就通过linux的rsyslog服务发送出去了。接下来配置logstash的syslog服务接收配置。在logstash安装目录下创建visyslog-pipeline.conf。syslog-pipeline.conf的具体配置如下:input{syslog{type=>"system-syslog"port=>514}}output{elasticsearch{hosts=>["172.28.65.24:9200"]index=>"system-syslog-%{+YYYY.MM}"}stdout{codec=>rubydebug}}input配置表示通过514端口接收syslog数据。output配置表示输出到elasticsearch,同时输出到标准输出,这是控制台。执行bin/logstash-fsyslog-pipeline.conf--config.reload.automatic启动logstash,可以看到logstash启动后,开启了514端口的tcp和upd协议的监听。我们访问nginx服务提供的web服务http://172.28.65.32/,在logstash的控制台可以看到相应的nginx访问和错误日??志。我们也可以通过Elasticsearch-headData看到ES中对应的日志2.通过配置rsyslog将syslog日志发送到logstash一些老版本的nginx不支持配置syslog输出日志,或者想输出其他日志怎么办不是nginx吗?直接配置rsyslog就可以将日志发送出去。在/etc/rsyslog.conf中配置:$IncludeConfig/etc/rsyslog.d/*.conf表示可以引用外部配置文件。一方面引用外部配置文件可以不影响主配置文件,另一方面也是比较好管理的。在/etc/rsyslog.d目录下新建nginx-log.conf,配置如下:$ModLoadimfile$InputFilePollInterval1$WorkDirectory/var/spool/rsyslog$PrivDropToGroupadm##Nginx访问日志文件路径,根据实际情况:$InputFileName/usr/local/nginx/logs/access.log$InputFileTagnginx-access:$InputFileStateFilestat-nginx-access$InputFileSeverityinfo$InputFilePersistStateInterval25000$InputRunFileMonitor##Nginx错误日志文件路径,根据实际情况修改:$InputFileName/usr/local/nginx/logs/error.log$InputFileTagnginx-error:$InputFileStateFilestat-nginx-error$InputFileSeverityerror$InputFilePersistStateInterval25000$InputRunFileMonitor*.*@172.28.65:514配置完成后重启rsyslog服务。systemctlrestartrsyslog我们访问nginx服务提供的web服务http://172.28.65.32/,在logstash的控制台可以看到同样的效果。本文介绍了通过filebeat、logstash、rsyslog收集nginx访问日志和错误日志的几种方式,需要根据实际情况灵活使用。