1.收集nginx访问日志
1.1部署nginx服务
[root@linux-node1 ~]# yum install gcc gcc-c++ automake pcre pcre-devel zlip wget zlib-devel openssl openssl-devel -y [root@linux-node1 ~]# cd /usr/local/src/ [root@linux-node1 src]# wget http://nginx.org/download/nginx-1.10.3.tar.gz [root@linux-node1 src]# tar xvf nginx-1.10.3.tar.gz [root@linux-node1 src]# cd nginx-1.10.3 [root@linux-node1 nginx-1.10.3]# ./configure --prefix=/usr/local/nginx-1.10.3 [root@linux-node1 nginx-1.10.3]# make && make install [root@linux-node1 nginx-1.10.3]# ln -sv /usr/local/nginx-1.10.3 /usr/local/nginx ‘/usr/local/nginx’ -> ‘/usr/local/nginx-1.10.3’
1.2编辑配置文件并准备web页面
[root@linux-node1 nginx-1.10.3]# cd /usr/local/nginx [root@linux-node1 nginx]# vim conf/nginx.conf +48 location /web { root html; index index.html index.htm; } [root@linux-node1 nginx]# mkdir /usr/local/nginx/html/web [root@linux-node1 nginx]# echo " Nginx WebPage! " > /usr/local/nginx/html/web/index.html
1.3测试nginx配置
[root@linux-node1 nginx]# /usr/local/nginx/sbin/nginx -t #测试配置文件语法
[root@linux-node1 nginx]# /usr/local/nginx/sbin/nginx #启动服务
[root@linux-node1 nginx]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 16821 root 6u IPv4 34788 0t0 TCP *:http (LISTEN)
nginx 16824 nobody 6u IPv4 34788 0t0 TCP *:http (LISTEN)
1.4访问nginx页面
1.5将nginx日志转换为json格式
[root@linux-node1 nginx]# vim conf/nginx.conf
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"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"}';
access_log /var/log/nginx/access.log access_json;
##创建log目录
[root@linux-node1 nginx]# mkdir /var/log/nginx
##检测语法
[root@linux-node1 nginx]# /usr/local/nginx/sbin/nginx -t
1.6确认日志格式为json
##重新加载配置文件
[root@linux-node1 nginx]# /usr/local/nginx/sbin/nginx -s reload
##查看日志
[root@linux-node1 nginx]# tailf /var/log/nginx/access.log
{"@timestamp":"2019-01-29T11:17:52+08:00","host":"118.190.201.11","clientip":"118.190.201.1","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"118.190.201.11","url":"/web/index.html","domain":"118.190.201.11","xff":"-","referer":"-","status":"304"}
如无特殊说明,文章均为本站原创,转载请注明出处
- 转载请注明来源:Nginx的日志转换为Json格式
- 本文永久链接地址:https://www.xionghaier.cn/archives/954.html