昨天产品提出要求,需要查看每天的uv数。awstats默认是按月统计的,所以无法查看uv数。下面介绍一下nginx做awstats的web容器的实现方法,有些是网上搜集的,有些是自己加进去的,希望能帮到有需求的朋友。 一、Awstats安装配置 详细安装步骤这里不再赘述,如有需要请参考博客另一篇文章:awstats+jawstats构建日志自动分析系统 1、安装 [iyunv@iredmail ~]# cd /opt [iyunv@iredmail opt]# wget http://sourceforge.net/projects/ ... wstats-7.1.1.tar.gz [iyunv@iredmail opt]# tar -zxf awstats-7.1.1.tar.gz [iyunv@iredmail opt]# mv awstats-7.1.1 /usr/local/awstats [iyunv@iredmail opt]# chown -R root:root /usr/local/awstats [iyunv@iredmail opt]# chmod +x /usr/local/awstats/tools/*.pl [iyunv@iredmail opt]# chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl [iyunv@iredmail opt]# cd /usr/local/awstats/tools [iyunv@iredmail tools]# ./awstats_configure.pl
1.输入 none 然后回车 2.输入 y 确认创建配置文件 3.输入配置文件名称,一般输入域名,这里是以slog.fity.cn为案例 4.配置文件使用默认路径 /etc/awstats 5.按回车继续 6.按回车完成配置文件的创建 2、修改awstats配置文件 [iyunv@iredmail tools]# vi /etc/awstats/awstats.slog.fity.cn.conf#如果有多个站点需要统计,请配置多个配置文件 LogFile="/htdoc/logs/2013/05/access_20130526.log"//指定log文件的存储位置
如果你希望每天能定时生成昨天的报表,可以: LogFile="/htdoc/logs/%YYYY-24/%MM-24%/access_%YYYY-24%MM-24%DD-24.log"
注:根据你的日志路径和对应的日志文件名进行修改。对应Nginx日志切割程序所生成的目录存放结构保存的nginx日志文件。要注意Awstats的年月日格式的跟Nginx的写法有所不同,其中%YYYY-24/%MM-24/%DD-24表示年月日都减去24小时,也就是昨天的日志目录。 3、生成Awstats统计结果静态页面 [iyunv@iredmail awstats]# mkdir -p /htdoc/awstats/html #创建存放结果的目录
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=slog.fity.cn -lang=cn -dir=/htdoc/awstats/html -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl 说明: /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 静态页面生成工具 -update -config=slog.fity.cn 更新配置项,指定配置文件 -lang=cn 语言为中文 -dir=指定静态页面的存储位置 -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径
二、Nginx配置及URL地址简化 1、配置Nginx 从Awstats提供的工具包中我们看到Awstats已经提供了在Nginx环境Awstats的安装配置,Nginx主机范例文件: [iyunv@iredmail awstats]#ls /usr/local/awstats/tools/nginx/ awstats-fcgi.phpawstats-nginx.confREADME.txt
这里我们先把awstats-fcgi.PHP复制一份到/usr/local/awstats/wwwroot/cgi-bin/fcgi.php [iyunv@iredmail awstats]# cd /usr/local/awstats/tools/nginx/ [iyunv@iredmail nginx]# cp -a awstats-fcgi.php /usr/local/awstats/wwwroot/cgi-bin/fcgi.php
然后创建Nginx虚拟主机配置文件(可以参考Awstats提供的示例文件awstats-nginx.conf)这里未来往事给出一个本次测试环境下Nginx的vhost完整配置: server { listen 80; server_name slog.fity.cn; #access_log /var/log/nginx/localhost.access_log main; #error_log /var/log/nginx/localhost.error_log info; root /htdoc/awstats/html; index index.html;
# Restrict access #auth_basic "Restricted"; #auth_basic_user_file /etc/awstats/htpasswd;
location ~ ^/cgi-bin/.*.(cgi|pl|py|rb) { gzip off; includefastcgi_params; fastcgi_pass127.0.0.1:9000;#注意配置fastcgi_pass为你的php-fpm server. fastcgi_indexcgi-bin.php; fastcgi_paramSCRIPT_FILENAME /usr/local/awstats/wwwroot/cgi-bin/fcgi.php;#注意文件路径 fastcgi_paramSCRIPT_NAME/cgi-bin/fcgi.php; fastcgi_paramX_SCRIPT_FILENAME /usr/local/awstats/wwwroot$fastcgi_script_name;#注意文件路径 fastcgi_paramX_SCRIPT_NAME $fastcgi_script_name; fastcgi_paramREMOTE_USER$remote_user; }
# Static awstats files: HTML files stored in DOCUMENT_ROOT/awstats/ location /classes/ { alias /usr/local/awstats/wwwroot/classes/; }
location /css/ { alias /usr/local/awstats/wwwroot/css/; }
location /js/ { alias /usr/local/awstats/wwwroot/js/; } location /icon/ { alias /usr/local/awstats/wwwroot/icon/; }
}
2、常见错误: 页面错误信息:File not found. 检查nginx调用的文件是否有读取权限,例如/usr/local/awstats/wwwroot/下的相关文件Nginx是否有权限读取。未来往事遇到该故障是由于Nginx没有读取/usr/local/awstats/wwwroot/cgi-bin/fcgi.php文件的权限。 页面错误信息:Page not found. 检查Nginx配置文件中是否有参数错误。 备注:为了避免以上两处错误,未来往事建议你把该目录下/usr/local/awstats/wwwroot/使用到的文件复制一份到你Nginx中配置的主机目录中,然后适当的修改下Nginx配置文件中的路径地址即可。 3、状态测试 至此,我们便可以通过如下url方式访问日志统计页面了:http://slog.fity.cn/cgi-bin/awstats.pl?config=slog.fity.cn 4、简化URL地址 原始地址:http://slog.fity.cn/cgi-bin/awstats.pl?config=slog.fity.cn
简化后的地址:http://slog.fity.cn/slog.fity.cn rewrite规则: location ~ ^/([a-z0-9-_.]+)$ { return 301 $scheme://slog.fity.cn/cgi-bin/awstats.pl?config=$1; }
5、利用密码保护统计数据 #htpasswd -c /etc/awstats/awstats.alpha.com.htpasswd username
参考:《HttpAuthBasicModule》 # Protect each config with a different credential if ($args ~ "config=([a-z0-9-_.]+)") { set $domain $1; }
auth_basic"Admin"; auth_basic_user_file/etc/awstats/awstats.$domain.htpasswd;
gzip off; includefastcgi_params; fastcgi_pass127.0.0.1:9000;#注意配置fastcgi_pass为你的php-fpm server. fastcgi_indexcgi-bin.php; fastcgi_paramSCRIPT_FILENAME /usr/local/awstats/wwwroot/cgi-bin/fcgi.php;#注意文件路径 fastcgi_paramSCRIPT_NAME/cgi-bin/fcgi.php; fastcgi_paramX_SCRIPT_FILENAME /usr/local/awstats/wwwroot$fastcgi_script_name;#注意文件路径 fastcgi_paramX_SCRIPT_NAME $fastcgi_script_name; fastcgi_paramREMOTE_USER$remote_user; }
6、最终的配置文件如下: server { listen 80; server_name slog.fity.cn; root /htdoc/awstats/html; index index.html;
error_log /var/log/nginx/awstats.error.log; access_log off; log_not_found off;
location ~ ^/([a-z0-9-_.]+)$ { return 301 $scheme://slog.fity.cn/<cgi-></cgi->bin/awstats.pl?config=$1; }
location ~ ^/cgi-bin/.*.(cgi|pl|py|rb) { if ($args ~ "config=([a-z0-9-_.]+)") { set $domain $1; }
auth_basic"Admin"; auth_basic_user_file/etc/awstats/awstats.$domain.htpasswd;
gzip off; includefastcgi_params; fastcgi_pass127.0.0.1:9000;#注意配置fastcgi_pass为你的php-fpm server. fastcgi_indexcgi-bin.php; fastcgi_paramSCRIPT_FILENAME /usr/local/awstats/wwwroot/cgi-bin/fcgi.php;#注意文件路径 fastcgi_paramSCRIPT_NAME/cgi-bin/fcgi.php; fastcgi_paramX_SCRIPT_FILENAME /usr/local/awstats/wwwroot$fastcgi_script_name;#注意文件路径 fastcgi_paramX_SCRIPT_NAME $fastcgi_script_name; fastcgi_paramREMOTE_USER$remote_user; }
location /classes/ { alias /usr/local/awstats/wwwroot/classes/; }
location /css/ { alias /usr/local/awstats/wwwroot/css/; }
location /js/ { alias /usr/local/awstats/wwwroot/js/; } location /icon/ { alias /usr/local/awstats/wwwroot/icon/; }
}
三、配置Awstats按日、按月归档日志统计结果 这里需要用到2个JS文件,见附件! 安装/使用: 下载完成并解压,可以看到如下三个文件: day-by-day-head.js day-by-day-end.js readme.txt
把day-by-day-head.js、day-by-day-end.js复制到站点/usr/local/awstats/wwwroot/js目录下,如果没有没有js目录可自行建立(当然这里的路径你需要修改为你的nginx虚拟主机路径) 修改Awstats配置文件(/etc/awstats/*)添加: HTMLHeadSection="<script language=javascript src="/js/day-by-day-head.js"></script>" HTMLEndSection="<script language=javascript src="/js/day-by-day-end.js"></script>"
更新日志数据时记得加上-databasebreak参数 /usr/local/awstats/tools/awstats_buildstaticpages.pl-update-config=slog.fity.cn -databasebreak=day -lang=cn -dir=/htdoc/awstats/html -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl /usr/local/awstats/tools/awstats_buildstaticpages.pl-update-config=slog.fity.cn -databasebreak=month -lang=cn -dir=/htdoc/awstats/html -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
至此,我们便可以通过如下url方式访问按年月日归档的日志统计页面了:http://slog.fity.cn/slog.fity.cn (如果你在Nginx配置文件中没有添加URL重写规则,请通过类似地址访问http://slog.fity.cn/cgi-bin/awstats.pl?config=slog.fity.cn) 最终效果如图:
|