歡迎您光臨本站 註冊首頁

使用 awstats 分析 Nginx 的訪問日誌

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  

在我的上一篇文章《使用 Nginx 提升網站訪問速度》中介紹了 Nginx 這個 HTTP 伺服器以及如何通過它來加速網站的訪問速度。在實際的網站運營中,我們經常需要了解到網站的訪問情況,例如每天有多少 IP 在訪問、PV 數是多少、哪個 URL 訪問量最大、用戶使用最多的瀏覽器是哪個、都是通過什麼方式知道這個網站的以及有多少用戶訪問出錯等等,通過掌握這些信息來提高用戶的體驗,從而改善網站的質量。一般我們可以通過一些免費的訪問統計網站例如 Google Analytics 來或者這些信息。但不足之處是這類網站只能對頁面進行分析,不包括靜態文件;另外可能有很多的站長不願意使用這類工具來暴露自己的數據,種種的這些因素使站長希望自己來分析訪問日誌。而 awstats 就可以滿足所有的這些需求。

Awstats 是在 SourceForge 上發展很快的一個基於 Perl 的 WEB 日誌分析工具,一個充分的日誌分析讓 Awstats 顯示您下列資料:

  • 訪問次數、獨特訪客人數,
  • 訪問時間和上次訪問,
  • 使用者認證、最近認證的訪問,
  • 每周的高峰時間(頁數,點擊率,每小時和一周的千位元組),
  • 域名/國家的主機訪客(頁數,點擊率,位元組,269域名/國家檢測, geoip 檢測),
  • 主機名單,最近訪問和未解析的 IP 地址名單
  • 大多數看過的進出頁面,
  • 檔案類型,
  • 網站壓縮統計表(mod_gzip 或者 mod_deflate),
  • 使用的操作系統 (每個操作系統的頁數,點擊率 ,位元組, 35 OS detected),
  • 使用的瀏覽器,
  • 機器人訪問(檢測 319 個機器人),
  • 蠕蟲攻擊 (5 個蠕蟲家族),
  • 搜索引擎,利用關鍵詞檢索找到你的地址,
  • HTTP 協議錯誤(最近查閱沒有找到的頁面),
  • 其他基於 URL 的個性報導,鏈接參數, 涉及綜合行銷領域目的.
  • 貴網站被加入"最喜愛的書籤".次數.
  • 屏幕大小(需要在索引頁補充一些 HTML 標籤).
  • 瀏覽器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.
  • 負載平衡伺服器比率集群報告.

Awstats 的運行是需要 PERL 環境的支持,從 awstats 的文檔來看,它對 Apache HTTP Server 的支持是非常完美的,而當我們把 Web 伺服器換成 Nginx 后,要運行 awstats 變得很麻煩。首先 Nginx 本身對 Perl 的支持是比較弱的,甚至官方也不建議使用;另外在日誌格式上有需要修改後才能運行。

本文主要介紹通過讓 awstats 對日誌統計的結果生成靜態頁面,然後通過 Nginx 輸出以達到統計 Nginx 訪問日誌的效果,其中還包括如何讓 Nginx 自動切割日誌文件。





配置 Nginx 自動切割日誌

跟 Apache HTTP Server(以下稱 Apache)不同的是,Apache 可以將日誌輸出通過管道的方式進行重新定向,依此來進行自動的日誌切割。Nginx 在現今版本上還沒能跟 Apache 一樣,通過%YY等參數按日期分批創建日誌,但是通過給 nginx 進程發送一個特定的信號,可以使 nginx 重新生成日誌文件。我們可以定期執行一個 Shell 腳本來切換日誌,重新命名或轉移,具體的腳本如下:

# mv  /opt/nginx/logs/access.log /opt/nginx/logs/access_`date +%Y%m%d`.log  # killall –s USR1 nginx	#使用USR1參數通知Nginx進程切換日誌文件  

將以上腳本內容保存為文件名為 logcron.sh 存到自定的目錄中,例如 /opt/nginx/sbin/logcron.sh

使用 Crontab 讓該腳本程序在每天晚上 11 點 59 分自動執行,即可做到按天創建日誌。





安裝和配置 Awstats

安裝之前,必須確認你的伺服器上 Perl 的環境已經就緒。

查看當前環境 Perl 版本的命令是 perl –version

我們還需要對 Nginx 的日誌格式做個小修改,不然 awstats 將無法進行統計。

例子如下(加粗部分):

# 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	#格式代稱 (注意,如果有多個虛擬主機,代稱不能一樣)  '$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 。然後執行 tools 目錄中的 awstats_configure.pl 配置嚮導,創建一個新的統計。

-----> 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 創建一個新的統計配置  

