Web Digest

还有众多网络文摘,仅供个人收藏和参考

Posts Tagged ‘awstats

为基于Nginx的Awstats添加fail2ban保护

leave a comment »

1. 首先,假设已经按照前文,将awstats的出错日志输出到/var/log/nginx/awstats_error.log中

2. 进入/etc/fail2ban/filter.d/目录,创建nginx-auth.conf文件,以下是其内容


[Definition]

# Option:  failregex

# Notes.:  regex to match the password failure messages in the logfile. The

#          host must be matched by a group named "host". The tag "<HOST>" can

#          be used for standard IP/hostname matching and is only an alias for

#          (?:::f{4,6}:)?(?P<host>\S+)

# Values:  TEXT

#

failregex = no user/password.*client\: <HOST>

user.*authentication failure.*client\: <HOST>

user.*not found.*client\: <HOST>

user.*password mismatch.*client\: <HOST>

# Option:  ignoreregex

# Notes.:  regex to ignore. If this regex matches, the line is ignored.

# Values:  TEXT

#

ignoreregex =

3. 编辑/etc/fail2ban/jail.local文件, 添加以下内容:

[awstats]

enalbed = true

port = http,https

logpath = /var/log/nginx/awstats_error.log

filter = nginx-auth
4. 最后重启服务即可,sudo /etc/init.d/fail2ban restart

Written by admin

December 28, 2009 at 11:31 pm

Posted in 网站|Site

Tagged with , ,

让AWStats的访问记录支持UTF-8的URL

leave a comment »

AWstats和Google Analytics目前都不支持用Title显示URL信息,以Drupal网站为例,经常看到一堆node/1, term/32之类没有意义的东西出现的排行榜,或是出口、入口地址,非要点击过去才能知道是什么。

所以最好利用Path Auto模块自动生成URL Alias,把Title自动映射为URL,便于在日志统计分析中一目了然。

可是,结果出现的确是以%B4%A3这样的Plain Text。

为此研究了一下awstats.pl的源代码,修改一下ShowURLInfo函数:

前几行改为

my $nompage;
if ($PluginsLoaded{‘DecodeURL’}{‘decodeutfurl’}) {
$nompage = CleanXSS(
DecodeURL_decodeutfurl($url, $PageCode || ‘gb2312’));
}
else {
$nompage = CleanXSS($url);
}

然后修改Awstats的配置文件,加入

LoadPlugin=”decodeutfurl.pm”

并且把这个decodeutfurl.pm文件放到/plugin目录下,就应该OK了。

刷新一下看看,enjoy。

现在AWStats除了无法显示单个用户访问明细,其他的效果还是不错的

Written by admin

November 12, 2009 at 11:38 am

Posted in 网站|Site

Tagged with , ,

为AWStats加装jAWStats

leave a comment »

首先,目录的安全性设置不好,解压缩后,需要在当前目录下

find . -type -d -print | xargs chmod 755

find . -type -f -print | xargs chmod 644

我把它放到/usr/local/awstats/wwwroot/jawstats下,并相应配置NGINX,添加一个php的入口

其他的都可以参照文档,唯一发现问题是启动后白屏,查一查error.log说undefined function Lang()。

搜了一下论坛,发现是个已知的Bug,不知道怎么又出现在发行版。

vim一下 index.php, shift-g末尾,用问号向前搜<?, 第一个改为<?php, 保存退出,刷新就OK了。

Written by admin

November 11, 2009 at 11:25 am

Posted in 网站|Site

Tagged with ,

为AWStats增加GeoIP模块,QQ纯真IP地址库和其他功能

with 10 comments

1. 为了支持UTF decode

perl -MCPAN -e ‘install “Encode”‘
perl -MCPAN -e ‘install “URI::Escape”‘

然后可以打开LoadPlugin=”decodeutfkeys”

2. 打开LoadPlugin="tooltips", 这个不需要添加而外的Perl 模块了

