阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

AWStats分析Nginx访问日志

183次阅读
没有评论

共计 5968 个字符,预计需要花费 15 分钟才能阅读完成。

AWStats 是在 Sourceforge 上发展很快的一个基于 Perl 的 WEB 日志分析工具。

它可以统计您站点的如下信息:

  • 访问量(UV),访问次数,页面浏览量(PV),点击数,数据流量等
  • 精确到每月、每日、每小时的数据
  • 访问者国家
  • 访问者 IP
  • Robots/Spiders 的统计
  • 访客持续时间
  • 对不同 Files type 的统计信息
  • Pages-URL 的统计
  • 访客操作系统浏览器等信息
  • 其它信息(搜索关键字等等)

AWStats 配置 (Windows + Apache) http://www.linuxidc.com/Linux/2013-09/89710.htm

CentOS 6.3 下 AWStats+GeoIP 实现查看网站访问归属地 http://www.linuxidc.com/Linux/2013-06/85984.htm

Nginx 日志分析 AWStats + JAWStats 安装配置 http://www.linuxidc.com/Linux/2013-06/85567.htm

经典日志分析工具 -AWStats http://www.linuxidc.com/Linux/2012-12/77080.htm

AWStats 日志系统配置文件和错误归纳 http://www.linuxidc.com/Linux/2012-12/75657.htm

使用 AWStats 分析网站日志 - 强大的日志分析工具 http://www.linuxidc.com/Linux/2012-11/74431.htm

下面是 AWStats 分析 Nginx 日志的操作步骤:

一、配置 nginx 日志格式

修改 nginx.conf 的日志格式,不然 awstats 无法分析。

    log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘

        ‘$status $body_bytes_sent “$http_referer” ‘

        ‘”$http_user_agent” “$http_x_forwarded_for”‘;

    access_log /var/log/nginx/access.log access;

注意,日志格式里的双引号不能漏了且每个参数之间是一个空格分隔,因为只要有细微的差别,awstats 就无法读取日志。

改好后,使 nginx 重读配置文件:

shell# nginx -t && nginx -s reload

二、自动切割 nginx 日志

每天晚上 23 点 59 分定时执行一个 shell 脚本来切割 nginx 日志。脚本内容如下:

#!/bin/bash

#

# Filename:    nginxCutLog.sh

# Author:      Qicheng

# Website:    http://www.linuxidc.com/

# Description: 切割 nginx 日志

# Notes:      设置 crontab,每天 23 点 59 分定时执行

#

ROOT_UID=0

if [“$UID” -ne “$ROOT_UID”];then

    echo “Error: 必须以 root 用户运行此程序!”

    exit 1

fi 

nginx_logs_dir=”/var/log/nginx”

nginx_pid_file=”/var/run/nginx.pid”

# 切割后的日志文件名,例如 access_20141022.log

nginx_log_today=”$nginx_logs_dir/access_`date +%Y%m%d`.log”

[-f “$nginx_log_today”] && exit 1

mv $nginx_logs_dir/access.log $nginx_log_today

# 给 nginx 发送 USR1 信号,使重新打开新的 access.log 日志文件

[-f $nginx_pid_file] && /bin/kill -USR1 $(cat $nginx_pid_file)

设置 crontab:

59 23 * * * /bin/bash /yourscriptpath/nginxCutLog.sh

三、安装 awstats

shell# wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz

shell# tar -zxvf awstats-7.0.tar.gz

shell# mv awstats-7.0 /usr/local/awstats

shell# mkdir -p /var/lib/awstats

四、配置 awstats

进入 /usr/local/awstats/tools/ 目录,执行配置脚本 awstats_configure.pl:

shell# cd /usr/local/awstats/tools/

shell# ./awstats_configure.pl

AWStats 分析 Nginx 访问日志

程序执行结束后,会在 /etc/awstats/ 目录下生成你的配置文件。然后编辑配置文件,修改 LogFile 参数,跟日志切割脚本中的日志路径对应起来:

LogFile=”/var/log/nginx/access_%YYYY-24%MM-24%DD-24.log”

注意,这里日期格式“%YYYY-24%MM-24%DD-24”,是指 24 小时之前的年月日,也就是昨天的日期。

测试

shell# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=yourwebsite

注意:-config= 后面的参数是你在执行 awstats_configure.pl 时输入的站点域名。

AWStats 分析 Nginx 访问日志

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-10/108362p2.htm

五、配置 awstats 生成静态页面

利用 awstats 的工具将统计的结果生成静态文件:

shell# mkdir -p /var/www/awstats

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

注意:-config= 后面的参数是你在执行 awstats_configure.pl 时输入的站点域名;-dir= 是统计结果静态文件的输出目录。

设置 crontab,每天凌晨 00:01 定时更新静态页面:

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

一般站长都不愿随便让人知道自己站的真实流量,所以要把 awstats 统计结果页面进行密码保护,这里需要用到 apache 自带的工具 htpasswd:

shell# yum -y install httpd-tools

shell# htpasswd -cd admin.pass admin

New password:

Re-type new password:

Adding password for user admin

把生成的密码文件 admin.pass 放到 nginx 主配置目录下(/etc/nginx/)。

