edccfw 发表于 2015-11-27 15:33:49

用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 pathlogs_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").logkill-USR1 `cat /usr/local/webserver/nginx/nginx.pid`
设置crontab,每天凌晨00:00切割Nginx日志
?crontab-e输入以下内容:
?10000 * * * /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/softwarecd/data0/softwarewgethttp://ncu.dl.sourceforge.net/project/awstats/AWStats/7.0/awstats-7.0.tar.gztarzxf 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 webserver install Enterfull config file path of your Web server.Example:/etc/httpd/httpd.confExample:/usr/local/apache2/conf/httpd.confExample:c:\Program files\apache group\apache\conf\httpd.confConfigfile path ('none' toskip web server setup):#>none
输入y,创建一个新的配置文件
?Yourweb server config file(s) could not be found.Youwill need to setup your web server manually to declare AWStatsscriptas aCGI, if youwant to build reports dynamically.SeeAWStats setup documentation (file docs/index.html) ----->Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf'Fileawstats.model.conf updated. ----->Need to create a new configfile ?Doyou want me to build a new AWStatsconfig/profilefile(required if firstinstall) ?#>y
输入要统计的域名,(以www.tele6.me为例)
?----->Define config file name to createWhatis thename of your web site or profile analysis ?Example:www.mysite.comExample:demoYourweb site, virtual serveror profile name:#>www.tele6.me
然后一路回车
?----->Define config file pathInwhich directory do youplan to store your config file(s) ?Default:/etc/awstatsDirectorypath to store config file(s) (Enter for default):#> ---->Add update process inside a schedulerSorry,configure.pl does not support automatic add to cron yet.Youcan do itmanually by adding the following command to your cron:/data0/software/awstats-7.0/wwwroot/cgi-bin/awstats.pl-update -config=www.tele6.me    Orif youhave several config files and prefer having only one command:/data0/software/awstats-7.0/tools/awstats_updateall.plnowPressENTER to continue...      ASIMPLE config file has been created: /etc/awstats/awstats.www.tele6.me.conf   Youshould have a look inside to check and change manually main parameters.Youcan then manually update your statistics for 'www.tele6.me' withcommand:>perl awstats.pl -update -config=www.tele6.meYoucan also build static reportpages for 'www.tele6.me' withcommand:>perl awstats.pl -output=pagetype -config=www.tele6.me PressENTER 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/awstatsmkdir-p /data0/htdocs/awstats            # --- web访问的目录
给部分需要执行的文件赋予权限(否则会出现 permission denied 的情况)
?chownroot /data0/software/awstats-7.0/tools/awstats_buildstaticpages.plchmod700/data0/software/awstats-7.0/tools/awstats_buildstaticpages.plchmod+x   /data0/software/awstats-7.0/tools/awstats_buildstaticpages.pl chownroot /data0/software/awstats-7.0/wwwroot/cgi-bin/awstats.plchmod700/data0/software/awstats-7.0/wwwroot/cgi-bin/awstats.plchmod+x   /data0/software/awstats-7.0/wwwroot/cgi-bin/awstats.pl
然后执行
?1/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/iconchmod-R 700 /data0/htdocs/awstats/js
ls -lh 查看一下   /data0/htdocs/awstats   目录的情况,如下图:


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

页: [1]
查看完整版本: 用Awstats对Nginx日志进行分析