awstats分析nginx日志

为了让我们能够对web服务器有一个充分的了解,以便我们对服务器架构做出相应的调整,给公司节约更多的成本;我们需要了解web服务器的pv,uv,独立ip等信息,同时也能够给用户带来更高的体验。

下面我们利用awstats来分析下nginx日志。

1.首先让nginx生成awstats能够识别的日志。(虽然系统自带的logrotate能够每天自动切割日志,但是生成的日志时间并不合适,导致我们不能随时查看统计数据)

通过shell脚本来切割日志,具体内容如下:

#!/bin/bash
mv /var/log/nginx/access.log /var/log/nginx/access_`date +%Y%m%d`.log
killall -s USR1 nginx #使用USR1参数通知Nginx进程切换日志文件
我们通过计划任务设置此脚本每天晚上12点之前来切割当天日志,脚本名称为awstats_log.sh。

我们先进行日志切割,为后面做准备。

bash /root/awstats_log.sh

2.安装awstats

cd /usr/local/src

tar -zxvf awstats-6.5.tar.gz 

cd awstats-6.5

mkdir -p /usr/local/awstats

cp -rf /usr/local/src/awstats-6.5/* /usr/local/awstats

cd /usr/local/awstats

perl 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   回车

-----> 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:
#>60.191.140.176  回车

-----> 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=<span style="font-size: 11px; background-color: rgb(240, 240, 240);">60.191.140.176</span>
               #回头把该命令填入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.<span style="font-size: 11px; background-color: rgb(240, 240, 240);">6</span><span style="font-size: 11px; background-color: rgb(240, 240, 240);">0.191.140.176</span><span style="font-size: 11px; background-color: rgb(240, 240, 240);">.conf  </span>
            #新配置文件所在的路径
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for '6<span style="font-size: 11px; background-color: rgb(240, 240, 240);">0.191.140.176</span><span style="font-size: 11px; background-color: rgb(240, 240, 240);">' with command:</span>
> perl awstats.pl -update -config=www.moabc.net
You can also build static report pages for '6<span style="font-size: 11px; background-color: rgb(240, 240, 240);">0.191.140.176</span><span style="font-size: 11px; background-color: rgb(240, 240, 240);">' with command:</span>
> perl awstats.pl -output=pagetype -config=<span style="font-size: 11px; background-color: rgb(240, 240, 240);">6</span><span style="font-size: 11px; background-color: rgb(240, 240, 240);">0.191.140.176</span>
Press ENTER to finish...

3.配置awstats配置文件

vim /etc/awstats/awstats.60.191.140.176.conf

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

至此此处后,DirData="/var/lib/awstats",在执行awstats_buildstaticpages.pl生成静态文件时会报错,这是由于没有/var/lib/awstats文件,我们只需创建即可。

4.生成awstats静态页面

# mkdir  /data/awstats
# /usr/local/awstats/tools/awstats_buildstaticpages.pl -update  \ 
-config=60.191.140.176 -lang=cn -dir=/data/awstats  \
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

5.在nginx中配置访问目录。

cd /etc/nginx/conf.d/awstats.conf

server {
   listen       83;
   server_name  60.191.140.176;
   location / {
#        proxy_pass  http://127.0.0.1:81;
#       proxy_set_header Host $host;
#       proxy_set_header X-Real-IP $remote_addr;
#       proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
#       proxy_set_header Accept-Encoding '';
#
#    }
#   location  ~* .*.(gif|png|jpg|jpeg|bmp|swf)$ {
#       proxy_pass http://squid;
#    }
#location ~ ^/awstats/ {            # html 静态页面目录
        root   /data/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://60.191.140.176:83/awstats.60.191.140.176.html

yout统计指标说明:

  • 参观者:按来访者不重复的IP统计,一个IP代表一个参观者,也就是我们所说的pv;
  • 参观次数:一个参观者可能1天之内参观多次(比如:上午一次,下午一次),所以按一定时间内(比如:1个小时),不重复的IP数统计,参观者的访问次数;
  • 网页数:不包括图片,CSS, JavaScript文件等的纯页面访问总数,但如果一个页面使用了多个帧,每个帧都算一个页面请求;
  • 文件数:来自浏览器客户端的文件请求总数,包括图片,CSS,JavaScript等,用户请求一个页面是,如果页面中包含图片等,所以对服务器会发出多次文件请求,文件数一般远远大于文件数;
  • 字节:传给客户端的数据总流量;
  • 来自REFERER中的数据:日志中的参考(REFERER)字段,记录了访问相应网页之前地址,因此如果用户是通过搜索引擎的搜索结果点击进入网站的,日志中就会有用户在相应搜索引擎的查询地址,这个地址中就可以通过解析将用户查询使用的关键词提取出来:
    比如:
    2003-03-26 15:43:58 123.123.123.123 - GET /index.html 200 192 HTTP/1.1 Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0) http://www.google.com/search?q=chedong
    AWStats在搜索引擎的关键短语和关键词统计方面的功能还是比较完整的:可以对全世界3百多种机器爬虫进行识别,并且可以识别大部分主流国际化搜索引擎和很多地区的本地语言搜索引擎
综上所述,我们需要设置计划任务自动执行:

50 11 * * * /bin/bash /root/awstats_log.sh

00 01 * * * /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=60.191.140.176 -lang=cn -dir=/data/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

另我们设置认证来保护日志访问:

/usr/bin/htpasswd -c passwordfile  ygd

将此文件保存到安全的地方,mv passwordfile /root

设置访问目录权限:

   location / {
        root   /data/awstats;
        index  index.html;
        access_log off;
        error_log off;
        charset gb2312; #最好把默认编码改成 gb2312避免浏览器因自动编码出现乱码的情况

        auth_basic “admin”;

        auth_basic_user_file /root/passwordfile;

}

认证配置完成后,输入用户名密码出现“500错误”,这是由于nginx 0.6.7版本必须将密码验证文件放到nginx.conf所在的文件目录下,因此我们只需

mv /root/passwordfile /etc/nginx/

nginx -s reload 

再次访问成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值