1.nginx日志格式设定

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

参数说明:

$remote_addr   与$http_x_forwarded_for 用以记录客户端的ip地址;
$remote_user   :用来记录客户端用户名称;
$time_local  : 用来记录访问时间与时区; 
$request  :  用来记录请求的url与http协议;
$status     :  用来记录请求状态;成功是200,
$body_bytes_s ent  :记录发送给客户端文件主体内容大小;
$http_referer  :用来记录从那个页面链接访问过来的;
$http_user_agent  :记录客户毒啊浏览器的相关信息;

2.nginx日志切割脚本

  1. #!/bin/bash
  2.  
  3. logs_path="/home/wwwlogs/" #设置日志文件存放目录
  4.  
  5. pid_path="/usr/local/nginx/logs/nginx.pid" #设置pid文件
  6.  
  7. mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log #重命名日志文件,将access.log切割为每天的日志文件如access_20130728.log
  8.  
  9. kill -USR1 `cat ${pid_path}` #向nginx主进程发信号重新打开日志

添加定时任务,每晚00:00切割,crontab -e

  1. * * * bash /usr/local/nginx/nginx_log.sh

3.安装和配置awstats

  1. wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz
  2. tar -zxvf awstats-7.0.tar.gz
  3. mv awstats-7.0 /usr/local/awstats
  4. chown -R root:root /usr/local/awstats
  5. chmod -R =rwX /usr/local/awstats
  6. chmod +x /usr/local/awstats/tools/*.pl
  7. chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl

执行 tools 目录中的 awstats_configure.pl 配置向导,创建一个新的统计

  1. cd /usr/local/awstats/tools
  2. ./awstats_configure.pl

将会有如下一些提示:

  1. -----> Running OS detected: Linux, BSD or Unix
  2.  
  3. -----> Check for web server install
  4. Enter full config file path of your Web server.
  5. Example: /etc/httpd/httpd.conf
  6. Example: /usr/local/apache2/conf/httpd.conf
  7. Example: c:\Program files\apache group\apache\conf\httpd.conf Config file path ('none' to skip web server setup):
  8. >none #这里添none并回车,因为我们没有使用apache

回车之后下一个选项:

  1. Your web server config file(s) could not be found.
  2. You will need to setup your web server manually to declare AWStats
  3. script as a CGI, if you want to build reports dynamically. See AWStats setup documentation (file docs/index.html)
  4. -----> Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf' File awstats.model.conf updated.
  5.  
  6. -----> Need to create a new config file ? Do you want me to build a new AWStats config/profilefile (required if first install) [y/N] ?

这里选Y,创建一个新的配置文件

  1. -----> Define config file name to create
  2. What is the name of your web site or profile analysis ?
  3. Example: www.mysite.com
  4. Example: demo
  5. Your web site, virtual server or profile name:
  6. >nginx #这里输入你要分析的域名,或是随便一个你易记的配置名并回车

接下来要定义你的配置文件存放的路径,可用默认

  1. -----> Define config file path In which directory do you plan to store your config file(s) ?
  2. Default: /etc/awstats
  3. Directory path to store config file(s) (Enter for default):
  4. > #直接回车,使用默认路径/etc/awstats

回车后的提示

  1. -----> Create config file '/etc/awstats/awstats.nginx.conf'
  2. Config file /etc/awstats/awstats.nginx.conf created.
  3. -----> Add update process inside a scheduler Sorry, configure.pl does not support automatic add to cron yet.
  4. You can do it manually by adding the following command to your cron: /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx
  5. Or if you have several config files and prefer having only one command: /usr/local/awstats/tools/awstats_updateall.pl now
  6. Press ENTER to continue...#按回车继续
  7. A SIMPLE config file has been created: /etc/awstats/awstats.nginx.conf You should have a look inside to check and change manually main parameters.
  8. You can then manually update your statistics for 'yuyuanchun.com' with command: > perl awstats.pl -update -config=nginx
  9. You can also build static report pages for 'nginx' with command: > perl awstats.pl -output=pagetype -config=nginx
  10. Press ENTER to finish... #回车完成配置文件的创建

默认会生成一个名为awstats.nginx.conf配置文件在/etc/awstats/目录下,修改该配置文件的日志位置

  1. vi /etc/awstats/awstats.nginx.conf
  2. LogFile="/home/wwwlogs/access_%YYYY-0%MM-0%DD-24.log"

日志说明

  1. # "LogFile" contains the web, ftp or mail server log file to analyze.
  2. # You can also use tags in this filename if you need a dynamic file name
  3. # depending on date or time (Replacement is made by AWStats at the beginning
  4. # of its execution). This is available tags :
  5. # %YYYY-n is replaced with digits year we were n hours ago
  6. # %YY-n is replaced with digits year we were n hours ago
  7. # %MM-n is replaced with digits month we were n hours ago
  8. # %MO-n is replaced with letters month we were n hours ago
  9. # %DD-n is replaced with day we were n hours ago
  10. # %HH-n is replaced with hour we were n hours ago
  11. # %NS-n is replaced with number of seconds at : since
  12. # %WM-n is replaced with the week number in month (-)
  13. # %Wm-n is replaced with the week number in month (-)
  14. # %WY-n is replaced with the week number in year (-)
  15. # %Wy-n is replaced with the week number in year (-)
  16. # %DW-n is replaced with the day number in week (-, =sunday)
  17. # use n= if you need (-, =monday)
  18. # %Dw-n is replaced with the day number in week (-, =sunday)
  19. # use n= if you need (-, =monday)
  20. # Use for n if you need current year, month, day, hour...
  21. n表示时间间隔,我这里就是分析距离当前今天24小时前(昨日)的日志,改成48就是前两天的日志

创建一个awstats用于记录数据的目录

  1. mkdir -p /var/lib/awstats

然后运行awstats的wwwroot目录中的awatsts.pl来测试一下

  1. /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx

如果看到类似下面的提示就说明配置文件都正确了

  1. Create/Update database for config "/etc/awstats/awstats.nginx.conf" by AWStats version 7.0 (build 1.964) From data
  2. in log file "/home/wwwlogs/access.log_20130727"... Phase : First bypass old records, searching new record...
  3. Direct access after last parsed record (after line ) Jumped lines in file: Found already parsed records.
  4. Parsed lines in file:
  5. Found dropped records,
  6. Found comments,
  7. Found blank records,
  8. Found corrupted records,
  9. Found old records,
  10. Found new qualified records

4.Nginx 对 Perl 支持并不好,所以这里利用 awstats 的工具将统计的结果生成静态文件

首先在 webroot 目录下创建一个文件夹。例:/home/www/awstats

  1. mkdir -p /home/www/awstats/nginx

写一个脚本,定期执行让 Awstats 把静态页面生成到该目录中,vi /usr/local/nginx/sbin/awstats.sh

  1. /usr/local/awstats/tools/awstats_buildstaticpages.pl -update
  2. \ -config=nginx -lang=cn -dir=/home/www/awstats/nginx
  3. \ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

/usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats  静态页面生成工具
-update -config=nginx 更新配置项
-lang=cn 语言为中文
-dir=/home/www/awstats 统计结果输出目录
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径

添加脚本执行权限

  1. chmod +x /usr/local/nginx/sbin/awstats.sh

运行脚本如果出现生成一堆网页类似

  1. Launch update process : "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx -update -configdir= ......
  2. Build keywords page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx -staticlinks
  3. -lang=cn -output=keywords Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl"
  4. -config=nginx -staticlinks -lang=cn -output=errors404 files built.
  5. Main HTML page is 'awstats.nginx.html'.

则说明成功了,接下来就可以加入定时任务了crontab -e

  1. * * * /usr/lcoal/nginx/sbin/awstats.sh #每天凌晨1:00执行

5.修改nginx配置文件,添加日志分析结果站点

  1. server
  2. {
  3. listen ;
  4. server_name 192.168.75.28;
  5. index index.html index.htm index.php default.html default.htm default.php;
  6. root /home/wwwroot/awstats;
  7. location ^~ /icon/ {
  8. root /usr/local/awstats/wwwroot;
  9. index index.html;
  10. access_log off;
  11. }
  12. autoindex on;
  13. access_log off;
  14. }

这样访问http://192.168.75.28就可以看到统计结果静态页面了。

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

  1. 烂泥:利用awstats分析nginx日志

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 昨天把nginx的日志进行了切割,关于如何切割nginx日志,可以查看<烂泥:切割 ...

  2. 使用awstats分析nginx日志

    1.awstats介绍 本文主要是记录centos6.5下安装配置awstats,并统计nginx访问日志 1.1 awstats介绍 awstats是一款日志统计工具,它使用Perl语言编写,可统计 ...

  3. Nginx配置Awstats分析Nginx日志笔记

    1.修改Nginx日志格式: log_format json '$remote_addr - $remote_user [$time_local] "$request" '     ...

  4. awstat分析nginx日志

    awstat分析nginx日志 http://lxw66.blog.51cto.com/5547576/1323712 server{ listen ; server_name localhost; ...

  5. elk实战分析nginx日志文档

    elk实战分析nginx日志文档 架构: kibana <--- es-cluster <--- logstash <--- filebeat 环境准备:192.168.3.1 no ...

  6. elk平台分析nginx日志的基本搭建

    一.elk套件介绍 ELK 由 ElasticSearch . Logstash 和 Kiabana 三个开源工具组成.官方网站: https://www.elastic.co/products El ...

  7. 一天,python搞个分析NGINX日志的脚本

    准备给ZABBIX用的. 统计接口访问字次,平均响应时间,4XX,5XX次数 以后可以再改进.. #!/usr/bin/env python # coding: utf-8 ############# ...

  8. 利用python分析nginx日志

    最近在学习python,写了个脚本分析nginx日志,练练手.写得比较粗糙,但基本功能可以实现. 脚本功能:查找出当天访问次数前十位的IP,并获取该IP来源,并将分析结果发送邮件到指定邮箱. 实现前两 ...

  9. hive分析nginx日志之UDF清洗数据

    hive分析nginx日志一:http://www.cnblogs.com/wcwen1990/p/7066230.html hive分析nginx日志二:http://www.cnblogs.com ...

随机推荐

  1. 【每日scrum】NO.2

    1.今天找到了铁大电子地图. 2.需求分析未完成,进度有点慢.

  2. 文档对象模型(DOM)

    文档对象模型(DOM)    DOM可以将任何HTML或XML文档描绘成一个由多层节点构成的结构.节点分为几种不同的类型:文档型节点.元素节点.特性节点.注释节点等共有12种节点类型.DOM1级定义了 ...

  3. elasticsearch中的mapping映射配置与查询典型案例

    elasticsearch中的mapping映射配置与查询典型案例 elasticsearch中的mapping映射配置示例比如要搭建个中文新闻信息的搜索引擎,新闻有"标题".&q ...

  4. matrix_world_final_2011

    C  http://acm.hust.edu.cn/vjudge/contest/view.action?cid=98613#problem/C 题意:输入16进制的n*m矩阵,其在二进制表示下有6种 ...

  5. .NET设计模式(8):适配器模式(Adapter Pattern)(转)

    概述 在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度?这就 ...

  6. Topcoder 多校T-shirt场

    盗用名字:C题题目都没看懂, B:You are given a long long n. Return the largest divisor of n that is a perfect squa ...

  7. git shell 中文

    alias ls="ls --show-control-chars" alias ll="ls -l"

  8. Mac下配置Maven

    1.Java环境变量设置就不说. 但是配置Maven需要检查下Java环境变量的设置.需要检查JAVA_HOME环境变量以及Java命令 wanyakundeMacBook-Pro:Library w ...

  9. const int *p与int *const p的区别(转:csdn,suer0101)

    本文只是一篇学习笔记,是看了<彻底搞定C指针>中的相关篇幅后的一点总结,仅此而已! 一.先搞清const int *p与int const *p的区别 它们的区别就是:没有区别!! 无论谁 ...

  10. 转 wince程序 中使用Listview显示图标问题 (C#) .

    思路: 1.窗体控件:lstaqgl [Listview控件名称]  imageList1[ImageList控件] 2.  图片路径添加到—imageList1——Listview显示图片从 ima ...