nginx 日志格式设定,去掉如下行#号
#vim /usr/local/nginx/conf/nginx.conf
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 log/access.log main;

参数说明:

$remote_addr与$http_x_forwarded_for:用以记录客户端的ip地址;

$remote_user:用来记录客户端用户名称;

$time_local:用来记录访问时间与时区;

$request:用来记录请求的url与http协议;

$status:用来记录请求状态;成功是200;

$body_bytes_sent:记录发送给客户端文件主体内容大小;

$http_referer:用来记录从哪个页面链接访问过来的;

$http_user_agent:记录客户端浏览器的相关信息;

nginx日志切割脚本

#vim /usr/local/nginx/nginx_log.sh

#!/bin/bash
logs_path="/usr/local/nginx/logs/" #设置日志文件存放目录
pid_path="/usr/local/nginx/logs/nginx.pid" #设置pid文件
mv${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
#重命名日志文件,将access.log切割为昨天的日志文件,因为要在每天零点切割,所有日志文件格式是前一天的日期,如access_20130728.log
kill-USR1 `cat${pid_path}` #向nginx主进程发信号重新打开日志

安装和配置awstats

yum方式安装
sudo yum -y install awstats
如果不能yum安装,请运行
sudo rpm -ivh http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
另外安装一下GeoIP,一会可以分析IP的国家
yum install GeoIP GeoIP-data GeoIP-devel perl-Geo-IP -y
如果安装不成功,可以运行如下方法
rpm -ivh https://epel.mirror.angkasa.id/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install mod_geoip GeoIP GeoIP-devel GeoIP-data zlib-devel   

sudo chown -R sysadmin:sysadmin /var/lib/awstats/
sudo chown -R sysadmin:sysadmin /var/www/awstats/

执行tools目录中的awstats_configure.pl配置向导,创建一个新的统计

/usr/share/awstats/tools/awstats_configure.pl

将会有如下一些提示:

-----> Running OS detected: Linux, BSD or Unix
Warning: AWStats standard directory on Linux OS is '/usr/local/awstats'.
If you want to use standard directory, you should first move all content
of AWStats distribution from current directory:
/home/sysadmin
to standard directory:
/usr/local/awstats
And then, run configure.pl from this location.
Do you want to continue setup from this NON standard directory [yN] ? y
-----> 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  #这里填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)

-----> 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   #这里选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:
>www.a.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.nginx.conf'
Configfile /etc/awstats/awstats.nginx.conf created.
-----> Add updateprocess inside a scheduler Sorry, configure.pl does not support automatic addto cron yet.
You can do it manually by adding the following command to yourcron: /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx
Or if you have several config files and prefer having only onecommand: /usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...#按回车继续
A SIMPLE config file has been created: /etc/awstats/awstats.nginx.conf You should have a lookinside to check and change manually main parameters.
Youcan then manually update your statistics for'yuyuanchun.com' withcommand: > perl awstats.pl -update -config=nginx
Youcan also build static report pages for'nginx' with command:> perl awstats.pl -output=pagetype -config=nginx
PressENTER to finish... #回车完成配置文件的创建

默认会生成一个名为awstats.www.a.com.conf配置文件在/etc/awstats/目录下,修改该配置文件的日志位置

vim /etc/awstats/awstats.www.a.com.conf 
LogFile="/usr/share/awstats/tools/logresolvemerge.pl /var/log/nginx/access_log* |"

这里是对应上面Nginx日志切割所生成的目录存放位置,注意awstats的年月日格式,-24表示昨天的日志,-0表示当前的,我这里因为要分析多种不同的日志文件,所以采用了*| 的方式

分析的执行顺序是:

Nginx产生日志->日志切割->Nginx继续产生日志->另存切割日志->交由Awstats统计->生成结果


默认awstats用于记录数据的目录在:

/var/lib/awstats

Nginx对Perl支持并不好,所以这里利用awstats的工具将统计的结果生成静态文件,首先创建一个文件,例:

mkdir  /var/www/awstats

生成静态文件:

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

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

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

-lang=cn    语言为中文

-dir=/var/www/awstats  统计输出结果

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

修改nginx配置文件,添加日志分析结果站点

server {
        listen 8080;
        server_name awstats.a.com;
        access_log /var/log/nginx/awstats.access_log main;
        error_log /var/log/nginx/awstats.error_log info;
        root /var/www/awstats;
        index index.html;

        # Restrict access
        #auth_basic "Restricted";
        #auth_basic_user_file /etc/awstats/htpasswd;

        # Dynamic stats.
        location ~ ^/cgi-bin/(awredir|awstats)\.pl {
                gzip off;
                include         fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME /usr/share/awstats/wwwroot/cgi-bin/fcgi.php;
                fastcgi_param X_SCRIPT_FILENAME /usr/share/awstats/wwwroot$fastcgi_script_name;
                fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;
                include fastcgi_params;
        }


        # Static awstats files: HTML files stored in DOCUMENT_ROOT/awstats/
        location /classes/ {
                alias /usr/share/awstats/wwwroot/classes/;
        }

        location /css/ {
        alias /usr/share/awstats/wwwroot/css/;
        }

        location /icon/ {
                alias /usr/share/awstats/wwwroot/icon/;
        }

        location /awstats-icon/ {
        alias /usr/share/awstats/wwwroot/icon/;

        }

        location /js/ {
                alias /usr/share/awstats/wwwroot/js/;
        }

}

重新加载nginx

sudo nginx -t    #检查nginx配置文件是否有误
sudo nginx -s reload    #加载

添加定时任务,每晚切割,分析nginx日志

#!/bin/bash

/bin/bash /opt/scripts/filetransfer.sh   #这个是从本机的/tmp目录拷贝到指定目录下的一个脚本
#=====================================================================
echo -e "\033[32m The first is www,now time is `date`\033[0m"

/usr/share/awstats/tools/awstats_buildstaticpages.pl -update -config=www.a.com -lang=cn -dir=/var/www/awstats -a
wstatsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl
#=====================================================================
echo -e "\033[32m The first is wx,now time is `date`\033[0m"

/usr/share/awstats/tools/awstats_buildstaticpages.pl -update -config=wx.a.com -lang=cn -dir=/var/www/awstats -aw
statsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl
#=======================================================================
echo -e "\033[32m The first is app,now time is `date`\033[0m"

/usr/share/awstats/tools/awstats_buildstaticpages.pl -update -config=app.a.com -lang=cn -dir=/var/www/awstats -a
wstatsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl
crontab -e
01 00 * * * /bin/bash /opt/scripts/awstats.sh >> /tmp/awstats.log