2014-04-07 09:14:01
来 源
ITJS.CN
Nginx
本文介绍设置Nginx日志自动割切并使用awstats分析,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。
说明:

awstats可以分析apache日志,同样也可以分析nginx日志。由于Awstats是Perl写的,从 awstats 的文档来看,它对 Apache HTTP Server 的支持是非常完美的。因此apache可以直接打开 Perl程序的网页查看统计。 但nginx对Perl支持并不好,所以我们需要利用awstats的工具将统计结果生成静态文件,方便nginx的查看。

本文将详细介绍自动定时切割nginx访问日志,并使用awstats来定时分析nginx日志的实现方法。

实现:

一. 设置nginx日志格式,日志按天切割部分

1.设置nginx日志格式,使更好的支持awstats。默认的nginx access格式就可以了。

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  /data1/logs/access.log  access;

 

2.编写shell,定时切割每天的访问日志,压缩一周前的访问日志

# cat /root/logrotate.sh //填写以下内容

#!/bin/bash

# This script will run at 00:00,cut yesterday's logs and gzip log files a week ago.

# yesterday's logs for awstats

source_path="/var/log/nginx/"

logs_path="/home/awstats/logs/"

date_dir=${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/

gzip_date_dir=${logs_path}$(date -d "1 week ago" +"%Y")/$(date -d "1 week ago" +"%m")/$(date -d "1 week ago" +"%d")/

mkdir -p $date_dir

mv ${source_path}*.access.log $date_dir

kill -USR1 `cat /usr/local/nginx/nginx.pid`

/usr/bin/gzip ${gzip_date_dir}*.access.log

 

3.修改crontal,使日志切割每天凌晨00:00运行

# crontab -e  //添加以下内容

00 00 * * * /bin/bash /root/logrotate.sh

二. awstats的安装与配置

1.下载与安装

# wget http://sourceforge.net/projects/awstats/files/AWStats/7.1.1/awstats-7.1.1.tar.gz/download

# tar -zxvf awstats-7.1.1.tar.gz

# mv awstats-7.1.1 /usr/local/awstats

//修改权限,wget下载下来的包权限转为为1000,需要修改权限,使.pl的文件可以运行了。

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

# chmod -R =rwX /usr/local/awstats

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

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

 

2.针对网站,创建配置文件

# cd /usr/local/awstats/tools  //切换到awstats/tools目录运行。否则会有一些关于标准目录的提示。

# ./awstats_configure.pl  //运行./awstats_configure.pl配置向导,创建一个新的统计

-----> Running OS detected: Linux, BSD or Unix

-----> 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 filesapache groupapacheconfhttpd.conf

>

#这里输入none并回车,因为我们没有使用apache

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] ?

#这里是说没有web server配置,是否新建一个。选y,创建一个新的配置文件

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:

>

#这里输入你要分析的域名,如www.server110.com,回车

-----> 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):

>

#定义配置文件存放路径,这里直接回车,使用默认路径/etc/awstats

-----> Create config file '/etc/awstats/awstats.www.server110.com.conf'

 Config file /etc/awstats/awstats.www.server110.com.conf created.

-----> 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=www.server110.com

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.www.server110.com.conf

You should have a look inside to check and change manually main parameters.

You can then manually update your statistics for 'yuyuanchun.com' with command:

> perl awstats.pl -update -config=www.server110.com

You can also build static report pages for 'www.server110.com' with command:

> perl awstats.pl -output=pagetype -config=www.server110.com

Press ENTER to finish...

#回车完成配置文件的创建

 

3.修改/etc/awstats/awstats.www.server110.com.conf,将LogFile="/var/log/httpd/mylog.log" 修改为:

LogFile="/home/www/logs/%YYYY-24/%MM-24/%DD-24/server110.com.access.log"

//YYYY-24这里的24是指24小时前,即昨天;server110.com.access.log为你nginx的访问日志,注意修改。

4.测试新的统计是否配置正确,这一步主要是对访问日志进行统计分析

# mkdir -p /var/lib/awstats  //创建一个awstats用于记录数据的目录,不懂是不是一定要。

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

运行awstats安装目录wwwroot下的awatsts.pl测试。如果看到类似下面的提示就说明配置文件都正确了。注意这里-config=www.server110.com要与上一步创建配置文件时输入的域名一致。

Create/Update database for config "/etc/awstats/awstats.www.server110.com.conf" by AWStats version 7.1.1 (build 1.964)

From data in log file "/home/www/logs/2010/07/24/server110.access.log"...

Phase 1 : First bypass old records, searching new record...

Direct access after last parsed record (after line 43260)

Jumped lines in file: 43260

 Found 43260 already parsed records.

Parsed 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

 

5.利用Awstats工具生成静态页面,方便nginx查看。统计分析完成后,结果还在 Awstats 的数据库中。apache可以直接打开 Perl 程序的网页查看统计。 但本文开始时已经提到,Nginx 对 Perl 支持并不好,因此我们需要利用awstats的工具将统计的结果生成静态文件

a.新建目录/home/www/awstats,定期执行shell,让 Awstats 把静态页面生成到该目录中

# mkdir -p /home/www/awstats

# vim /root/awstats.sh  //然后输入以下内容

#!/bin/bash

mkdir -p /home/www/awstats/server110.com

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

具体参数说明如下:

/usr/local/awstats/tools/awstats_buildstaticpages.pl    Awstats静态页面生成工具

-update -config=www.server110.com        更新配置项

-lang=cn         语言为中文

-dir=/home/www/awstats/server110.com    统计结果输出目录

-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl      Awstats 日志更新程序路径。

# chmod +x /root/awstats.sh

# /root/awstats.sh

如果看到输出信息,那就说明成功了,例如:

Build keywords page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=www.server110.com -staticlinks -lang=cn -output=keywords

Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=www.server110.com -staticlinks -lang=cn -output=errors404

21 files built.

Main HTML page is 'awstats.www.server110.com.html'.

 

b.修改crontab,让awstats输出静态文件自动运行

# crontab -e  //添加以下内容

00 1 * * * /root/awstats.sh

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。