日志分析工具Awstats實戰之Nginx篇-分析結果靜態化

原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://nolinux.blog.51cto.com/4824967/1316979

前言:

Awstats 是在 SourceForge 上發展很快的一個基原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://nolinux.blog.51cto.com/4824967/1316979

前言:

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可以分析apache日志,同樣也可以分析nginx日志。本文將詳細介紹自動定時切割nginx的訪問日志,并使用awstats來定時分析nginx日志及實現統計結果可供安全便捷的查閱。


環境:

CentOS 6.4 x86_64ip:192.168.1.113域名:www.sunsky.com(server和client都通過hosts文件解析)nginx-1.2.9 編譯安裝,路徑/usr/local/nginx,服務開啟狀態日志記錄格式為nginx默認的,切勿更改,否則會造成awstats無法分析日志。log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                  '$status $body_bytes_sent "$http_referer" '                  '"$http_user_agent" "$http_x_forwarded_for"';awstats-7.2.tar.gz

一、日志自動切割

對于nginx的日志切割,由于沒有像apache一樣去用cronolog工具,這里我們就寫一個腳本,讓它可以在每天00:01自動執行,切割昨天的日志(交由awstats分析),壓縮前天的日志(壓縮日志可減小存儲空間,為防止awstats沒有分析完就被壓縮,所以只壓縮前天的日志)。

1 vim /server/scripts/cut_nginx_log.sh

輸入以下內容:

#!/bin/shyesterday=`date -d "yesterday" +"%Y%m%d"`before_yesterday=`date -d "-2 day" +"%Y%m%d"`Nginx_Dir="/usr/local/nginx"Nginx_logs="/app/logs"Log_Name="www_access"cd /tmp[ -d $Nginx_Logs ] && cd $Nginx_logs || exit 1[ -f $Log_Name.log ] && /bin/mv $Log_Name.log ${Log_Name}_${yesterday}.log || exit 1if [ $? -eq 0 -a -f $Nginx_Dir/logs/nginx.pid ]    then       kill -USR1 `cat $Nginx_Dir/logs/nginx.pid`fi[ -f  ${Log_Name}_${before_yesterday}.log ] && /usr/bin/gzip ${Log_Name}_${before_yesterday}.log|| exit 1

執行crontab -e將該腳本加入定時任務中

1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

這樣每天凌晨00:01就能自動實現日志的切割,壓縮等功能了

因為本次實驗下的nginx此時已經有日志了,另外為了后文awstats能對切割過的日志進行分析,所以這里我們要運行一下此腳本,來將現有日志進行切割生成昨天的日志方便后文操作。

/bin/sh
 
/server/scripts/cut_nginx_log
.sh >
/dev/null
 
2>&1

二、Awstats的安裝與配置

1.部署awstats

首先我們要下載awstats軟件包,并將其放在常規目錄(/usr/local)下

wget http://awstats.sourceforge.net/files/awstats-7.2.tar.gztar zxf awstats-7.2.tar.gzmv awstats-7.2 /usr/local/awstats

由于wget下載下來的包中權限是非root的,所以這里要修改權限,否則稍后*.pl將無法運行

