nginx默认的日志格式

  1. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  2. '$status $body_bytes_sent "$http_referer" '
  3. '"$http_user_agent" "$http_x_forwarded_for"';

字段说明

  1. 127.0.0.1 - - [14/May/2017:12:51:13 +0800] "GET /index.html HTTP/1.1" 200 4286 "http://127.0.0.1/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"
  2. 远程主机IP 请求时间 时区 方法 资源 协议 状态码 发送字节 referer 浏览器信息

统计访问IP前十

  1. # awk '{print $1}' /usr/local/nginx/logs/access.log | sort | uniq -c | sort -nr | head -10
  2. 6958 123.174.51.164
  3. 2307 111.85.34.165
  4. 1617 118.112.143.148
  5. 1489 117.63.146.40
  6. 1404 118.182.116.39
  7. 1352 1.48.219.30
  8. 1132 60.222.231.46
  9. 1129 10.35.1.82
  10. 943 27.227.163.200
  11. 880 58.253.6.133

统计指定某一天的访问IP

  1. # grep "17/May/2017" /usr/local/nginx/logs/access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -10
  2. # awk '/17\/May\/2017/{print $1}' /usr/local/nginx/logs/access.log | sort | uniq -c | sort -nr | head -10
  3. 6958 123.174.51.164
  4. 2307 111.85.34.165
  5. 1617 118.112.143.148
  6. 1489 117.63.146.40
  7. 1404 118.182.116.39
  8. 1352 1.48.219.30
  9. 1132 60.222.231.46
  10. 1129 10.35.1.82
  11. 943 27.227.163.200
  12. 880 58.253.6.133
  13. 经过测试,在文件较大的时候,先grepawk速度快很多。

过滤URL

  1. # awk '{print $11}' /usr/local/nginx/logs/access.log | sort | uniq -c | sort -nr | head -10
  2. 20737 "http://www.adreambox.net/index.php?app=home&mod=User&act=index"
  3. 4155 "-"
  4. 3981 "http://www.adreambox.net/"
  5. 1921 "http://www.adreambox.net/index.php?app=adreambox&mod=Class&act=prensent&id=5&type=2"
  6. 1299 "http://www.adreambox.net/index.php?app=home&mod=Public&act=doLogin"
  7. 1191 "http://www.adreambox.net/index.php?app=group&mod=Group&act=index&gid=1413"
  8. 718 "http://www.adreambox.net/index.php?app=group&mod=Group&act=index&gid=1403"
  9. 657 "http://www.adreambox.net/index.php?app=wap&mod=Index&act=index"
  10. 657 "http://www.adreambox.net/index.php?act=index&app=home&mod=User"
  11. 639 "http://www.adreambox.net/index.php?app=group&mod=Manage&act=index&gid=1413"

统计指定资源

  1. # awk '($7~/\.html$/){print $1 " " $7 " " $9}' /usr/local/nginx/logs/access.log #处理第7个字段以'.html'结尾的行
  2. 11.0.8.5 //ckeditor/notexist_path.html 404
  3. 11.0.8.5 //ckeditor/CHANGES.html 404
  4. 11.0.8.18 //docs/CHANGELOG.html 404
  5. 11.0.8.5 //themes/mall/default/seller_order.confirm.html 404
  6. 11.0.8.18 //themes/mall/default/header.html 404
  7. 11.0.8.5 //themes/store/default/footer.html 404
  8. 11.0.8.5 //templates/admin/index.html 404
  9. 11.0.8.5 //system/templates/admin/login.html 404
  10. 11.0.8.18 //templates/404.html 404
  11. 11.0.8.18 //admin/editor/editor/dialog/fck_about.html 404
  12. 11.0.8.5 //fckeditor/_whatsnew.html 404
  13. 11.0.8.5 //FCKeditor/_docs/whatsnew.html 404
  14. 11.0.8.5 //style/gb/help/index.html 404
  15. 10.10.1.11 /Login/login.html 404

过滤指定时间后的日志并打印IP

  1. # awk '($4>"[15/May/2017:21:16:38"){print $1}' /usr/local/nginx/logs/access.log | sort | uniq -c | sort -nr
  2. 291031 11.0.8.5
  3. 274174 11.0.8.18
  4. 2764 10.10.1.11
  5. 1193 11.0.8.6
  6. 1 127.0.0.1

统计流量

  1. # grep "17/May/2017" /usr/local/nginx/logs/access.log | awk '{sum+=$10}END{print sum}'
  2. 95210093059

统计状态码

  1. # awk '{print $9}' /usr/local/nginx/logs/access.log | sort | uniq -c | sort -nr | head -10
  2. 1271257 200
  3. 957444 503
  4. 61875 502
  5. 32852 404
  6. 19121 302
  7. 13356 304
  8. 2819 500
  9. 2789 400
  10. 271 499
  11. 203 401