回車

-----> 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		#統計網站的域名 例:  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):  #> 	  

使用默認直接回車,接下來便會出現以下的提示
----> 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 按指定時間執行  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...		回車繼續    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...		  

回車完成嚮導,接下來修改 www.moabc.net 的統計配置

#vi /etc/awstats/awstats.www.moabc.net.conf

找到統計的日誌文件的路徑

LogFile="/var/log/httpd/mylog.log"

改為

LogFile="/opt/nginx/logs/access_%YYYY-0%MM-0%DD-0.log

對應上邊 Nginx 日誌切割程序的所生成的目錄存放結構,要注意 Awstats 的年月日格式的跟 Nginx 的寫法有所不同。我們現在執行統計的順序是:

Nginx 產生日誌 –> 日誌切割 –> Nginx 繼續產生日誌 –> 另存切割日誌 –> 交由Awstats統計 –> 生成結果

在本文中 Awstats 所統計的日誌,是已切下來的那部分。也能調轉順序,先統計完了再切。不過這比較容易造成統計的遺漏。配置修改完成後,保存退出。然後我們可以開始試一下手動執行。

  1. 先執行日誌切割腳本 logcron.sh 把 Nginx 的日誌切下來。
  2. 然後執行 Awstats 日誌更新程序開始統計分析。
# /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 都已經運行無誤了。統計分析完成後,結果還在 Awstats 的資料庫中。在 Apache 上,可以直接打開 Perl 程序的網頁查看統計。但本文開始時已經提到,Nginx 對 Perl 支持並不好,所以我們要換個方法,利用 awstats 的工具將統計的結果生成靜態文件,具體的步驟如下:

  • 首先在 webroot 目錄下創建一個文件夾。例:/data/webroot/awstats
  • 然後讓 Awstats 把靜態頁面生成到該目錄中
# 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 靜態頁面生成工具
  • -update -config=www.moabc.net 更新配置項
  • -lang=cn 語言為中文
  • -dir=/data/admin_web/awstats 統計結果輸出目錄
  • -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 靜態頁面目錄          root   /data/webroot/awstats;          index  index.html;          access_log off;          error_log off;          charset gb2312; #最好把默認編碼改成 gb2312避免瀏覽器因自動編碼出現亂碼的情況  }    location ~ ^/icon/ {             # 圖標目錄          root   /usr/local/awstats/wwwroot;          index  index.html;          access_log off;          error_log off;          charset gb2312;          }  }  

用瀏覽器查看到統計的詳細結果 http://youhostname/awstats/awstats.www.moabc.net.html

至此,使用 awstats 已能完全支持 Nginx 的日誌統計。





配置 Awstats 自動運行

為了讓整個日誌的統計過程自動完成,我們需要設置 crontab 計劃任務,讓 Nginx 日誌切割以及 Awstats 自動運行,定時生成結果頁面。

#vi /etc/crontab    11 59 * * * /opt/nginx/sbin/logcron.sh			#半夜11:59  進行日誌切割    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進行日誌分析    :wq保存退出  #crontab /etc/crontab	指定cron所執行的配置檔路徑  





保護日誌統計結果頁面

一般站長都不願隨便讓人知道自己站的真實流量,所以要把 Awstats 統計結果頁面進行密碼保護。Nginx 使用的是跟 Apache 一樣的密碼加密格式,這裡需要用到 apache 自帶的工具 htpasswd。

如果你在本機上默認裝有 Apache,這你就只需在它的程序目錄下運行

例:

#/usr/local/apache2/bin/htpasswd -c admin.pass admin #用戶名為admin    New password:			輸入密碼  Re-type new password:		重複輸入  Adding password for user admin	創建成功    然後把 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 靜態頁面目錄          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/ {             # 圖標目錄          root   /usr/local/awstats/wwwroot;          index  index.html;          access_log off;          error_log off;          charset gb2312;          }  }    修改 Nginx 配置完畢后,執行命令 killall –s HUP nginx 讓 Nginx 重新載入配置即可。  





總結

儘管跟 Apache HTTP Server 相比較而言,Nginx 的功能是比較弱的,但是我們依然可以利用一些技巧來規避這些弱點,Nginx 的設計者肯定也是充分考慮到這個問題。現在也越來越多的第三方開發的模塊在逐漸的擴展 Nginx 的功能。但是從應用本身的角度而言,Nginx 更傾向於卓越的性能,而非大而全的功能,因而在一些附加方面的功能,我們也不能對之要求過高。(責任編輯:A6)



[火星人 ] 使用 awstats 分析 Nginx 的訪問日誌已經有1170次圍觀

http://coctec.com/docs/linux/show-post-69086.html