烂泥:利用awstats分析nginx日志

昨天把nginx的日志进行了切割,关于如何切割nginx日志,能够查看《烂泥:切割nginx日志》这篇文章。    php

今天打算分析下nginx日志,要分析nginx日志,咱们能够经过shell脚本和第三方软件awstats进行分析,在此咱们选择的是经过第三方软件awstats进行分析。 css

要使用awstats分析nginx日志,咱们要安装awstats,而在安装awstats以前,咱们须要先来介绍下awstats是什么? html

1、awstats是什么 python

awstats是一个免费很是简洁并且强大有个性的基于Perl语言的WEB日志分析工具。 nginx

它能够统计网站的以下信息: web

1):访问量、访问次数、页面浏览量、点击数、数据流量等。 shell

2):精确到每个月、每日、每小时的数据。 数据库

3):访问者国家以及IP。 apache

4):Robots/Spiders的统计。 ubuntu

5):访客持续时间。

6):对不一样files type的统计信息。

7):Pages-URL的统计。

8):访客操做系统浏览器等信息。

9):其它信息(搜索关键字等等)

2、准备工做

在介绍完awstats是什么以后,咱们再来介绍下安装awstats以前的准备工做。

2.1 安装GeoIP

GeoIP主要用于定位访问者所属的国家以及城市的数据库,可使用以下命令进行安装:

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

clip_p_w_picpath001[1]

查看GeoIP安装的相关组件,以下:

rpm -ql GeoIP-GeoLite-data

rpm -ql GeoIP-GeoLite-data-extra

clip_p_w_picpath002[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_p_w_picpath003[1]

安装httpd-tools时,会自动安装httpd,因此咱们须要把httpd禁用。以下:

/etc/init.d/httpd stop

chkconfig httpd off

生成密码文件,使用以下命令:

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

clip_p_w_picpath004[1]

3、安装awstats

准备工做作完后,咱们如今来安装awstats。

3.1 下载awstats

awstst官网是http://www.awstats.org/,下载最新版本de awstats。以下:

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

clip_p_w_picpath005[1]

clip_p_w_picpath006[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_p_w_picpath007[1]

开始安装awstats,以下:

./awstats_configure.pl

clip_p_w_picpath008[1]

由于在此咱们使用的是nginx,因此以上截图填写none。

clip_p_w_picpath009

建立一个新的统计配置文件。

clip_p_w_picpath010

在这输入本身的网站域名,也能够进行自定义。

clip_p_w_picpath011

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

clip_p_w_picpath012

安装程序执行结束后,会在/etc/awstats/目录下生成相应的配置文件。以下:

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

clip_p_w_picpath013

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_p_w_picpath014

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_p_w_picpath015

最后修改awstats的配置文件awstats.www.ilanni.com.conf添加LoadPlugin="qqhostinfo"选项,以下:

clip_p_w_picpath016

到此awstats安装和配置已经所有完成。

4、配置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_p_w_picpath017

5、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_p_w_picpath018

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_p_w_picpath019

6、生成awstats数据库

如今开始使用awstats分析日志,使用以下命令:

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

clip_p_w_picpath020

生成静态文件,使用以下命令:

/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_p_w_picpath021

为了看到每一天更新的数据,咱们能够经过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_p_w_picpath022

7、访问awstats

awstats数据库生成完毕后,咱们如今来经过web页面访问下。在此我使用的是第二种方式awstats与nginx进行集成。

以下:

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

clip_p_w_picpath023

上输入的是咱们在第二章准备工做中htpasswd生成的用户名和密码。

clip_p_w_picpath024

上图就是awstats对nginx日志文件分析后,所获得的相关信息。

clip_p_w_picpath025

上图是咱们安装的GeoIP和纯真IP数据库显示的用户访问的国家、城市以及来源地址。

clip_p_w_picpath026

上图是awstats根据nginx日志文件分析出来用户是搜索哪些关键词过来的。

相关文章
相关标签/搜索