nginx日志统计分析

 本文主要使用的是grep,awk,cut等工具来对nginx日志进行统计和分析,具体如下:

1,列出当天访问最多次数的ip地址

cut -d- -f 1 /usr/local/nginx/logs/20160329/access_2016032913.log |uniq -c | sort -rn | head -20 

1[root@httpservera 20160329]# cut -d- -1 /usr/local/nginx/logs/20160329/access_2016032913.log |uniq -| sort -rn | head -20   2     69 180.116.214.31  3     45 180.116.214.31  4     45 180.116.214.31  5     36 49.80.54.111  6     35 183.206.185.204  7     35 180.116.214.31  8     32 49.80.54.111  9     32 49.80.54.111  10     32 180.116.214.31  11     31 117.136.45.101  12     29 180.116.214.31  13     28 218.205.19.112  14     28 180.116.214.31  15     28 180.116.214.31  16     27 49.80.54.111  17     27 222.185.248.242  18     24 49.80.54.111  19     24 175.0.8.161  20     23 49.80.54.111  21     23 49.80.54.111

2,查看某一个页面被访问的次数

[root@httpservera 20160329]#grep "/index.php" log_file | wc -l

3,查看每一个IP访问了多少页面并排序

awk '{++S[$1]} END {for (a in S) print a,S[a]}' access_2016032913.log |uniq|sort -rn|more

1[root@httpservera 20160329]# awk '{++S[$1]} END {for (a in S) print a,S[a]}' access_2016032913.log |uniq|sort -rn|more 2223.94.229.51 148 3223.73.166.191 1 4223.68.252.103 156 5223.68.167.66 2 6223.68.106.138 43 7223.67.99.72 7 8223.67.153.173 12 9223.66.93.152 15 10223.66.38.31 103 11223.65.191.181 1 12223.65.191.135 11 13223.65.190.71 13 14223.65.141.78 3 15223.64.63.71 31 16223.64.63.229 7 17223.64.62.242 59 18223.64.62.23 27 19223.64.62.216 1 20223.64.62.160 40 21223.64.61.136 28 22223.64.60.80 13 23223.64.60.21 12 24223.64.237.37 187 25223.64.209.247 2 26223.64.158.4 15

其中,sort -rn 按照数字从大到小排序,uniq 将重复行去除。

4,查看某一个ip访问了那些页面:grep ^xx.xx.xx.xx log_file |awk '{print $1,$7}'

1[root@httpservera 20160329]# grep ^223.147.39.194 17 access_2016032913.log |awk '{print $1,$7}'          grep: 17: No such file or directory 2access_2016032913.log:223.147.39.194 //customer/customerInfo/getCustUnReadMsgInfo.json 3access_2016032913.log:223.147.39.194 //customer/customerInfo/getCustUnReadMsgInfo.json 4access_2016032913.log:223.147.39.194 //remind/redDot/checkRedDot.json 5access_2016032913.log:223.147.39.194 //remind/redDot/checkRedDot.json 6access_2016032913.log:223.147.39.194 //thirdpartyapi/appaction/app_action/action_send_batch.json 7access_2016032913.log:223.147.39.194 //customer/customerInfo/getCustUnReadMsgInfo.json 8access_2016032913.log:223.147.39.194 //customer/customerInfo/getCustUnReadMsgInfo.json 9access_2016032913.log:223.147.39.194 //remind/redDot/checkRedDot.json 10access_2016032913.log:223.147.39.194 //remind/redDot/checkRedDot.json 11access_2016032913.log:223.147.39.194 //customer/customerInfo/getCustUnReadMsgInfo.json 12access_2016032913.log:223.147.39.194 //customer/customerInfo/getCustUnReadMsgInfo.json 13access_2016032913.log:223.147.39.194 //remind/redDot/checkRedDot.json 14access_2016032913.log:223.147.39.194 //remind/redDot/checkRedDot.json 15access_2016032913.log:223.147.39.194 //customer/customerInfo/getCustUnReadMsgInfo.json 16access_2016032913.log:223.147.39.194 //customer/customerInfo/getCustUnReadMsgInfo.json 17access_2016032913.log:223.147.39.194 //remind/redDot/checkRedDot.json 18access_2016032913.log:223.147.39.194 //remind/redDot/checkRedDot.json

5,去掉搜索引擎统计当天的页面:awk '{print $12,$1}' access_2016032913.log | grep ^\"Mozilla | awk '{print $2}' |sort | uniq | wc -l      

1[root@httpservera 20160329]# awk '{print $12,$1}' access_2016032913.log | grep ^\"Mozilla | awk '{print $2}' |sort | uniq | wc -l       235

6,查看一个小时内有多少ip访问:

1[root@httpservera 20160329]# awk '{print $4,$1}' access_2016032913.log | grep 29/Mar/2016:13 | awk '{print $2}'| sort | uniq | wc -l    21926

2.访问量统计

1.根据访问IP统计UV

awk '{print $1}'  access.log|sort | uniq -c |wc -l

2.统计访问URL统计PV

awk '{print $7}' access.log|wc -l

3.查询访问最频繁的URL

awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more

4.查询访问最频繁的IP

awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|more

5.根据时间段统计查看日志

 cat  access.log| sed -n '/14\/Mar\/2015:21/,/14\/Mar\/2015:22/p'|more

备注:nginx 日志分割脚本

1[root@iZ237lzm354Z logs]# vim  /opt/shell/nginx_log.sh 2#! /bin/bash 3#Power by guojinbao 4date=`date +%Y-%m-%d-%H-%M-%S` 5logfile="/guojinbao/nginx/logs/access.log" 6logdir=/guojinbao/nginx/logs 7pid=`cat /usr/local/nginx/logs/nginx.pid` 8if [ ! -d $logdir ]; then 9    mkdir -p $logdir 10fi 11/bin/mv $logfile $logdir/access_${date}.log 12kill -HUP $pid

本文出自 “清风明月” 博客,请务必保留此出处http://liqingbiao.blog.51cto.com/3044896/1758009

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )