歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 爛泥:利用awstats分析nginx日志

爛泥:利用awstats分析nginx日志

日期:2017/3/1 12:10:13   编辑:關於Linux

昨天把nginx的日志進行了切割,關於如何切割nginx日志,可以查看《爛泥:切割nginx日志》這篇文章。

今天打算分析下nginx日志,要分析nginx日志,我們可以通過shell腳本和第三方軟件awstats進行分析,在此我們選擇的是通過第三方軟件awstats進行分析。

要使用awstats分析nginx日志,我們要安裝awstats,而在安裝awstats之前,我們需要先來介紹下awstats是什麼?

一、awstats是什麼

awstats是一個免費非常簡潔而且強大有個性的基於Perl語言的WEB日志分析工具。

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

1):訪問量、訪問次數、頁面浏覽量、點擊數、數據流量等。

2):精確到每月、每日、每小時的數據。

3):訪問者國家以及IP。

4):Robots/Spiders的統計。

5):訪客持續時間。

6):對不同files type的統計信息。

7):Pages-URL的統計。

8):訪客操作系統浏覽器等信息。

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

二、准備工作

在介紹完awstats是什麼之後,我們再來介紹下安裝awstats之前的准備工作。

2.1 安裝GeoIP

GeoIP主要用於定位訪問者所屬的國家以及城市的數據庫,可以使用如下命令進行安裝:

yum -y install GeoIP GeoIP-devel perl-Geo-IP

clip_image001[1]

查看GeoIP安裝的相關組件,如下:

rpm -ql GeoIP-GeoLite-data

rpm -ql GeoIP-GeoLite-data-extra

clip_image002[1]

如果是ubuntu系統,我們可以使用如下命令進行安裝:

sudo apt-get -y install geoip-bin geoip-database-contrib php5-geoip python-geoip python-geoip-dbg libgeoip1 libgeoip-dev

在上述截圖的安裝的文件中,我們只需要關注GeoIP.dat和GeoLiteCity.dat這兩個文件即可,其中GeoIP.dat用於定位訪問者所屬的國家,而GeoLiteCity.dat用於定位訪問者所屬的城市。

2.2 安裝httpd-tools

一般站長都不願隨便讓其他人知道自己網站的真實流量,所以要把awstats統計結果頁面進行密碼保護。

因為nginx使用的是跟apache一樣的密碼加密格式,所以這裡需要用到apache自帶的工具htpasswd,而htpasswd包含在httpd-tools組件中的,所以我們要安裝httpd-tools。

安裝httpd-tools,使用如下命令:

yum -y install httpd-tools

clip_image003[1]

安裝httpd-tools時,會自動安裝httpd,所以我們需要把httpd禁用。如下:

/etc/init.d/httpd stop

chkconfig httpd off

生成密碼文件,使用如下命令:

htpasswd -c -m /usr/local/htpasswd.pass ilanni

clip_image004[1]

三、安裝awstats

准備工作做完後,我們現在來安裝awstats。

3.1 下載awstats

awstst官網是http://www.awstats.org/,下載最新版本de awstats。如下:

wget http://www.awstats.org/files/awstats-7.5.tar.gz

clip_image005[1]

clip_image006[1]

3.2 安裝awstats

awstats下載完畢後,我們現在來安裝awstats。解壓安裝包,並進行相關操作,如下:

tar -C /usr/local/ -xf awstats-7.5.tar.gz

mv /usr/local/awstats-7.5/ /usr/local/awstats

chown root:root -R /usr/local/awstats/

mkdir -p /var/www/awstats/

cd /usr/local/awstats/tools/

