來自:http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx/
劉 柄成 (javayou@gmail.com), 軟件工程師, 廣州市摩網(wǎng)信息技術(shù)有限公司
范 綽耘 (qbanke@163.com), 系統(tǒng)管理員, 廣州市摩網(wǎng)信息技術(shù)有限公司
本文主要介紹如何通過 awstats 來分析 nginx 的訪問日志,并通過 Nginx 輸出分析結(jié)果到瀏覽器。
前言
在我的上一篇文章《使用 Nginx 提升網(wǎng)站訪問速度》中介紹了 Nginx 這個 HTTP 服務(wù)器以及如何通過它來加速網(wǎng)站的訪問速度。在實際的網(wǎng)站運營中,我們經(jīng)常需要了解到網(wǎng)站的訪問情況,例如每天有多少 IP 在訪問、PV 數(shù)是多少、哪個 URL 訪問量最大、用戶使用最多的瀏覽器是哪個、都是通過什么方式知道這個網(wǎng)站的以及有多少用戶訪問出錯等等,通過掌握這些信息來提高用戶的體驗,從而改善網(wǎng)站的質(zhì)量。一般我們可以通過一些免費的訪問統(tǒng)計網(wǎng)站例如 Google Analytics 來或者這些信息。但不足之處是這類網(wǎng)站只能對頁面進(jìn)行分析,不包括靜態(tài)文件;另外可能有很多的站長不愿意使用這類工具來暴露自己的數(shù)據(jù),種種的這些因素使站長希望自己來分析訪問日志。而 awstats 就可以滿足所有的這些需求。
Awstats 是在 SourceForge 上發(fā)展很快的一個基于 Perl 的 WEB 日志分析工具,一個充分的日志分析讓 Awstats 顯示您下列資料:
* 訪問次數(shù)、獨特訪客人數(shù),
* 訪問時間和上次訪問,
* 使用者認(rèn)證、最近認(rèn)證的訪問,
* 每周的高峰時間(頁數(shù),點擊率,每小時和一周的千字節(jié)),
* 域名/國家的主機(jī)訪客(頁數(shù),點擊率,字節(jié),269域名/國家檢測, geoip 檢測),
* 主機(jī)名單,最近訪問和未解析的 IP 地址名單
* 大多數(shù)看過的進(jìn)出頁面,
* 檔案類型,
* 網(wǎng)站壓縮統(tǒng)計表(mod_gzip 或者 mod_deflate),
* 使用的操作系統(tǒng) (每個操作系統(tǒng)的頁數(shù),點擊率 ,字節(jié), 35 OS detected),
* 使用的瀏覽器,
* 機(jī)器人訪問(檢測 319 個機(jī)器人),
* 蠕蟲攻擊 (5 個蠕蟲家族),
* 搜索引擎,利用關(guān)鍵詞檢索找到你的地址,
* HTTP 協(xié)議錯誤(最近查閱沒有找到的頁面),
* 其他基于 URL 的個性報導(dǎo),鏈接參數(shù), 涉及綜合行銷領(lǐng)域目的.
* 貴網(wǎng)站被加入”最喜愛的書簽”.次數(shù).
* 屏幕大小(需要在索引頁補充一些 HTML 標(biāo)簽).
* 瀏覽器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.
* 負(fù)載平衡服務(wù)器比率集群報告.
Awstats 的運行是需要 PERL 環(huán)境的支持,從 awstats 的文檔來看,它對 Apache HTTP Server 的支持是非常完美的,而當(dāng)我們把 Web 服務(wù)器換成 Nginx 后,要運行 awstats 變得很麻煩。首先 Nginx 本身對 Perl 的支持是比較弱的,甚至官方也不建議使用;另外在日志格式上有需要修改后才能運行。
本文主要介紹通過讓 awstats 對日志統(tǒng)計的結(jié)果生成靜態(tài)頁面,然后通過 Nginx 輸出以達(dá)到統(tǒng)計 Nginx 訪問日志的效果,其中還包括如何讓 Nginx 自動切割日志文件。
回頁首
配置 Nginx 自動切割日志
跟 Apache HTTP Server(以下稱 Apache)不同的是,Apache 可以將日志輸出通過管道的方式進(jìn)行重新定向,依此來進(jìn)行自動的日志切割。Nginx 在現(xiàn)今版本上還沒能跟 Apache 一樣,通過%YY等參數(shù)按日期分批創(chuàng)建日志,但是通過給 nginx 進(jìn)程發(fā)送一個特定的信號,可以使 nginx 重新生成日志文件。我們可以定期執(zhí)行一個 Shell 腳本來切換日志,重新命名或轉(zhuǎn)移,具體的腳本如下:
# mv /opt/nginx/logs/access.log /opt/nginx/logs/access_`date +%Y%m%d`.log
# killall –s USR1 nginx #使用USR1參數(shù)通知Nginx進(jìn)程切換日志文件
將以上腳本內(nèi)容保存為文件名為 logcron.sh 存到自定的目錄中,例如 /opt/nginx/sbin/logcron.sh
使用 Crontab 讓該腳本程序在每天晚上 11 點 59 分自動執(zhí)行,即可做到按天創(chuàng)建日志。
回頁首
安裝和配置 Awstats
安裝之前,必須確認(rèn)你的服務(wù)器上 Perl 的環(huán)境已經(jīng)就緒。
查看當(dāng)前環(huán)境 Perl 版本的命令是 perl –version
我們還需要對 Nginx 的日志格式做個小修改,不然 awstats 將無法進(jìn)行統(tǒng)計。
例子如下(加粗部分):
# vi /opt/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
location ~ ^/web/ {
root /data/web;
index index.html;
error_log off;
charset gb2312;
}
log_format new_log #格式代稱 (注意,如果有多個虛擬主機(jī),代稱不能一樣)
‘$remote_addr – $remote_user [$time_local] $request ‘
‘”$status” $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
access_log logs/access.log new_log; #日志生成路徑
}
下載最新版本的 awstats 包,下載地址請見文章最后的參考資料。把下載后的 tar 包解壓到任意目錄中,例: /usr/local/awstats 。然后執(zhí)行 tools 目錄中的 awstats_configure.pl 配置向?qū)?,?chuàng)建一個新的統(tǒng)計。
—–> Check for web server install
Enter full config file path of your Web server.
Example: /etc/httpd/httpd.conf
Example: /usr/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf
Config file path (’none’ to skip web server setup):
#> none #因為我們這里用的是 Nginx,所以寫 none,跳過。
回車
Your web server config file(s) could not be found.
You will need to setup your web server manually to declare AWStats
script as a CGI, if you want to build reports dynamically.
See AWStats setup documentation (file docs/index.html)
—–> Update model config file ‘/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf’
File awstats.model.conf updated.
—–> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ?
#> y #y 創(chuàng)建一個新的統(tǒng)計配置
回車
—–> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
#> www.moabc.net #統(tǒng)計網(wǎng)站的域名 例:
www.moabc.net
回車
—–> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
#>
使用默認(rèn)直接回車,接下來便會出現(xiàn)以下的提示
—-> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.moabc.net
#回頭把該命令填入crontab 按指定時間執(zhí)行
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue… 回車?yán)^續(xù)
A SIMPLE config file has been created: /etc/awstats/awstats.www.moabc.net.conf
#新配置文件所在的路徑
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for ‘www.moabc.net’ with command:
> perl awstats.pl -update -config=www.moabc.net
You can also build static report pages for ‘www.moabc.net’ with command:
> perl awstats.pl -output=pagetype -config=www.moabc.net
Press ENTER to finish…
回車完成向?qū)?,接下來修?www.moabc.net 的統(tǒng)計配置
#vi /etc/awstats/awstats.www.moabc.net.conf
找到統(tǒng)計的日志文件的路徑
LogFile=”/var/log/httpd/mylog.log”
改為
LogFile=”/opt/nginx/logs/access_%YYYY-0%MM-0%DD-0.log
對應(yīng)上邊 Nginx 日志切割程序的所生成的目錄存放結(jié)構(gòu),要注意 Awstats 的年月日格式的跟 Nginx 的寫法有所不同。我們現(xiàn)在執(zhí)行統(tǒng)計的順序是:
Nginx 產(chǎn)生日志 –> 日志切割 –> Nginx 繼續(xù)產(chǎn)生日志 –> 另存切割日志 –> 交由Awstats統(tǒng)計 –> 生成結(jié)果
在本文中 Awstats 所統(tǒng)計的日志,是已切下來的那部分。也能調(diào)轉(zhuǎn)順序,先統(tǒng)計完了再切。不過這比較容易造成統(tǒng)計的遺漏。配置修改完成后,保存退出。然后我們可以開始試一下手動執(zhí)行。
1. 先執(zhí)行日志切割腳本 logcron.sh 把 Nginx 的日志切下來。
2. 然后執(zhí)行 Awstats 日志更新程序開始統(tǒng)計分析。
# /opt/nginx/sbin/logcron.sh
# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.moabc.net
Create/Update database for config “/etc/awstats/awstats.www.moabc.net.conf”
by AWStats version 6.7 (build 1.892)
From data in log file “/opt/nginx/logs/access_20080804.log”…
Phase 1 : First bypass old records, searching new record…
Direct access after last parsed record (after line 450421)
Jumped lines in file: 450421
Found 450421 already parsed records.
Parsed lines in file: 120
Found 0 dropped records,
Found 0 corrupted records,
Found 0 old records,
Found 120 new qualified records.
看到以上顯示,證明日志切割和 Awstats 都已經(jīng)運行無誤了。統(tǒng)計分析完成后,結(jié)果還在 Awstats 的數(shù)據(jù)庫中。在 Apache 上,可以直接打開 Perl 程序的網(wǎng)頁查看統(tǒng)計。但本文開始時已經(jīng)提到,Nginx 對 Perl 支持并不好,所以我們要換個方法,利用 awstats 的工具將統(tǒng)計的結(jié)果生成靜態(tài)文件,具體的步驟如下:
* 首先在 webroot 目錄下創(chuàng)建一個文件夾。例:/data/webroot/awstats
* 然后讓 Awstats 把靜態(tài)頁面生成到該目錄中
# mkdir /data/webroot/awstats
# /usr/local/awstats/tools/awstats_buildstaticpages.pl -update \
-config=www.moabc.net -lang=cn -dir=/data/admin_web/awstats \
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
上述命令的具體意思如下:
* /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 靜態(tài)頁面生成工具
* -update -config=www.moabc.net 更新配置項
* -lang=cn 語言為中文
* -dir=/data/admin_web/awstats 統(tǒng)計結(jié)果輸出目錄
* -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路徑。
接下來,只需在nginx.conf 中,把該目錄配置上去即可。例子如下:(加粗部分):
server {
listen 80;
server_name localhost;
location ~ ^/web/ {
root /data/web;
index index.html;
error_log off;
charset gb2312;
}
location ~ ^/awstats/ { # html 靜態(tài)頁面目錄
root /data/webroot/awstats;
index index.html;
access_log off;
error_log off;
charset gb2312; #最好把默認(rèn)編碼改成 gb2312避免瀏覽器因自動編碼出現(xiàn)亂碼的情況
}
location ~ ^/icon/ { # 圖標(biāo)目錄
root /usr/local/awstats/wwwroot;
index index.html;
access_log off;
error_log off;
charset gb2312;
}
}
用瀏覽器查看到統(tǒng)計的詳細(xì)結(jié)果 http://youhostname/awstats/awstats.www.moabc.net.html
至此,使用 awstats 已能完全支持 Nginx 的日志統(tǒng)計。
回頁首
配置 Awstats 自動運行
為了讓整個日志的統(tǒng)計過程自動完成,我們需要設(shè)置 crontab 計劃任務(wù),讓 Nginx 日志切割以及 Awstats 自動運行,定時生成結(jié)果頁面。
#vi /etc/crontab
11 59 * * * /opt/nginx/sbin/logcron.sh #半夜11:59 進(jìn)行日志切割
00 1 * * * /usr/local/awstats/tools/awstats_buildstaticpages.pl \
-update -config=www.moabc.net -lang=cn -dir=/data/admin_web/awstats \
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
#凌晨00:01 Awstats進(jìn)行日志分析
:wq保存退出
#crontab /etc/crontab 指定cron所執(zhí)行的配置檔路徑
回頁首
保護(hù)日志統(tǒng)計結(jié)果頁面
一般站長都不愿隨便讓人知道自己站的真實流量,所以要把 Awstats 統(tǒng)計結(jié)果頁面進(jìn)行密碼保護(hù)。Nginx 使用的是跟 Apache 一樣的密碼加密格式,這里需要用到 apache 自帶的工具 htpasswd。
如果你在本機(jī)上默認(rèn)裝有 Apache,這你就只需在它的程序目錄下運行
例:
#/usr/local/apache2/bin/htpasswd -c admin.pass admin #用戶名為admin
New password: 輸入密碼
Re-type new password: 重復(fù)輸入
Adding password for user admin 創(chuàng)建成功
然后把 admin.pass 這個密碼包找個的地方藏起來.
修改 nginx.conf 在 location 中加入(加粗部分):
server {
listen 80;
server_name localhost;
location ~ ^/web/ {
root /data/web;
index index.html;
error_log off;
charset gb2312;
}
location ~ ^/awstats/ { # html 靜態(tài)頁面目錄
root /data/admin_web;
index index.html;
access_log off;
error_log off;
charset gb2312;
auth_basic “admin”; #用戶名
/opt/ngx/conf/admin.pass; #密碼包路徑
}
location ~ ^/icon/ { # 圖標(biāo)目錄
root /usr/local/awstats/wwwroot;
index index.html;
access_log off;
error_log off;
charset gb2312;
}
}
修改 Nginx 配置完畢后,執(zhí)行命令 killall –s HUP nginx 讓 Nginx 重新加載配置即可。
回頁首
總結(jié)
盡管跟 Apache HTTP Server 相比較而言,Nginx 的功能是比較弱的,但是我們依然可以利用一些技巧來規(guī)避這些弱點,Nginx 的設(shè)計者肯定也是充分考慮到這個問題?,F(xiàn)在也越來越多的第三方開發(fā)的模塊在逐漸的擴(kuò)展 Nginx 的功能。但是從應(yīng)用本身的角度而言,Nginx 更傾向于卓越的性能,而非大而全的功能,因而在一些附加方面的功能,我們也不能對之要求過高。
回頁首
參考資源
* 開源中國社區(qū):http://www.oschina.net
* Nginx 中文網(wǎng)站:http://www.nginx.cn
* Nginx 英文站點:http://www.nginx.net
* Awstats 網(wǎng)址:http://awstats.sourceforge.net
作者簡介
劉柄成一直使用 J2EE/J2ME 從事移動互聯(lián)網(wǎng)方面的開發(fā)。DLOG4J 的作者,您可以通過 Java 自由人網(wǎng)站來跟他聯(lián)系,網(wǎng)址是:http://www.javayou.com ,另外他的郵件地址是 javayou@gmail.com。
范綽耘,服務(wù)器軟硬件技術(shù)架構(gòu)、開源軟件的熱衷者,現(xiàn)任職廣州市摩網(wǎng)信息技術(shù)有限公司系統(tǒng)管理員,您可以通過開源中國社區(qū)網(wǎng)站 http://www.oschina.net 來聯(lián)系我。