3. 去maxmind.com上下载最新的GeoIP C API, 并编译安装,虽然出于安全考虑,一般不愿意编译非Ubuntu源的第三方程序,但考虑它是开源,又广为传播,其模块甚至写道了awstats的默认配置文件了,应该不会有太大问题。此外FreeBSD也有了Port,那就随大流吧。

cd /usr/local/src
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
tar zxf GeoIP.tar.gz
cd GeoIP-1.4.6
./configure
make
make install

Install后,就在/usr/local/share/GeoIP下生成了GeoIP.dat
然后下载最新的3个IP库

cd /usr/local/share/GeoIP/
wget -t 5 http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget -t 5 http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget -t 5 http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
gzip -d *

再安装必须的Perl模块

perl -MCPAN -e 'install "Geo::IP"'

最后修改Awstatus配置文件

LoadPlugin="geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/local/share/GeoIP/GeoLiteCity.dat"
LoadPlugin="geoip_org_maxmind GEOIP_STANDARD /usr/local/share/GeoIP/GeoIPASNum.dat"
#百万级以上网址使用:LoadPlugin="hashfiles"

4. 加载QQIP地址库

这里要用到2个第三方的Perl脚本
http://www.ieasy.org/download/qqhostinfo.pm
http://www.ieasy.org/download/qqwry.pl

看了一下源代码,比较简单,应该没有安全问题,也可以去刘辉的blog下载这两个文件

下载完成后,将文件保存到/usr/local/awstats/wwwroot/cgi-bin/plugin下

修改qqwry.pl,把

my $ipfile=”./QQWry.Dat”;
修改为:
my $ipfile=”/usr/local/share/GeoIP/QQWry.Dat”;

然后

cd /usr/local/share/GeoIP
wget http://update.cz88.net/soft/qqwry.rar

apt-get install unrar
unrar e qqwry.rar

在把除了QQWry.dat之外的文件全部删除

在/etc/awstats/awstats.yoursite.com.conf中,添加

LoadPlugin=”qqhostinfo”

应该就可以了

6. 创建GeoIP自动更新脚本

在/usr/local/share/GeoIP下,创建GeoIP_update.sh

#!/bin/sh

wget -q -t 5 -O /usr/local/share/GeoIP/GeoIP.dat.gz http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget -q -t 5 -O /usr/local/share/GeoIP/GeoLiteCity.dat.gz http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget -q -t 5 -O /usr/local/share/GeoIP/GeoIPASNum.dat.gz http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz

gzip -df /usr/local/share/GeoIP/GeoIP.dat.gz
gzip -df /usr/local/share/GeoIP/GeoLiteCity.dat.gz
gzip -df /usr/local/share/GeoIP/GeoIPASNum.dat.gz

然后 chmod +x GeoIP_update.sh

crontab -uwww-data -e

27 3 5 * * /usr/local/share/GeoIP/GeoIP_update.sh

在每月5日3点21分,自动更新IP数据库

Written by admin

November 10, 2009 at 1:13 pm

Posted in 网站|Site

Tagged with ,

为NGINX配置AWStats日志分析

leave a comment »

虽然AWStats现在已经支持输出静态网页形式的分析结果,但是页面布局没有用Perl生成的动态网页方便,而且用自带的用Perl编写的转换工具稍微有点慢,在流量不大的情况下,还是在Perl+Fast CGI上运行更舒服一些。

首先要参考之前的文章,建立好PERL+FCGI的运行环境,这步准备工作就够费劲的。

然后开始切入正题:

1. 下载最新版的AWStats,基本就是Perl包,所以没必要用apt-get,解压缩到/usr/local/awstats下

wget http://prdownloads.sourceforge.net/awstats/awstats-6.95.tar.gz
tar -xzf awstats-6.95.tar.gz
mv awstats-6.95 /usr/local/awstats

2. 创建一个存放awstats分析数据的目录
mkdir /var/lib/awstats
chown www-data /var/lib/awstats  //这是为了让awstats页面上能直接刷新最新数据

3. 自动配置awstats

cd /usr/local/awstats/tools
perl awstats_configure.pl