chmod +x /usr/local/awstats/tools/*.pl

chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl

clip_image007[1]

開始安裝awstats,如下:

./awstats_configure.pl

clip_image008[1]

因為在此我們使用的是nginx,所以以上截圖填寫none。

clip_image009

創建一個新的統計配置文件。

clip_image010

在這輸入自己的網站域名,也可以進行自定義。

clip_image011

使用默認配置,生成配置文件。

clip_image012

安裝程序執行結束後,會在/etc/awstats/目錄下生成相應的配置文件。如下:

ll /etc/awstats/awstats.www.ilanni.com.conf

clip_image013

3.3 編輯配置文件

awstats安裝完畢後,我們現在來修改其配置文件awstats.www.ilanni.com.conf。在該配置文件中,我們主要修改的是awstats分析nginx日志的路徑,以及日志格式,當然還有其它的一些插件。如下:

vim /etc/awstats/awstats.www.ilanni.com.conf

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

#配置顯示國家和地區

LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"

LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/share/GeoIP/GeoLiteCity.dat"

#解決搜索關鍵字的亂碼

LoadPlugin="decodeutfkeys"

注意:LogFile路徑中日志文件中的日期格式“%YYYY-24%MM-24%DD-24”,是指24小時之前的年月日,也就是昨天的日期。-24表示一天前的,-0表示當前。

clip_image014

3.4 安裝純真IP數據庫插件

如果要詳細顯示國內訪客的具體來源信息,我們可以使用純真IP數據庫插件。

純真IP地址數據庫下載地址:http://update.cz88.net/soft/setup.zip

在windows機器上下載安裝後,安裝目錄下的qqwry.dat文件即是最新版IP地址數據庫。

下載awstats純真IP數據庫解析插件,下載地址如下:

http://down.51cto.com/data/1888530

注意,附件qqhostinfo.pm、qqwry.pl裡的文件路徑已經修改好。

下載完畢後,把qqhostinfo.pm、qqwry.pl、qqwry.dat這三個文件,放到awstats的wwwroot/cgi-bin/plugins目錄下,並且給予qqhostinfo.pm文件可執行權限。如下:

clip_image015

最後修改awstats的配置文件awstats.www.ilanni.com.conf添加LoadPlugin="qqhostinfo"選項,如下:

clip_image016

到此awstats安裝和配置已經全部完成。

四、配置nginx日志格式

awstats安裝完畢後,我們要配置nginx日志的格式,因為awstats是按照指定的格式對nginx日志文件進行分析的,所以我們要進行對應的配置。

nginx日志的配置方法如下:

vim /etc/nginx/nginx.conf

log_format site '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $http_x_forwarded_for';

clip_image017

五、awstats與nginx進行web集成

Nginx日志格式配置完畢後,我們現在要把awstats與nginx進行集成。

為什麼awstats與nginx進行集成?因為awstats的訪問是通過web方式進行的。這個集成也分為兩種情況 ,一個是awstats單獨以nginx虛擬主機的形式存在,一種是awstats與現有nginx虛擬主機進行集成。下面分別把這兩種方式的nginx配置貼出來,如下:

awstats單獨作為nginx的一個虛擬主機,配置文件:

server {

listen 80;

server_name www.ilanni.com;

root /var/www/awstats/;

index index.html;

#或者

#index awstats.test.haiyn.com.html;

location ~ ^/cgi-bin/.*.(cgi|pl|py|rb) {

gzip off;

include fastcgi_params;

fastcgi_pass unix:/tmp/php-cgi.sock;

# fastcgi_pass 127.0.0.1:9000; #注意配置fastcgi_pass為你的php-fpm server.

fastcgi_index cgi-bin.php;

fastcgi_param SCRIPT_FILENAME /usr/local/awstats/wwwroot/cgi-bin/fcgi.php; #注意文件路徑

fastcgi_param SCRIPT_NAME /cgi-bin/fcgi.php;

fastcgi_param X_SCRIPT_FILENAME /usr/local/awstats/wwwroot$fastcgi_script_name; #注意文件路徑

fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;

fastcgi_param REMOTE_USER $remote_user;

}

# Static awstats files: HTML files stored in DOCUMENT_ROOT/awstats/

location /classes/ {

alias /usr/local/awstats/wwwroot/classes/;

}

location /css/ {

alias /usr/local/awstats/wwwroot/css/;

}

location /js/ {

alias /usr/local/awstats/wwwroot/js/;

}

location /icon/ {

alias /usr/local/awstats/wwwroot/icon/;

}

}

clip_image018

awstats與現有nginx虛擬主機進行集成。

注意:一定要把awstats相關的配置存放到最前面,這是因為nginx的匹配規則是從上到下進行匹配的。

server {

listen 80;

server_name www.ilanni.com ilanni.com;

charset utf-8;

access_log /var/log/nginx/ilanni.com.log main;

location ~ ^/icon/ { #圖標目錄

root /usr/local/awstats/wwwroot;

index index.html;

access_log off;

charset gb2312;

}

location ~ ^/cgi-bin/.*.(cgi|pl|py|rb) {

root /usr/local/awstats/wwwroot;

gzip off;

include fastcgi_params;

fastcgi_pass 127.0.0.1:9000;

fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;

fastcgi_param SCRIPT_FILENAME $document_root/cgi-bin/fcgi.php;

fastcgi_param X_SCRIPT_FILENAME $document_root$fastcgi_script_name;

#fastcgi_param REMOTE_USER $remote_user;

auth_basic "Restricted";

auth_basic_user_file /usr/local/awstats/htpasswd.pass;

}

location / {

root /www/ilanni.com;

index index.php index.html index.htm;

}

location ~ \.php$ {

root /www/ilanni.com/;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

clip_image019

六、生成awstats數據庫

現在開始使用awstats分析日志,使用如下命令:

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

clip_image020

生成靜態文件,使用如下命令:

/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.ilanni.com -lang=cn -dir=/var/www/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

clip_image021

為了看到每一天更新的數據,我們可以通過crontab在每天凌晨00:01定時更新靜態頁面。如下:

vim /etc/crontab

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

clip_image022

七、訪問awstats

awstats數據庫生成完畢後,我們現在來通過web頁面訪問下。在此我使用的是第二種方式awstats與nginx進行集成。

如下:

http://www.ilanni.com/cgi-bin/awstats.pl

clip_image023

上輸入的是我們在第二章准備工作中htpasswd生成的用戶名和密碼。

clip_image024

上圖就是awstats對nginx日志文件分析後,所得到的相關信息。

clip_image025

上圖是我們安裝的GeoIP和純真IP數據庫顯示的用戶訪問的國家、城市以及來源地址。

clip_image026

上圖是awstats根據nginx日志文件分析出來用戶是搜索哪些關鍵詞過來的。

本文出自 “爛泥行天下” 博客,請務必保留此出處http://ilanni.blog.51cto.com/526870/1763175

Copyright © Linux教程網 All Rights Reserved