过滤某个时间段的日志

  1. # sed -n '/2017-5-18 9:51:13/,/2017-5-18 9:55:13/p' access.log

Nginx访问日志分析的更多相关文章

  1. 一、基于hadoop的nginx访问日志分析---解析日志篇

    前一阵子,搭建了ELK日志分析平台,用着挺爽的,再也不用给开发拉各种日志,节省了很多时间. 这篇博文是介绍用python代码实现日志分析的,用MRJob实现hadoop上的mapreduce,可以直接 ...

  2. nginx访问日志分析,筛选时间大于1秒的请求

    处理nginx访问日志,筛选时间大于1秒的请求   #!/usr/bin/env python ''' 处理访问日志,筛选时间大于1秒的请求 ''' with open('test.log','a+' ...

  3. 四、基于hadoop的nginx访问日志分析---top 10 request

    代码: # cat top_10_request.py #!/usr/bin/env python # coding=utf-8 from mrjob.job import MRJob from mr ...

  4. Nginx 访问日志分析

    0:Nginx日志格式配置 # vim nginx.conf ## # Logging Settings ## log_format access '$remote_addr - $remote_us ...

  5. 13 Nginx访问日志分析

    #!/bin/bash export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin # Nginx 日志格式: # ...

  6. 二、基于hadoop的nginx访问日志分析---计算日pv

    代码: # pv_day.py#!/usr/bin/env python # coding=utf-8 from mrjob.job import MRJob from nginx_accesslog ...

  7. 五、基于hadoop的nginx访问日志分析--userAgent和spider

    useragent: 代码(不包含蜘蛛): # cat top_10_useragent.py #!/usr/bin/env python # coding=utf-8 from mrjob.job ...

  8. 三、基于hadoop的nginx访问日志分析--计算时刻pv

    代码: # cat pv_hour.py #!/usr/bin/env python # coding=utf-8 from mrjob.job import MRJob from nginx_acc ...

  9. 采集并分析Nginx访问日志

    日志服务支持通过数据接入向导配置采集Nginx日志,并自动创建索引和Nginx日志仪表盘,帮助您快速采集并分析Nginx日志. 许多个人站长选取了Nginx作为服务器搭建网站,在对网站访问情况进行分析 ...

随机推荐

  1. Linux加密到K8S中

    文件名字 test.conf 加密:  base64 --wrap=0 aaa.conf 把得到的密钥填入配置文件当中即可

  2. java按照字节切割字符串,解决汉字的问题

    编写一个截取字符串的函数,输入为一个字符串,截取开始地址,截取字节数,输出为按字节截取的字符串. 但是要保证汉字不被截半个, 如“我ABC”,0,4,应该截为“我AB”,输入“我ABC汉DEF”,1, ...

  3. python-python爬取豆果网(菜谱信息)

    #-*- coding = utf-8 -*- #获取豆果网图片 import io from bs4 import BeautifulSoup import requests #爬取菜谱的地址 ur ...

  4. hwconfig命令详解

    基础命令学习目录首页 转载自系统技术非业余研究 本文链接地址: hwconfig查看硬件信息 最近经常要测试新硬件,了解硬件的具体型号和参数就非常重要,过去经常透过lspci, dmidecode, ...

  5. IOS上z-index和fixed定位无效

    IOS上z-index和fixed定位无效 在该元素上加: -webkit-transform:translateZ(1px); -moz-transform:translateZ(1px); -o- ...

  6. TeamWork#3,Week5,Introduction to the "take-away" Sale Selection Project

    一.NABCD 1.N(Need 需求) 当今社会生活节奏快,很多大学生.上班族叫外卖比较普遍,外卖生意异常火爆.最近美团.饿了么等外卖服务竞争激烈,产生了大量外卖优惠信息.而网络上外卖信息比较混乱, ...

  7. CS小分队第二阶段冲刺站立会议(6月4日)

    昨日成果:昨天一直在对主界面进行修改,遇到问题没有进展 遇到的问题:我代码写的不够缜密,各按钮信息添加的删除的时候总是有重名或者覆盖现象,需要有一次大的检查 今日计划:冲刺已经结束,项目的难度超过了预 ...

  8. Hibernate笔记①--myeclipse制动配置hibernate

    Hibernate 是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hibernate可以应用在任何使用JD ...

  9. ubuntu中启动VIM,以及学习VIM

    启动VIM:首先打开终端,然后输入vi回车,然后输入i或者a,进入. 学习VIM:首先打开终端,然后输入vimtutor回车,然后进入教程学习.

  10. vue 使用出现的问题(持续记录)

    今天写vue 的时候,发现有几个警告.原因是 我把组件起的名字写的和默认标签的名字一样了,导致系统不知道,怎么解析. 我写了一个Header 组件, 和h5里面的header重名, 解决方案1: he ...