logstash 采集nginx 日志

简单安装nginx

root@elkstack03 \~\]# yum install -y nginx ## 主配置文件 \[root@elkstack03 \~\]# cat /etc/nginx/nginx.conf user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/\*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user \[$time_local\] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/\*.conf; } ## 子配置文件 \[root@elkstack03 \~\]# vim /etc/nginx/conf.d/www.conf server{ listen 80; server_name _; root /code; index index.html; } \[root@elkstack03 \~\]# mkdir /code \[root@elkstack03 \~\]# echo 'test nginx' \> /code/index.html \[root@elkstack03 \~\]# systemctl start nginx ### 将nginx日志改成Json格式 之前我们讲了tomcat日志,在企业中,修改格式需要与开发商量,但是nginx我们不需要,如果需要原来的格式日志,我们可以将日志输出两份,一份 `main`格式,一份`Json`格式 http{ ... log_format json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"ipaddr":"$remote_addr",' '"login_user":"$remote_user",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"url":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"status":"$status"}'; ... } \[root@elkstack03 conf.d\]# vim www.conf server{ listen 80; server_name www.zls.com; root /code; index index.html; access_log /var/log/nginx/www.zls.com_access_json.log json; } \[root@elkstack03 conf.d\]# cat /etc/nginx/conf.d/blog.conf server{ listen 80; server_name blog.zls.com; root /blog; index index.html; access_log /var/log/nginx/blog.zls.com_access_json.log json; } ## 使用Logstash收集nginx日志 \[root@elkstack03 conf.d\]# cat /etc/logstash/conf.d/nginx_file_es.conf input{ file{ type =\> "www.zls.com_access" path =\> "/var/log/nginx/www.zls.com_access_json.log" start_position =\> "beginning" } file{ type =\> "blog.zls.com_access" path =\> "/var/log/nginx/blog.zls.com_access_json.log" start_position =\> "beginning" } } filter{ json{ source =\> "message" remove_field =\> \["message"

}

}

output{

elasticsearch{

hosts => ["10.0.0.81:9200"]

index => "%{type}-%{+yyyy.MM.dd}"

codec => "json"

}

}

root@elkstack03 conf.d\]# /usr/share/logstash/bin/logstash --path.data=/var/lib/logstash/nginx -f /etc/logstash/conf.d/nginx_file_es.conf \& kibana 展示 ![](https://file.jishuzhan.net/article/1690973510958059521/97060c16f9ff4e4fa74ca50fb9747698.png)

相关推荐
6v6-博客7 小时前
P2P 远程桌面工具|免费开源内网穿透远程软件
运维·服务器
deeper_wind7 小时前
k8s-容器化部署论坛和商城服务(小白的“升级打怪”成长之路)
linux·运维·容器·kubernetes
勇往直前plus7 小时前
VMware centos磁盘容量扩容教程
linux·运维·centos
qq_441996059 小时前
SSH 反向隧道:快速解决服务器网络限制
服务器·网络·ssh
政安晨9 小时前
Ubuntu 服务器无法 ping 通网站域名的问题解决备忘 ——通常与网络配置有关(DNS解析)
linux·运维·服务器·ubuntu·ping·esp32编译服务器·dns域名解析
带刺的坐椅10 小时前
老码农教你 Solon Web Context-Path 的两种配置方式
java·nginx·tomcat·web·solon
007php00710 小时前
Jenkins+docker 微服务实现自动化部署安装和部署过程
运维·数据库·git·docker·微服务·自动化·jenkins
路溪非溪10 小时前
嵌入式Linux驱动开发杂项总结
linux·运维·驱动开发
2501_9200470310 小时前
nginx-限速-限制并发连接数-限制请求数
运维·nginx
RainbowJie111 小时前
Gemini CLI 与 MCP 服务器:释放本地工具的强大潜力
java·服务器·spring boot·后端·python·单元测试·maven