博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用awstats分析nginx日志
阅读量:7208 次
发布时间:2019-06-29

本文共 5919 字,大约阅读时间需要 19 分钟。

1、awstats介绍

AWStats是一款免费的功能强大的工具,它以图形方式生成高级web、流、FTP或邮件服务器统计信息。可以以CGI或从命令行方式运行,并在几个图形网页中显示日志包含的所有可能信息。本文主要介绍centos6.9下安装、配置awstats,并统计nginx访问日志

2、环境准备

1.1 nginx日志格式设定:

[root@web data]# vim /usr/local/nginx/conf/nginx.conflog_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 access;

如果使用了网站使用了CDN或者代理, 则格式应该有所调整,以便第一个参数为真实客户端的地址:

[root@web data]# vim /usr/local/nginx/conf/nginx.conflog_format access '$http_x_forwarded_for -$remote_user [$time_local] "$request" ''$status $body_bytes_sent"$http_referer" ''"$http_user_agent" $remote_addr';access_log log/access.log access;

2.2 nginx日志切割:

nginx的日志不能像apache一样去用cronolog工具进行切割,这里我们就写一个脚本,让它可以在每天00:00自动执行,切割昨天的日志(交由awstats分析),并压缩前天的日志(压缩日志可减小存储空间)。

[root@web data]# vim /data/scripts/cut_nginx_log.sh#!/bin/bash# Settings logs_path="/data/wwwlogs"back_path="/data/backup/wwwlogs"log_file=access.logdate_dir=${back_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/ gzip_date_dir=${back_path}/$(date -d "-2 day" +"%Y")/$(date -d "-2 day" +"%m")/$(date -d "-2 day" +"%d")/ # Settings Endmkdir -p $date_dir[ -d $logs_path ] && cd $logs_path || exit 1[ -f $log_file ] && /bin/mv $log_file $date_dir ||exit 1[ $? -eq 0 ] && /usr/local/nginx/sbin/nginx -s reopen [ -f ${gzip_date_dir}*.log ] && /usr/bin/gzip ${gzip_date_dir}*.log || exit 1

执行crontab -e 配置00:00进行切割即可

3、awstats安装配置

3.1安装

cd /usr/local/srcwget http://prdownloads.sourceforge.net/awstats/awstats-7.6.tar.gztar xvf awstats-7.6.tar.gzmv awstats-7.6 /usr/local/awstatschown -R root:root /usr/local/awstatschmod +x /usr/local/awstats/tools/*.plchmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl

3.2生成web站点配置文件

生成的配置文件,默认存储在/etc/awstats下, 如下为生产步骤:

[root@web local]# cd /usr/local/awstats/tools[root@web tools]# ./awstats_configure.pl......Enter full config file path of your Web server.Example: /etc/httpd/httpd.confExample: /usr/local/apache2/conf/httpd.confExample: c:\Program files\apache group\apache\conf\httpd.confConfig file path ('none' to skip web server setup): > none    #因为我们是nginx服务器, 此处填写noneYour web server config file(s) could not be found.You will need to setup your web server manually to declare AWStatsscript 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/profilefile (required if first install) [y/N] ? y             #新建配置文件-----> Define config file name to createWhat is the name of your web site or profile analysis ?Example: www.mysite.comExample: demoYour web site, virtual server or profile name:> log.test.com      #这个名字可以随便写,会生成以此名字为基础的配置文件-----> Define config file pathIn which directory do you plan to store your config file(s) ?Default: /etc/awstatsDirectory path to store config file(s) (Enter for default):>       #直接回车-----> Create config file '/etc/awstats/awstats.log.test.com.conf' Config file /etc/awstats/awstats.log.test.com.conf created.-----> Add update process inside a schedulerSorry, 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=log.test.comOr if you have several config files and prefer having only one command:/usr/local/awstats/tools/awstats_updateall.pl nowPress ENTER to continue... 以下直接回车即可

默认会在/etc/awstats目录下生成一个/etc/awstats/awstats.test.com.conf的配置文件

3.3 修改web站点配置文件