在 nginx 配置扩展目录(/etc/nginx/conf.d/)下新建 awstats.conf 配置文件,内容如下:

server {

    listen      83;

    server_name  localhost;

    location ~ ^/awstats/ {# html 静态页面目录

        root  /var/www;

        index  index.html;

        access_log off;

        error_log off;

        charset gb2312;

        auth_basic “admin”;

        auth_basic_user_file admin.pass;

    }

 

    location ~ ^/icon/ {# 图标目录

        root  /usr/local/awstats/wwwroot;

        index  index.html;

        access_log off;

        error_log off;

    }

}

最后使 nginx 重读配置文件,用浏览器查看统计结果 http://yourhostname:83/awstats/awstats.yourwebsite.html

日志分析页面示例:

AWStats 分析 Nginx 访问日志

AWStats 的详细介绍 :请点这里
AWStats 的下载地址 :请点这里

AWStats 是在 Sourceforge 上发展很快的一个基于 Perl 的 WEB 日志分析工具。

它可以统计您站点的如下信息:

  • 访问量(UV),访问次数,页面浏览量(PV),点击数,数据流量等
  • 精确到每月、每日、每小时的数据
  • 访问者国家
  • 访问者 IP
  • Robots/Spiders 的统计
  • 访客持续时间
  • 对不同 Files type 的统计信息
  • Pages-URL 的统计
  • 访客操作系统浏览器等信息
  • 其它信息(搜索关键字等等)

AWStats 配置 (Windows + Apache) http://www.linuxidc.com/Linux/2013-09/89710.htm

CentOS 6.3 下 AWStats+GeoIP 实现查看网站访问归属地 http://www.linuxidc.com/Linux/2013-06/85984.htm

Nginx 日志分析 AWStats + JAWStats 安装配置 http://www.linuxidc.com/Linux/2013-06/85567.htm

经典日志分析工具 -AWStats http://www.linuxidc.com/Linux/2012-12/77080.htm

AWStats 日志系统配置文件和错误归纳 http://www.linuxidc.com/Linux/2012-12/75657.htm

使用 AWStats 分析网站日志 - 强大的日志分析工具 http://www.linuxidc.com/Linux/2012-11/74431.htm

下面是 AWStats 分析 Nginx 日志的操作步骤:

一、配置 nginx 日志格式

修改 nginx.conf 的日志格式,不然 awstats 无法分析。

    log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘

        ‘$status $body_bytes_sent “$http_referer” ‘

        ‘”$http_user_agent” “$http_x_forwarded_for”‘;

    access_log /var/log/nginx/access.log access;

注意,日志格式里的双引号不能漏了且每个参数之间是一个空格分隔,因为只要有细微的差别,awstats 就无法读取日志。

改好后,使 nginx 重读配置文件:

shell# nginx -t && nginx -s reload

二、自动切割 nginx 日志

每天晚上 23 点 59 分定时执行一个 shell 脚本来切割 nginx 日志。脚本内容如下:

#!/bin/bash

#

# Filename:    nginxCutLog.sh

# Author:      Qicheng

# Website:    http://www.linuxidc.com/

# Description: 切割 nginx 日志

# Notes:      设置 crontab,每天 23 点 59 分定时执行

#

ROOT_UID=0

if [“$UID” -ne “$ROOT_UID”];then

    echo “Error: 必须以 root 用户运行此程序!”

    exit 1

fi 

nginx_logs_dir=”/var/log/nginx”

nginx_pid_file=”/var/run/nginx.pid”

# 切割后的日志文件名,例如 access_20141022.log

nginx_log_today=”$nginx_logs_dir/access_`date +%Y%m%d`.log”

[-f “$nginx_log_today”] && exit 1

mv $nginx_logs_dir/access.log $nginx_log_today

# 给 nginx 发送 USR1 信号,使重新打开新的 access.log 日志文件

[-f $nginx_pid_file] && /bin/kill -USR1 $(cat $nginx_pid_file)

设置 crontab:

59 23 * * * /bin/bash /yourscriptpath/nginxCutLog.sh

三、安装 awstats

shell# wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz

shell# tar -zxvf awstats-7.0.tar.gz

shell# mv awstats-7.0 /usr/local/awstats

shell# mkdir -p /var/lib/awstats

四、配置 awstats

进入 /usr/local/awstats/tools/ 目录,执行配置脚本 awstats_configure.pl:

shell# cd /usr/local/awstats/tools/

shell# ./awstats_configure.pl

AWStats 分析 Nginx 访问日志

程序执行结束后,会在 /etc/awstats/ 目录下生成你的配置文件。然后编辑配置文件,修改 LogFile 参数,跟日志切割脚本中的日志路径对应起来:

LogFile=”/var/log/nginx/access_%YYYY-24%MM-24%DD-24.log”

注意,这里日期格式“%YYYY-24%MM-24%DD-24”,是指 24 小时之前的年月日,也就是昨天的日期。

测试

shell# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=yourwebsite

注意:-config= 后面的参数是你在执行 awstats_configure.pl 时输入的站点域名。

AWStats 分析 Nginx 访问日志

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-10/108362p2.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计5968字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中