
环境:CentOs 7,nginx,ELk-7.6.1
安装nginx到指定的文件夹下 usr/local/nginx
nginx,知道是进行负载均衡和请求转发的一个反向代理服务器;这个里面有用户请求发起的ip,时间,请求的方式......的日志信息,不用不就浪费了嘛,诸多的用户之外的信息,对于整个系统来说这些用户之外的信息,不会对系统产生影响,但是对公司、集团业务的发展有一定的帮助。拿到具体的信息之后,处理成我们需要的数据,比如用户的ip分布,用户的访问方式分类等等,当我们把大量的数据拿到并且进行分析之后,就能得到公司对待某个业务的用户群体和分布,能对这些群体进行精准的区域营销,区域扩展,提高公司业务在市场中的影响力。不多说了,直接来安装流程
首先安装好的 nginx 需要修改它的配置文件 nginx.conf 或者直接替换
vi /usr/local/nginx/conf/nginx.confworker_processes 1;error_log logs/error.log;error_log logs/error.log notice;error_log logs/error.log info;pid logs/nginx.pid;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;#修改日志的打印格式log_format main '{"client_ip":"$remote_addr","log_time":"$time_local","request":"$request","status":"$status","body_bytes_sent":"$body_bytes_sent","http_referer":"$http_referer","AgentVersion":"$http_user_agent","upstream_addr":"$upstream_addr","request_time":"$request_time","upstream_response_time":"$upstream_response_time"}';access_log logs/access.log main;sendfile on;keepalive_timeout 65;server {listen 80;server_name localhost;location {root html;index index.html index.htm;}error_page 500 502 503 504 50x.html;location = 50x.html {root html;}}}
注:log_format 的日志信息格式不要有换行,否则统计不到数据
先测试启动 nginx 测试 nginx.conf的正确型
./usr/local/nginx/sbin/nginx -tnginx: the configuration file usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file usr/local/nginx/conf/nginx.conf test is successful#启动./usr/local/nginx/sbin/nginx#关闭防火墙 进行浏览器访问 或者直接 curl localhost:80#打开日志查看请求日志
cat usr/local/nginx/logs/access.log{"client_ip":"127.0.0.1","log_time":"26/Apr/2021:18:22:30 +0800","request":"GET HTTP/1.1","status":"200","body_bytes_sent":"612","http_referer":"-","AgentVersion":"curl/7.29.0","upstream_addr":"-","request_time":"0.000","upstream_response_time":"-"}
先输出到Logstash的控制台
编写 nginx_elk.conf 配置文件 配置nginx日志的格式
vi usr/local/logstash-7.6.1/nginx_elk.conf #输入以下内容input{file{#日志存放路径path => "/usr/local/nginx/logs/access.log"#从日志文件开头读取start_position => "beginning"#设置多长时间检测文件是否修改 默认是1sstat_interval => "2"codec => "json"}}output { # 定义日志输出stdout {codec => rubydebug # 输出到控制台先}}
启动 Logstash 访问 nginx 看控制台打印日志信息
cd /usr/local/logstash-7.6.1bin/logstash -f nginx_elk.conf --config.debug# 访问nginx 查看打印到控制台的日志信息{"client_ip" => "192.168.111.1","status" => "304","request_time" => "0.000","http_referer" => "-","AgentVersion" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36","log_time" => "26/Apr/2021:18:40:40 +0800","@timestamp" => 2021-04-26T10:40:41.709Z,"@version" => "1","request" => "GET / HTTP/1.1","upstream_addr" => "-","upstream_response_time" => "-","path" => "/usr/local/nginx/logs/access.log","host" => "bogon","body_bytes_sent" => "0"}
再输出到ElasticSearch
编写 nginx_elk.conf 配置文件 配置nginx日志的格式
input{file{#日志存放路径path => "/usr/local/nginx/logs/access.log"#从日志文件开头读取start_position => "beginning"#设置多长时间检测文件是否修改 默认是1sstat_interval => "2"codec => "json"}}filter {}# 定义日志输出output {elasticsearch {hosts => ["192.168.111.143:9200"] # 定义es服务器的ip,这里使用本地index => "nginx-log" #定义索引名称}stdout {codec => json_lines}}
启动 Logstash 访问 nginx 看控制台打印日志信息
cd /usr/local/logstash-7.6.1bin/logstash -f nginx_elk.conf --config.debug# 等待时间稍微长一点:访问 nginx 在等一会发现已经有了nginx的数据# 打开 https://github.com/mobz/elasticsearch-head 下载head插件,选择下载zip 解压 打开index.html查看节点状态

{"_index": "nginx-log","_type": "_doc","_id": "QBrUDXkBjUFwuBXCQWu6","_version": 1,"_score": 1,"_source": {"AgentVersion": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36","body_bytes_sent": "0","client_ip": "192.168.111.1","upstream_addr": "-","request_time": "0.000","upstream_response_time": "-","@version": "1","status": "304","@timestamp": "2021-04-26T10:57:57.462Z","host": "bogon","log_time": "26/Apr/2021:18:57:51 +0800","http_referer": "-","path": "/usr/local/nginx/logs/access.log","request": "GET / HTTP/1.1"}}#打开kibana 可以对索引进行管理 到此基本完成nginx对日志的统计
处理日志分布的饼状图就可以区分 用户访问的 “热带” 和 “亚热带” 地区,进行精准的服务营销和市场扩展

小小的日志就可以解决客户分布问题,真值:
总结:elk 有很多种统计手段,统计 nginx 日志只是其中的一种,本文纯属个人见解和个人的搭建,没有实战,但可以实现 elk 统计 nginx 日志成功
文章转载自臭虫说,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




