wangbin
  • wangbin
  • 2018-06-21
  • IT

用awstats分析nginx日志

一. 简介

awstats可以用来分析nginx日志,通过它我们可以很方便的查看网站访问量、访客来源等信息。

二. 安装

install_awstats.sh

yum -y install epel-release  
yum -y --enablerepo=epel install GeoIP GeoIP-data
yum -y --enablerepo=epel install GeoIP GeoIP-data GeoIP-devel perl-Geo-IP
yum -y --enablerepo=epel install perl-Time-HiRes perl-libwww-perl
yum -y --enablerepo=epel install perl-String-Escape perl-URI-Encode

yum -y install GeoIP GeoIP-data GeoIP-devel perl-Geo-IP
yum -y install perl-Time-HiRes perl-libwww-perl
yum -y install perl-String-Escape perl-URI-Encode

# awstats
cd ~
wget https://prdownloads.sourceforge.net/awstats/awstats-7.7.tar.gz
tar zxvf awstats-7.7.tar.gz
mv awstats-7.7 /usr/local/awstats
ln -fs /usr/local/awstats/tools/nginx/awstats-fcgi.php /usr/local/awstats/wwwroot/cgi-bin/fcgi.php
rm -f ~/awstats-7.7.tar.gz 

三. 配置

执行下面命令,生成默认的配置文件

cd /usr/local/awstats/
/usr/local/awstats/tools/awstats_configure.pl
按提示输入
Do you want me to build a new AWStats config/profile
    y
Do you want me to setup Apache to write 'combined' log files [y/N] ? 
    y
Need to create a new config file ?
    y
Your web site, virtual server or profile name:
    wangbin.io
In which directory do you plan to store your config file(s) ?
    回车,使用默认目录/etc/awstats
    后面继续回车

默认配置文件需要修改下,才能符合我们的需求,我的修改点是:

SiteDomain修改为
SiteDomain="www.wangbin.io"

HostAliases修改为
HostAliases="wangbin.io"

LogFile="/var/log/httpd/mylog.log"
修改为
LogFile="/usr/local/nginx/logs/wangbin.io/access-wangbin.io.log"
说明:这里设置为自己nginx log的路径

LogFormat=1
修改为
# 直连
LogFormat = "%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %other"
# cdn
# LogFormat = "%other %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %host"
说明:
1.直连和LogFormat=1是一样的配置,nginx标准日志格式。网上很多教程提供的日志格式是有问题的,最后使用awast处理后得到的结果不对。使用我这个配置,是没问题的。
2.如果网站使用cdn的话,使用下面的配置,%other和%host对调了下,cdn的源host放到后面。

HostAliases="wangbin.io www.wangbin.io 127.0.0.1 localhost"
修改为
HostAliases="wangbin.io 127.0.0.1 localhost"
说明:
这里设置为自己要分析的域名

DirData="/var/lib/awstats"
修改为
DirData="/vps/hosts/awstats/wangbin.io/wangbin.io"
说明:
awstats生成的结果文件存放的位置

AllowToUpdateStatsFromBrowser=0
修改为
AllowToUpdateStatsFromBrowser=1

AllowFullYearView=2
修改为
AllowFullYearView=3

#LoadPlugin="decodeutfkeys"
修改为
LoadPlugin="decodeutfkeys"

#LoadPlugin="geoip GEOIP_STANDARD /pathto/GeoIP.dat"
修改为
LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"

#LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /pathto/GeoIPCity.dat"
修改为
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/share/GeoIP/GeoIPCity.dat"

四. 命令

awstats处理数据

perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -lang=cn -update -config="/etc/awstats/wangbin.io/wangbin.io.conf"

输出

Create/Update database for config "/etc/awstats/wangbin.io/wangbin.io.conf" by AWStats version 7.7 (build 20180105)
From data in log file "/usr/local/nginx/logs/wangbin.io/access-wangbin.io.log"...
Phase 1 : First bypass old records, searching new record...
Direct access after last parsed record (after line 44)
Jumped lines in file: 44
 Found 44 already parsed records.
Parsed lines in file: 1
 Found 0 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 0 corrupted records,
 Found 0 old records,
 Found 1 new qualified records.

new qualified records有值,说明配置没问题

corrupted record有值,有问题

五. nginx配置

server {

    listen       443 ssl http2;
    listen       [::]:443 ssl http2;

    server_name awstats.wangbin.io;

    # 禁用log
    access_log  off;
    error_log   off;

    ssl_certificate             /vps/certificate/acme/*.wangbin.io/fullchain.cer;
    ssl_certificate_key         /vps/certificate/acme/*.wangbin.io/*.wangbin.io.key;
    ssl_trusted_certificate     /vps/certificate/acme/*.wangbin.io/fullchain.cer;

    # Basic HTTP authentication
    auth_basic "nginx basic http authentication for awstats.wangbin.io";
    auth_basic_user_file /vps/certificate/passwd/htpasswd;

    root /usr/local/awstats/wwwroot;
    index index.html;

    # Static awstats files: HTML files stored in DOCUMENT_ROOT/awstats/
    location /awstats/classes/ {
        alias /usr/local/awstats/wwwroot/classes/;
    }

    location /awstats/css/ {
        alias /usr/local/awstats/wwwroot/css/;
    }

    location /awstats/icon/ {
        alias /usr/local/awstats/wwwroot/icon/;
    }

    location /awstats-icon/ {
        alias /usr/local/awstats/wwwroot/icon/;
    }

    location /awstatsicons/ {
        alias /usr/local/awstats/wwwroot/icon/;
    }

    location /awstats/js/ {
        alias /usr/local/awstats/wwwroot/js/;
    }

    location = / {
        root  /vps/hosts/awstats;
    }

    location = /index.html {
        root  /vps/hosts/awstats;
    }

    # Dynamic stats.
    location ~ ^/cgi-bin/(awredir|awstats)\.pl {
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root/cgi-bin/fcgi.php;
        fastcgi_param X_SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;
        include fastcgi_params;
    }

    # 禁止访问.svn目录,防止svn信息泄漏,必加项
    location ~ ^(.*)\/\.svn\/ {
        deny all;
    }

}

后面就可以到awstats.wangbin.io查看日志分析结果了。

点立即更新可能会提示权限错误,按照提示赋予权限就好啦。

六. 结尾

awstats生成的结果文件有动态和静态两种,我这里用的是动态的方式。

距离最初安装使用awstats过去了一段时间,可能有些问题没有覆盖,后面发现了再添加吧。

PS:最近越来越发现日志的重要,数据分析、定位问题全靠它。