Nginx日志分析解决方案

利用Nginx强大的日志功能可以做非常多的事情,比如点击统计、流量分析、在线人数统计以及其他高级数据挖掘等。

现在简单讲解下日志分析的思路。

 

一,日志的切割

周期性切割日志,比如每天分割一下日志,如果不注意这点日志文件很快就上G,给后续工作带来不便。

分割日志很简单,就是把日志文件复制到别的地方,然后再通知让 Nginx 重新生成日志就可以了。

shell脚本如下:

#!/bin/bash

logs_path="/nginx/logs/" #日志存储路径
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /nginx/logs/nginx.pid`

 

其中 "/nginx/logs" 指的是nginx的log日志文件所在目录,生成的日志文件是以昨天日期命名的。

为了达到每天自动分割的目的,在crontab中加入以下部分:

 

1 0 * * * sh /home/zyf/sh/cut_nginx_log.sh

 

这样就每天的0点1分把nginx日志重命名为日期格式,并重新生成今天的新日志文件。

 

二,Awstats的安装与配置


日志文件分割好了,接下来就是分析了,也就是工具软件Awstats的使用了。

wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz  
tar -zxvf awstats-7.0.tar.gz  
mv awstats-7.0 /usr/local/awstats  
  
#chown -R root:root /usr/local/awstats  
#chmod -R =rwX /usr/local/awstats  
#chmod +x /usr/local/awstats/tools/*.pl  
#chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl  
  
cd /usr/local/awstats/tools  
./awstats_configure.pl  
#现在进入配置文件,按照下面的输入  
  
#  none  (我这里是nginx 不是apache,所以是none)  
#  y    (创建新的一个配置文件)  
#  www.xxxx.com  (基于什么域名)  
#    
#A SIMPLE config file has been created: /etc/awstats/awstats.firefoxbug.conf  
#You should have a look inside to check and change manually main parameters.  
#You can then manually update your statistics for 'firefoxbug' with command:  
#> perl awstats.pl -update -config=firefoxbug  
#You can also build static report pages for 'firefoxbug' with command:  
#> perl awstats.pl -output=pagetype -config=firefoxbug  
  
#-----> Create config file '/etc/awstats/awstats.firefoxbug.conf'  
#                 Config file /etc/awstats/awstats.firefoxbug.conf create  

 

Awstats的配置文件默认会存储在/etc/awstats/目录下,包括你安装时设置的域名如:awstats.www.xxxx.com.conf。

在这个配置文件中修改这个地方:

LogFile="/nginx/logs/access_%YYYY-24%MM-24%DD-24.log" #读取路径下日志文件

 


这个意思是要去读取nginx昨天的日志文件,关于后边%YYYY-24%MM-24%DD-24的设置,规则如下:

 

# You can also use tags in this filename if you need a dynamic file name
# depending on date or time (Replacement is made by AWStats at the beginning
# of its execution). This is available tags :
# %YYYY-n is replaced with 4 digits year we were n hours ago
# %YY-n is replaced with 2 digits year we were n hours ago
# %MM-n is replaced with 2 digits month we were n hours ago
# %MO-n is replaced with 3 letters month we were n hours ago
# %DD-n is replaced with day we were n hours ago
# %HH-n is replaced with hour we were n hours ago
# %NS-n is replaced with number of seconds at 00:00 since 1970
# %WM-n is replaced with the week number in month (1-5)
# %Wm-n is replaced with the week number in month (0-4)
# %WY-n is replaced with the week number in year (01-52)
# %Wy-n is replaced with the week number in year (00-51)
# %DW-n is replaced with the day number in week (1-7, 1=sunday)
# use n=24 if you need (1-7, 1=monday)
# %Dw-n is replaced with the day number in week (0-6, 0=sunday)
# use n=24 if you need (0-6, 0=monday)
# Use 0 for n if you need current year, month, day, hour

 

三,开始分析、生成结果

最后,可以执行分析了。

使用这个命令:

mkdir -p /var/lib/awstats #生成路径文件夹
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.xxxx.com #生成txt文件


这个命令会把结果生成到/var/lib/awstats 目录下 awstatsXXXX.www.XXXX.com.txt文件。

当然这样看起来不太方便,可以再用下面的命令来生成html页面,更加直观。

 

perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -update \ -config=www.xxxxo.com -lang=cn \ -dir=/html/awstats \ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

统计分析完成后,结果还在 Awstats 的数据库中。在 Apache 上,可以直接打开 Perl 程序的网页查看统计。 但Nginx 对 Perl 支持并不好,所以要换个方法,利用 awstats 的工具将统计的结果生成静态文件,具体的步骤如下:

先生成存放awstats生成的静态文件的目录,我这里用的是/html/awstats

mkdir /html/awstats/usr/local/awstats/tools/awstats_buildstaticpages.pl -update  -config=www.xxxx.com -lang=cn -dir=//html/awstats 

 

这样就会在/html/awstats目录下生成很直观的分析结果页。

 最后是nginx的配置文件,根据自己路径配置

server
        {
                listen 80;
                #listen [::]:80 default ipv6only=on;
                server_name 128.awstats.com;
                index index.html index.htm;
                

                #error_page   404   /404.html;
   
        location ~ / {     # awstats  静态页面目录  
        root  /html/awstats; 
        autoindex on; #可以目录浏览你的多个域名的目录用于分析  
        index  index.html;  
        access_log off;  
        }  
      
        location ~ ^/icon/ {             # 图标目录  
        root   /usr/local/awstats/wwwroot;  
        index  index.html;  
        access_log off;  
        }
        }

 

四,自动化

要是每天都去服务器上运行几条命令肯定是件令人烦燥的事情,幸亏Linux的世界里有crontab,

下面是从网友那找的 crontab

 

1 0 * * * sh /home/zyf/sh/cut_nginx_log.sh ##日志的切割

0 1 * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.xxxxke.com ##执行分析 生成txt

0 2 * * * perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.xxxxke.com -lang=cn -dir=/html/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl  
或者
0 2 * * * /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.xxxx.com -lang=cn -dir=//html/awstats
## 生成html 

 

LogFile="/usr/local/nginx/logs/%YYYY-24/%MM-24/expotia2.access_%YYYY-24%MM-24%DD-24.log" ##读取日志文件

##nginx服务器日志默认格式
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

 

posted on 2016-12-19 15:44  小乔流水人家  阅读(185)  评论(0)    收藏  举报

导航