chown -R root.root /usr/local/awstatschmod +x /usr/local/awstats/tools/*.plchmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl

接下來我們要執行awstats/tools下的awstats_configure.pl配置向導,用來生成awstats的配置文件,awstats配置文件的命名規則是awstats.website.conf。

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

此時會出現如下提示

----- AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur -----This tool will help you to configure AWStats to analyze statistics forone web server. You can try to use it to let it do all that is possiblein AWStats setup, however following the step by step manual setupdocumentation (docs/index.html) is often a better idea. Above all if:- You are not an administrator user,- You want to analyze downloaded log files without web server,- You want to analyze mail or ftp log files instead of web log files,- You need to analyze load balanced servers log files,- You want to 'understand' all possible ways to use AWStats...Read the AWStats documentation (docs/index.html).-----> Running OS detected: Linux, BSD or Unix-----> Check for web server installEnter full config file path of your Web server.Example: /etc/httpd/httpd.confExample: /usr/local/apache2/conf/httpd.confExample: c:\Program files\apache group\apache\conf\httpd.confConfig file path ('none' to skip web server setup):> none      #這里讓填寫網頁服務器的配置文件路徑,因為我們用的不是apache,所以這里要填noneYour web server config file(s) could not be found.You will need to setup your web server manually to declare AWStatsscript 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/profilefile (required if first install) [y/N] ? y         #詢問是否創建一個新的配置文件,這里填y-----> Define config file name to createWhat is the name of your web site or profile analysis ?Example: www.mysite.comExample: demoYour web site, virtual server or profile name:> www.sunsky.com      #這里讓填寫你的網站域名,虛擬主機名或者隨便一個配置名-----> Define config file pathIn which directory do you plan to store your config file(s) ?Default: /etc/awstatsDirectory path to store config file(s) (Enter for default):>              #這里要填寫你配置文件存放路徑,我們使用它默認的路徑/etc/awstats,所以直接回車即可-----> Create config file '/etc/awstats/awstats.www.sunsky.com.conf' Config file /etc/awstats/awstats.www.sunsky.com.conf created.-----> Add update process inside a schedulerSorry, 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.sunsky.comOr if you have several config files and prefer having only one command:/usr/local/awstats/tools/awstats_updateall.pl nowPress ENTER to continue...   #提示不能自動加入crontab定時任務,需要稍后自己添加,我們按回車繼續即可A SIMPLE config file has been created: /etc/awstats/awstats.www.sunsky.com.confYou should have a look inside to check and change manually main parameters.You can then manually update your statistics for 'www.sunsky.com' with command:> perl awstats.pl -update -config=www.sunsky.comYou can also build static report pages for 'www.sunsky.com' with command:> perl awstats.pl -output=pagetype -config=www.sunsky.comPress ENTER to finish... #提示配置文件創建完成和如何更新配置及建立靜態報告頁,這里我們回車即可結束這個配置向導

2、修改awstats配置文件

完成配置文件的創建之后,我們還需要對/etc/awstats/awstats.www.sunsky.com.conf里的一些參數進行修改。

sed -i 's#LogFile="/var/log/httpd/mylog.log"#LogFile="/app/logs/www_access_%YYYY-24%MM-24%DD-24.log"#g' /etc/awstats/awstats.www.sunsky.com.conf

這里更改的目的是指定awstats需要分析的nginx的日志文件路徑。這里的路徑大家要按自己的日志路徑來填。

sed -i 's#DirData="/var/lib/awstats"#DirData="/usr/local/awstats/data"#g' /etc/awstats/awstats.www.sunsky.com.conf

這里更改的目的是指定awstats的數據庫配置文件(即awstats的數據庫(純文本))。由于,此處沒有/usr/local/awstats/data目錄,所以我們要創建出來

mkdir /usr/local/awstats/data

以上的兩個替換操作進行完之后一定要用命令查看替換是否成功,以便及早發現紕漏

grep "LogFile=" /etc/awstats/awstats.www.sunsky.com.confgrep "DirData=" /etc/awstats/awstats.www.sunsky.com.conf

查詢替換結果正確之后,即可進行下面的步驟。


3、創建awstats統計結果存放目錄

現在我們要創建awstats統計結果的數據庫存放

/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.sunsky.com

目錄如果屏幕輸出類似下面的提示就說明配置文件都正確無誤了

Create/Update database for config "/etc/awstats/awstats.www.sunsky.com.conf" by AWStats version 7.2 (build 1.992)From data in log file "/app/logs/www_access.log"...Phase 1 : First bypass old records, searching new record...Searching new records from beginning of log file...Jumped lines in file: 0Parsed lines in file: 0 Found 0 dropped records, Found 0 comments, Found 0 blank records, Found 0 corrupted records, Found 0 old records, Found 0 new qualified records.

注釋:awstats.pl 會到 /etc/awstats目錄下搜索,根據讀取到的配置文件運行程序,去讀取-config中的參數,把www.sunsky.com擴展成 awstats.www.sunsky.com.conf

分析日志:運行后將這樣的日志統計結果歸檔到一個awstats的數據庫(純文本)里;

然后是輸出:分兩種形式

1、一種是通過cgi程序讀取統計結果數據庫輸出;

2、一種是運行后臺腳本將輸出導出成靜態文件;

統計分析完成后,結果還在 Awstats 的數據庫中。在 Apache 上,可以直接打開 Perl 程序的網頁查看統計。 

vim /server/scripts/awstats.sh

但Nginx 對 Perl 支持并不好,所以要換個方法,利用 awstats 的工具將統計的結果生成靜態文件,這里方便我們還是用腳本來實現輸入以下內容

#!/bin/shAwstats_Dir="/usr/local/awstats"[ -d /www/awstats ]||mkdir /www/awstats$Awstats_Dir/wwwroot/cgi-bin/awstats.pl -update -config=www.sunsky.com$Awstats_Dir/tools/awstats_buildstaticpages.pl -update -config=www.sunsky.com -awstatsprog=$Awstats_Dir/wwwroot/cgi-bin/awstats.pl -lang=cn -dir=/www/awstats

腳本內容講解:

/usr/local/awstats/tools/awstats_buildstaticpages.pl    Awstats 靜態頁面生成工具-update -config=www.sunsky.com  更新配置項-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl  Awstats 日志更新程序路徑-lang=cn     語言為中文-dir= /www/awstats  統計結果輸出目錄

awstats_buildstaticpages.pl會根據-config的參數去讀取里面的DirData路徑下的數據庫配置文件,然后生成靜態的html文件,生成的文件重定向到/www/awstats目錄下。

三、配置nginx實現安全訪問

接下來我們要配置nginx使其能安全的訪問到分析的數據

vim /usr/local/nginx/conf/nginx.conf

在server{}內添加如下內容:

server {        listen       80;        server_name  www.sunsky.com;        location / {            root   /www/sunsky;            index  index.html index.htm;            access_log /app/logs/www_access.log commonlog;}location ~ ^/awstats/ {            root   /www/;            index  awstats.www.sunsky.com.html;          #根據自己的網站域名進行更改首頁文件            autoindex on;            access_log off;            charset gb2312;            auth_basic "Restricted";                                          #有些網站不愿意公開網站流量信息,所以加個認證            auth_basic_user_file /usr/local/nginx/htpasswd.pass;   #該文件由apache的加密認證工具htpasswd創建}location ~ ^/icon/ {            root   /usr/local/awststs/wwwroot;            index  index.html;            access_log off;            charset gb2312;}    }

由于nginx沒有好的加密認證工具,需要借助apache的htpasswd來實現加密認證功能

htpasswd -c -m /usr/local/nginx/htpasswd.pass sunskyadmin      #用戶名為sunskyadmin

配置完畢之后,檢查nginx語法,然后優雅重啟之后,用游覽器訪問www.sunsky.com/awstats,輸入賬號密碼之后即可查看統計信息了。

至此,awstats已經可以實現對Nginx的日志統計及靜態化的安全訪問功能了。

四、配置awstats自動運行

為了讓整個日志的統計過程可以實現自動化,將awstats.sh腳本加入crontab定時任務中去,此時結合上面的定時切割任務,我們的crontab里面會有多出來兩條定時任務

1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&10 1 * * * /bin/sh /server/scripts/awstats.sh >/dev/null 2>&1

到此,我們整個日志訪問工具awstats在nginx上的配置就完成了。當然此篇是基于靜態頁面的顯示的,后面會再寫一篇博文日志分析工具Awstats實戰之Nginx篇-分析結果動態化出來和大家一起交流學習的。

于 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可以分析apache日志,同樣也可以分析nginx日志。本文將詳細介紹自動定時切割nginx的訪問日志,并使用awstats來定時分析nginx日志及實現統計結果可供安全便捷的查閱。


環境:

CentOS 6.4 x86_64ip:192.168.1.113域名:www.sunsky.com(server和client都通過hosts文件解析)nginx-1.2.9 編譯安裝,路徑/usr/local/nginx,服務開啟狀態日志記錄格式為nginx默認的,切勿更改,否則會造成awstats無法分析日志。log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                  '$status $body_bytes_sent "$http_referer" '                  '"$http_user_agent" "$http_x_forwarded_for"';awstats-7.2.tar.gz

一、日志自動切割

對于nginx的日志切割,由于沒有像apache一樣去用cronolog工具,這里我們就寫一個腳本,讓它可以在每天00:01自動執行,切割昨天的日志(交由awstats分析),壓縮前天的日志(壓縮日志可減小存儲空間,為防止awstats沒有分析完就被壓縮,所以只壓縮前天的日志)。

vim /server/scripts/cut_nginx_log.sh

輸入以下內容:

#!/bin/shyesterday=`date -d "yesterday" +"%Y%m%d"`before_yesterday=`date -d "-2 day" +"%Y%m%d"`Nginx_Dir="/usr/local/nginx"Nginx_logs="/app/logs"Log_Name="www_access"cd /tmp[ -d $Nginx_Logs ] && cd $Nginx_logs || exit 1[ -f $Log_Name.log ] && /bin/mv $Log_Name.log ${Log_Name}_${yesterday}.log || exit 1if [ $? -eq 0 -a -f $Nginx_Dir/logs/nginx.pid ]    then       kill -USR1 `cat $Nginx_Dir/logs/nginx.pid`fi[ -f  ${Log_Name}_${before_yesterday}.log ] && /usr/bin/gzip ${Log_Name}_${before_yesterday}.log|| exit 1

執行crontab -e將該腳本加入定時任務中

1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

這樣每天凌晨00:01就能自動實現日志的切割,壓縮等功能了

因為本次實驗下的nginx此時已經有日志了,另外為了后文awstats能對切割過的日志進行分析,所以這里我們要運行一下此腳本,來將現有日志進行切割生成昨天的日志方便后文操作。

/bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

二、Awstats的安裝與配置

1.部署awstats

首先我們要下載awstats軟件包,并將其放在常規目錄(/usr/local)下

wget http://awstats.sourceforge.net/files/awstats-7.2.tar.gztar zxf awstats-7.2.tar.gzmv awstats-7.2 /usr/local/awstats

由于wget下載下來的包中權限是非root的,所以這里要修改權限,否則稍后*.pl將無法運行

chown -R root.root /usr/local/awstatschmod +x /usr/local/awstats/tools/*.plchmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl

接下來我們要執行awstats/tools下的awstats_configure.pl配置向導,用來生成awstats的配置文件,awstats配置文件的命名規則是awstats.website.conf。

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

此時會出現如下提示

----- AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur -----This tool will help you to configure AWStats to analyze statistics forone web server. You can try to use it to let it do all that is possiblein AWStats setup, however following the step by step manual setupdocumentation (docs/index.html) is often a better idea. Above all if:- You are not an administrator user,- You want to analyze downloaded log files without web server,- You want to analyze mail or ftp log files instead of web log files,- You need to analyze load balanced servers log files,- You want to 'understand' all possible ways to use AWStats...Read the AWStats documentation (docs/index.html).-----> Running OS detected: Linux, BSD or Unix-----> Check for web server installEnter full config file path of your Web server.Example: /etc/httpd/httpd.confExample: /usr/local/apache2/conf/httpd.confExample: c:\Program files\apache group\apache\conf\httpd.confConfig file path ('none' to skip web server setup):> none      #這里讓填寫網頁服務器的配置文件路徑,因為我們用的不是apache,所以這里要填noneYour web server config file(s) could not be found.You will need to setup your web server manually to declare AWStatsscript 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/profilefile (required if first install) [y/N] ? y         #詢問是否創建一個新的配置文件,這里填y-----> Define config file name to createWhat is the name of your web site or profile analysis ?Example: www.mysite.comExample: demoYour web site, virtual server or profile name:> www.sunsky.com      #這里讓填寫你的網站域名,虛擬主機名或者隨便一個配置名-----> Define config file pathIn which directory do you plan to store your config file(s) ?Default: /etc/awstatsDirectory path to store config file(s) (Enter for default):>              #這里要填寫你配置文件存放路徑,我們使用它默認的路徑/etc/awstats,所以直接回車即可-----> Create config file '/etc/awstats/awstats.www.sunsky.com.conf' Config file /etc/awstats/awstats.www.sunsky.com.conf created.-----> Add update process inside a schedulerSorry, 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.sunsky.comOr if you have several config files and prefer having only one command:/usr/local/awstats/tools/awstats_updateall.pl nowPress ENTER to continue...   #提示不能自動加入crontab定時任務,需要稍后自己添加,我們按回車繼續即可A SIMPLE config file has been created: /etc/awstats/awstats.www.sunsky.com.confYou should have a look inside to check and change manually main parameters.You can then manually update your statistics for 'www.sunsky.com' with command:> perl awstats.pl -update -config=www.sunsky.comYou can also build static report pages for 'www.sunsky.com' with command:> perl awstats.pl -output=pagetype -config=www.sunsky.comPress ENTER to finish... #提示配置文件創建完成和如何更新配置及建立靜態報告頁,這里我們回車即可結束這個配置向導

2、修改awstats配置文件

完成配置文件的創建之后,我們還需要對/etc/awstats/awstats.www.sunsky.com.conf里的一些參數進行修改。

sed -i 's#LogFile="/var/log/httpd/mylog.log"#LogFile="/app/logs/www_access_%YYYY-24%MM-24%DD-24.log"#g' /etc/awstats/awstats.www.sunsky.com.conf

這里更改的目的是指定awstats需要分析的nginx的日志文件路徑。這里的路徑大家要按自己的日志路徑來填。

sed -i 's#DirData="/var/lib/awstats"#DirData="/usr/local/awstats/data"#g' /etc/awstats/awstats.www.sunsky.com.conf

這里更改的目的是指定awstats的數據庫配置文件(即awstats的數據庫(純文本))。由于,此處沒有/usr/local/awstats/data目錄,所以我們要創建出來

mkdir /usr/local/awstats/data

以上的兩個替換操作進行完之后一定要用命令查看替換是否成功,以便及早發現紕漏。

grep "LogFile=" /etc/awstats/awstats.www.sunsky.com.confgrep "DirData=" /etc/awstats/awstats.www.sunsky.com.conf

查詢替換結果正確之后,即可進行下面的步驟。


3、創建awstats統計結果存放目錄

現在我們要創建awstats統計結果的數據庫存放目錄

/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.sunsky.com

如果屏幕輸出類似下面的提示就說明配置文件都正確無誤了

Create/Update database for config "/etc/awstats/awstats.www.sunsky.com.conf" by AWStats version 7.2 (build 1.992)From data in log file "/app/logs/www_access.log"...Phase 1 : First bypass old records, searching new record...Searching new records from beginning of log file...Jumped lines in file: 0Parsed lines in file: 0 Found 0 dropped records, Found 0 comments, Found 0 blank records, Found 0 corrupted records, Found 0 old records, Found 0 new qualified records.

注釋:awstats.pl 會到 /etc/awstats目錄下搜索,根據讀取到的配置文件運行程序,去讀取-config中的參數,把www.sunsky.com擴展成 awstats.www.sunsky.com.conf

分析日志:運行后將這樣的日志統計結果歸檔到一個awstats的數據庫(純文本)里;

然后是輸出:分兩種形式

1、一種是通過cgi程序讀取統計結果數據庫輸出;

2、一種是運行后臺腳本將輸出導出成靜態文件;

統計分析完成后,結果還在 Awstats 的數據庫中。在 Apache 上,可以直接打開 Perl 程序的網頁查看統計。 但Nginx 對 Perl 支持并不好,所以要換個方法,利用 awstats 的工具將統計的結果生成靜態文件,這里方便我們還是用腳本來實現

vim /server/scripts/awstats.sh

輸入以下內容

#!/bin/shAwstats_Dir="/usr/local/awstats"[ -d /www/awstats ]||mkdir /www/awstats$Awstats_Dir/wwwroot/cgi-bin/awstats.pl -update -config=www.sunsky.com$Awstats_Dir/tools/awstats_buildstaticpages.pl -update -config=www.sunsky.com -awstatsprog=$Awstats_Dir/wwwroot/cgi-bin/awstats.pl -lang=cn -dir=/www/awstats

腳本內容講解:

/usr/local/awstats/tools/awstats_buildstaticpages.pl    Awstats 靜態頁面生成工具-update -config=www.sunsky.com  更新配置項-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl  Awstats 日志更新程序路徑-lang=cn     語言為中文-dir= /www/awstats  統計結果輸出目錄

awstats_buildstaticpages.pl會根據-config的參數去讀取里面的DirData路徑下的數據庫配置文件,然后生成靜態的html文件,生成的文件重定向到/www/awstats目錄下。

三、配置nginx實現安全訪問

接下來我們要配置nginx使其能安全的訪問到分析的數據

vim /usr/local/nginx/conf/nginx.conf

在server{}內添加如下內容:

server {        listen       80;        server_name  www.sunsky.com;        location / {            root   /www/sunsky;            index  index.html index.htm;            access_log /app/logs/www_access.log commonlog;}location ~ ^/awstats/ {            root   /www/;            index  awstats.www.sunsky.com.html;          #根據自己的網站域名進行更改首頁文件            autoindex on;            access_log off;            charset gb2312;            auth_basic "Restricted";                                          #有些網站不愿意公開網站流量信息,所以加個認證            auth_basic_user_file /usr/local/nginx/htpasswd.pass;   #該文件由apache的加密認證工具htpasswd創建}location ~ ^/icon/ {            root   /usr/local/awststs/wwwroot;            index  index.html;            access_log off;            charset gb2312;}    }

由于nginx沒有好的加密認證工具,需要借助apache的htpasswd來實現加密認證功能

htpasswd -c -m /usr/local/nginx/htpasswd.pass sunskyadmin      #用戶名為sunskyadmin

配置完畢之后,檢查nginx語法,然后優雅重啟之后,用游覽器訪問www.sunsky.com/awstats,輸入賬號密碼之后即可查看統計信息了。

至此,awstats已經可以實現對Nginx的日志統計及靜態化的安全訪問功能了。

四、配置awstats自動運行

為了讓整個日志的統計過程可以實現自動化,將awstats.sh腳本加入crontab定時任務中去,此時結合上面的定時切割任務,我們的crontab里面會有多出來兩條定時任務

1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&10 1 * * * /bin/sh /server/scripts/awstats.sh >/dev/null 2>&1

到此,我們整個日志訪問工具awstats在nginx上的配置就完成了。當然此篇是基于靜態頁面的顯示的,后面會再寫一篇博文日志分析工具Awstats實戰之Nginx篇-分析結果動態化出來和大家一起交流學習的。

轉自:http://nolinux.blog.51cto.com/4824967/1316979

原創文章,作者:s19930811,如若轉載,請注明出處:http://www.www58058.com/2022

(0)
s19930811s19930811
上一篇 2016-08-15
下一篇 2016-08-15

相關推薦

  • linux 進程解析

    博文介紹了進程信息以及部分進程管理命令

    2017-09-09
  • 馬哥Linux第六周作業

    網絡管理 進程查看及管理之ps,top,htop,dstat,vmstat

    2018-01-20
  • 網絡管理的相關命令

    今天要說的是有關網絡的一些相關內容,主要介紹的有以下幾點:網絡的服務:Centos6/7中的常用命令,如何手動配置網絡,路由表的相關內容,關于Bond(僅主機模式)網絡接口的配置以及Bridge(橋接)的網絡接口配置。 首先要說的是網絡的服務: 1,Centos6/7的常用網絡的命令: Centos6:禁用網卡:ifconfig 網卡名 down啟用網卡:i…

    2017-09-07
  • 堡壘機-麒麟開源堡壘機內置SSL VPN使用指南

      一、安裝 (一)確定服務器的操作系統位數 Windws xp、2000、2003系統,在我的電腦屬性里,可以很明顯地看到標識。如果沒有注明是64位的,那么默認就是32位的 Windows 7 系統在控制面板,點擊系統,在系統類型里,標注有操作系統位數 (二)安裝VPN客戶端 VPN客戶端分為32位系統和64位系統二…

    Linux干貨 2016-05-29
  • 【26期】Linux第七周學習小總結

        今天給大家的分享是本周的一個有趣的小內容,LVM邏輯卷相關的,為什么分享這個內容呢?主要是剛沒有接觸或者說深入接觸Linux時,有人在群里提問,如果不用虛擬機,那Centos本身有快照嗎?當時說的答案很熱鬧,不過大部分還是傾向于沒有快照,本周學習的邏輯卷管理器章節里,就這個問題給了一個準確答案,那我們一起來看一下吧!  …

    2017-08-26
欧美性久久久久