vim /etc/awstats/awstats.test.com.confLogFile="/data/backup/wwwlogs/%YYYY-24/%MM-24/%DD-24/zhibo.log"...DirData="/data/awstats"

LogFile是对应上面Nginx日志切割所生成的目录存放位置,注意awstats的年月日格式,-24表示昨天的日志。此处年月日都需要-24, 否则跨年或跨月时会报错。

DirData指数据文件保存目录, 这个文件需要真实存在,否则会报错
分析的执行顺序是:
Nginx 产生日志 –> 日志切割 –> Nginx 继续产生日志 –> 另存切割日志 –> 交由Awstats统计 –> 生成结果

3.4 生成静态统计数据

创建记录数据的目录:

mkdir -p /data/awstats
生成静态文件
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=log.test.com -lang=cn -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -dir=/data/awstats/

参数说明:

/usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats静态页面生成工具-update -config=log.test.com 更新配置项-lang=cn 语言为中文-dir=/data/awstats 统计结果输出目录-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径

3.5 修改nginx配置文件,添加日志分析结果站点(添加虚拟主机)

vim /usr/local/nginx/conf/vhost/awstats.conf

#cat /usr/local/nginx/conf/vhosts/awstats.conf
server {
listen 8080;
server_name logs.yourdomain;
access_log /home/logs/awstats_access.log;
root /data/awstats; #html存放目录
index index.html;

location /awstats/classes/ {    alias /usr/local/awstats/wwwroot/classes/;}location /awstats/css/ {    alias /usr/local/awstats/wwwroot/css/;}location /awstats/icon/ {    alias /usr/local/awstats/wwwroot/icon/;}location /awstats-icon/ {    alias /usr/local/awstats/wwwroot/icon/;}location /awstats/js/ {    alias /usr/local/awstats/wwwroot/js/;}location /awstatsicons/ {    alias /usr/local/awstats/wwwroot/icon/;}location ~ ^/icon/

{

root /usr/local/awstats/wwwroot;
index index.html;
access_log off;
error_log off;
}
}

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

vim /data/scripts/awstats.sh

#!/bin/bashback_path="/data/backup/wwwlogs"log_file=access.logdate_dir=${back_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/ if [ -f &data_dir$log_file ]then         /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=log.c.com -lang=cn -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -dir=/data/awstats/else    exit 1

在网站访问低谷,创建定时任务

crontab -e0       5       *       *       *       cd /data/scripts && sh awstats.sh >/dev/null 2>&1

如果你已经切割了日志,现在就可以通过http://ogs.yourdomain:8080 来访问静态页面了

转载于:https://blog.51cto.com/songky/2118088

你可能感兴趣的文章
支援日本/厄瓜多尔震区 Skype推免费通话
查看>>
转账给张三,钱却被李四收到,如何狙击凶险的 App 漏洞?——专访娜迦CTO玩命...
查看>>
有些车已经不能再买了!因为国五排放标准就要来了!
查看>>
怎样为企业挑选正确的EDR解决方案
查看>>
《编程原本 》一2.2 轨道
查看>>
社交媒体广告看不出来?Instagram加标签让你一目了然
查看>>
Facebook已经过时,蜂巢新网络崛起
查看>>
智能城市技术能够更好地改善日常生活
查看>>
大数据会如何影响VC领域?
查看>>
人力资源SaaS软件“乐才Joy HR”获数百万元战略融资
查看>>
借攻防演习提升企业安全能力
查看>>
美国国土安全部发布物联网安全最佳实践
查看>>
迅游科技拟作价27亿元收购移动应用开发商狮之吼
查看>>
最新桌面操作系统份额统计:Windows占比91.59%
查看>>
华平助广东海事局构建水上搜救应急指挥系统
查看>>
投资数据中心所考虑的重要因素
查看>>
第一太阳能公司(First Solar)在罗斯资本公司的评级上调
查看>>
德国软件巨头SAP旗下风投基金募集10亿美元 专门投资科技初创公司
查看>>
保险报业携手万丈金数 探索大数据应用升级
查看>>
诺基亚正式收购阿朗:物联网时代再出发
查看>>