除了第一步因为是Ngin服务器的关系,所以要选none,其他基本按照提示选默认值

4. 手工编辑配置文件

1) 修改LogFile路径

LogFile = “/var/log/nginx/access.log”

如果是压缩格式的日志,可以用LogFile = “zcat /var/log/nginx/%YYYY-24%MM-24%DD-24.gz|"。这里用zcat是因为其使用管道输出,对系统资源消耗比较小,千万不要忘了最后的管道操作符!

假设原来/etc/nginx/nginx.conf中关于log部分是如此定义的:(要小心各个变量之间必须添加的空格,不能少,否则awstats就不认了

log_format  main  ‘$remote_addr $remote_user [$time_local] “$request” $status ‘
‘$host $body_bytes_sent $gzip_ratio “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;

很容易知道,对应awstats配置文件中,LogFormat应该设置为
LogFormat= “%host %logname %time1 %methodurl %code %host_r %bytesd  %gzipratio %refererquot %uaquot %otherquot”

最后一个选%otherquot是应为$http_x_forwarded_for在AWstats 6.95中还不认识

3) 将AllowToUpdateStatsFromBrowser=1,便于浏览器中即时刷新数据

5. 更新Awstats的分析记录,测试一下刚才的配置
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=yousite.com

如果一切正常,应该看到类似以下的结果:
Create/Update database for config “/etc/awstats/awstats.yoursite.com.conf” by AWStats version 6.95 (build 1.943)
From data in log file “/var/log/nginx/access.log”…
Phase 1 : First bypass old records, searching new record…
Searching new records from beginning of log file…
Phase 2 : Now process new records (Flush history on disk after 20000 hosts)…
Jumped lines in file: 0
Parsed lines in file: 1234
Found 0 dropped records,
Found 0 corrupted records,
Found 0 old records,
Found 1234 new qualified records.

6. 修改logrotate.d下的nginx配置文件,在每天切割日志前,更新awstats状态

/var/log/nginx/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 640 www-data www-data
dateext
sharedscripts
prerotate
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=yoursite.com
sleep 59
endscript
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}

7. 接下来是最关键的NGINX配置,

#AWStatus Perl CGI  server
server {
listen       80;
server_name  awstats.yoursite.com;
access_log   /var/log/nginx/awstats.log main;

error_log    /var/log/nginx/awstats_error.log; #这可以为fail2ban留下记录
root /usr/local/awstats/wwwroot;
auth_basic            “Restricted”;
auth_basic_user_file  /etc/nginx/awstatus.pass;

location = / {
rewrite ^ /awstats.pl?config=freshventure.info;
}

location ~ .*(\.cgi|\.pl?)$ {
gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
root /usr/local/awstats/wwwroot/cgi-bin;
fastcgi_pass 127.0.0.1:8000;
fastcgi_index awstats.pl;
fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
}

}

好了,测试一下http://awstats.yoursite.com,和前文一样,输入密码后,这次看到的应该就awstats的界面了

8. 如果需要配置静态页面,则可以在logrotate中替换掉上面的awstats.pl, 换成

/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=yoursite.com -lang=cn -dir=/usr/local/awstats/wwwroot -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl。

如果需要更频繁的更新访问情况,则可以把这句命令加入到crontab中,例如crontab -e -uwww-data。

然后nginx相应调整为显示静态网页就可以了

参考文章:

1. 刘辉 - awstats 配置模板 参考

2. 刘辉 - AWStats:示来访者位置 QQ纯真库IP解析插件安装

3. Jeff’s Blog – Awstats安装和设置

4. Jeff’s Blog – NGINX下运行Awstats

5. Jeff’s Blog – IP地址数据库的自动更新

6. 使用 awstats 分析 Nginx 的访问日志

今后拓展阅读

1. 车东 - AWStats的千万级日志解决方案

wget http://prdownloads.sourceforge.net//-6.9.tar.gz
tar -xvzf -6.9.tar.gz
mv -6.9 /usr/local/

Written by admin

November 10, 2009 at 10:38 am

Posted in 网站|Site

Tagged with , ,