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*