AWStats分析Nginx訪問日誌

    AWStats是在Sourceforge上發展很快的一個基於Perl的WEB日誌分析工具。

    它可以統計您站點的如下信息:

  • 訪問量(UV),訪問次數,頁面瀏覽量(PV),點擊數,數據流量等

  • 精確到每月、每日、每小時的數據

  • 訪問者國家

  • 訪問者IP

  • Robots/Spiders的統計

  • 訪客持續時間

  • 對不同Files type的統計信息

  • Pages-URL的統計

  • 訪客操作系統瀏覽器等信息

  • 其它信息(搜索關鍵字等等)

    下面是AWStats分析Nginx日誌的操作步驟:

一、配置nginx日誌格式

    修改nginx.conf的日誌格式,不然awstats無法分析。

    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 /var/log/nginx/access.log access;

    注意,日誌格式裏的雙引號不能漏了且每個參數之間是一個空格分隔,因爲只要有細微的差別,awstats就無法讀取日誌。

    改好後,使nginx重讀配置文件:

shell# nginx -t && nginx -s reload

二、自動切割nginx日誌

    每天晚上23點59分定時執行一個shell腳本來切割nginx日誌。腳本內容如下:

#!/bin/bash
#
# Filename:    nginxCutLog.sh
# Author:      Qicheng
# Website:     http://qicheng0211.blog.51cto.com/
# Description: 切割nginx日誌
# Notes:       設置crontab,每天23點59分定時執行
#
ROOT_UID=0
if [ "$UID" -ne "$ROOT_UID" ];then
    echo "Error: 必須以root用戶運行此程序!"
    exit 1
fi

nginx_logs_dir="/var/log/nginx"
nginx_pid_file="/var/run/nginx.pid"
# 切割後的日誌文件名,例如access_20141022.log
nginx_log_today="$nginx_logs_dir/access_`date +%Y%m%d`.log"

while [ `date +%S` -ne 59 ];do
        sleep 1
done
sleep 1
[ -f "$nginx_log_today" ] && exit 1
mv $nginx_logs_dir/access.log $nginx_log_today
# 給nginx發送USR1信號,使重新打開新的access.log日誌文件
[ -f $nginx_pid_file ] && /bin/kill -USR1 $(cat $nginx_pid_file)

    設置crontab:

59 23 * * * /bin/bash /yourscriptpath/nginxCutLog.sh

三、安裝awstats

shell# wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz
shell# tar -zxvf awstats-7.0.tar.gz
shell# mv awstats-7.0 /usr/local/awstats
shell# mkdir -p /var/lib/awstats

四、配置awstats

    進入/usr/local/awstats/tools/目錄,執行配置腳本awstats_configure.pl:

shell# cd /usr/local/awstats/tools/
shell# ./awstats_configure.pl

wKiom1RHIZLC7_9AAAb4OHgq8T4716.jpg

    程序執行結束後,會在/etc/awstats/目錄下生成你的配置文件。然後編輯配置文件,修改LogFile參數,跟日誌切割腳本中的日誌路徑對應起來:

LogFile="/var/log/nginx/access_%YYYY-24%MM-24%DD-24.log"

    注意,這裏日期格式“%YYYY-24%MM-24%DD-24”,是指24小時之前的年月日,也就是昨天的日期。

    測試:

shell# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=yourwebsite

    注意:-config=後面的參數是你在執行awstats_configure.pl時輸入的站點域名。

wKiom1RHUo3RAos-AAJFMJnsCX0874.jpg

五、配置awstats生成靜態頁面

    利用awstats的工具將統計的結果生成靜態文件:

shell# mkdir -p /var/www/awstats
shell# /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=yourwebsite -lang=cn -dir=/var/www/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

    注意:-config=後面的參數是你在執行awstats_configure.pl時輸入的站點域名;-dir=是統計結果靜態文件的輸出目錄。

    設置crontab,每天凌晨00:01定時更新靜態頁面:

1 0 * * * /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=yourwebsite -lang=cn -dir=/var/www/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

    一般站長都不願隨便讓人知道自己站的真實流量,所以要把awstats統計結果頁面進行密碼保護,這裏需要用到apache自帶的工具htpasswd:

shell# yum -y install httpd-tools
shell# htpasswd -cd admin.pass admin
New password: 
Re-type new password: 
Adding password for user admin

    把生成的密碼文件admin.pass放到nginx主配置目錄下(/etc/nginx/)。

    在nginx配置擴展目錄(/etc/nginx/conf.d/)下新建awstats.conf配置文件,內容如下:

server {
    listen       83;
    server_name  localhost;
    location ~ ^/awstats/ {     # html 靜態頁面目錄
        root   /var/www;
        index  index.html;
        access_log off;
        error_log off;
        charset gb2312;
        auth_basic "admin";
        auth_basic_user_file admin.pass;
    }

    location ~ ^/icon/ {        # 圖標目錄
        root   /usr/local/awstats/wwwroot;
        index  index.html;
        access_log off;
        error_log off;
    }

    location / {
        return 403;
    }
}

最後使nginx重讀配置文件,用瀏覽器查看統計結果 http://yourhostname:83/awstats/awstats.yourwebsite.html

日誌分析頁面示例:

wKiom1RHSUKylF9SAAWotBFTXts481.jpg


參考資料:

http://baike.baidu.com/view/228748.htm?fr=aladdin

http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx/(有些錯誤,已在本文中糾正)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章