用Awstats对Nginx日志进行分析

Nginx是一款性能很好的web服务程序,已经越来越多的被人使用。

安装方法:http://blog.s135.com/nginx_php_v6/

今天我想说说,如何分析Nginx日志,进而对用户习惯进行分析。

对用户的访问习惯进行分析,是网站运营中比较重要的环节。

我使用Awstats来分析Nginx日志。

Awstats默认支持Apache和IIS(见 http://www.chedong.com/tech/awstats.html ),需要设置一下才能分析Nginx日志。另外Nginx不支持Perl,而Awstats是Perl写的,所以需要一点变通的方法才能使用。

------------------------------------------------------------------


一 :设置Nginx日志格式、分割办法(以便后面Awstats读取)

 

安装过程就不介绍了,只要安装张宴同学的教程安装:http://blog.s135.com/nginx_php_v6/,就可以了。

重点说一下Nginx日志格式和分割日志的办法。

 

设置Nginx的日志格式为:

 log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
 access_log  /data1/logs/access.log  access;

 

分割日志办法:

创建脚本/usr/local/webserver/nginx/sbin/cut_nginx_log.sh

vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh

输入以下内容:

#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="/usr/local/webserver/nginx/logs/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`

 

设置crontab,每天凌晨00:00切割Nginx日志

crontab -e

输入以下内容:

00 00 * * * /bin/bash  /usr/local/webserver/nginx/sbin/cut_nginx_log.sh

 

这样每天零点准时切割Nginx日志,以日期为文件名保存。

例如:/data1/logs/2010/07/access_20100721.log

 

------------------------------------------------------------------


二 :安装配置 Awstats

 

建立Software目录,下载Awstats源码,然后解压。

mkdir -p /data0/software
cd /data0/software
wget http://ncu.dl.sourceforge.net/project/awstats/AWStats/7.0/awstats-7.0.tar.gz
tar zxf awstats-7.0.tar.gz

 

检查Perl版本

perl –version

 

执行 awstats-7.0/tools 目录中的 awstats_configure.pl 配置向导,创建统计。

perl /data0/software/awstats-7.0/tools/awstats_configure.pl

 

因为用的是 Nginx,输入 none

-----> Check for web server install

Enter full config file path of your Web server.
Example: /etc/httpd/httpd.conf
Example: /usr/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf
Config file path ('none' to skip web server setup):
#> none

 

输入y,创建一个新的配置文件

Your web server config file(s) could not be found.
You will need to setup your web server manually to declare AWStats
script as a CGI, if you want to build reports dynamically.
See AWStats setup documentation (file docs/index.html)

-----> Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf'
  File awstats.model.conf updated.

-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ?
#> y

 

输入要统计的域名,(以www.tele6.me为例)

-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
#> www.tele6.me

 

然后一路回车

-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
#> 

----> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/data0/software/awstats-7.0/wwwroot/cgi-bin/awstats.pl -update -config=www.tele6.me  


Or if you have several config files and prefer having only one command:
/data0/software/awstats-7.0/tools/awstats_updateall.pl now
Press ENTER to continue...		

A SIMPLE config file has been created: /etc/awstats/awstats.www.tele6.me.conf  

You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.tele6.me' with command:
> perl awstats.pl -update -config=www.tele6.me
You can also build static report pages for 'www.tele6.me' with command:
> perl awstats.pl -output=pagetype -config=www.tele6.me

Press ENTER to finish...

 

回车完成安装,然后修改awstats的配置文件:

vi /etc/awstats/www.tele6.me.conf

 

 

查找文件中,修改 日志格式LogFormat 和 日志目录结构LogFile

LogFile="/data1/logs/%YYYY-24/%MM-24/access_%YYYY-24%MM-24%DD-24.log"

LogFormat = "%host %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %other"

 

创建Awstats数据目录,生成静态文件的目录

mkdir -p /var/lib/awstats
mkdir -p /data0/htdocs/awstats              # --- web访问的目录

 

给部分需要执行的文件赋予权限(否则会出现 permission denied 的情况)

chown root /data0/software/awstats-7.0/tools/awstats_buildstaticpages.pl
chmod 700  /data0/software/awstats-7.0/tools/awstats_buildstaticpages.pl
chmod +x   /data0/software/awstats-7.0/tools/awstats_buildstaticpages.pl

chown root /data0/software/awstats-7.0/wwwroot/cgi-bin/awstats.pl
chmod 700  /data0/software/awstats-7.0/wwwroot/cgi-bin/awstats.pl
chmod +x   /data0/software/awstats-7.0/wwwroot/cgi-bin/awstats.pl

 

然后执行

/data0/software/awstats-7.0/tools/awstats_buildstaticpages.pl -update -config=www.tele6.me -lang=cn -dir=/data0/htdocs/awstats/ -awstatsprog=/data0/software/awstats-7.0/wwwroot/cgi-bin/awstats.pl 

 

这样,一切正常的话就会在  /data0/htdocs/awstats  目录中生成一系列静态文件

还需要把一些图片文件和js文件,copy到 /data0/htdocs/awstats 中 并赋予访问权限

cp -a /data0/software/awstats-7.0/wwwroot/icon/ /data0/htdocs/awstats/icon/
cp -a /data0/software/awstats-7.0/wwwroot/js/ /data0/htdocs/awstats/js/

chmod -R 700 /data0/htdocs/awstats/icon
chmod -R 700 /data0/htdocs/awstats/js

 

ls -lh 查看一下   /data0/htdocs/awstats   目录的情况,如下图:

 

完成了。

可以访问刚才生成在  /data0/htdocs/awstats  目录里的静态文件看看效果了。

 

参考资料:

http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx/index.html

posted @ 2010-07-28 22:11  居小齐  阅读(2246)  评论(0编辑  收藏  举报