热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

设置Nginx日志自动割切并使用awstats分析

说明:awstats可以分析apache日志,同样也可以分析nginx日志。由于Awstats是Perl写的,从awstats的文档来看,它对ApacheHTTPServer的支持是非常完美的。因此apache可以直接打开Perl程序的网页查看统计。但nginx对Perl支持并不好,所以我们需要利用aws
说明:
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 files\apache group\apache\conf\httpd.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


推荐阅读
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • mac php错误日志配置方法及错误级别修改
    本文介绍了在mac环境下配置php错误日志的方法,包括修改php.ini文件和httpd.conf文件的操作步骤。同时还介绍了如何修改错误级别,以及相应的错误级别参考链接。 ... [详细]
  • 一句话解决高并发的核心原则
    本文介绍了解决高并发的核心原则,即将用户访问请求尽量往前推,避免访问CDN、静态服务器、动态服务器、数据库和存储,从而实现高性能、高并发、高可扩展的网站架构。同时提到了Google的成功案例,以及适用于千万级别PV站和亿级PV网站的架构层次。 ... [详细]
  • Linux下部署Symfoy2对app/cache和app/logs目录的权限设置,symfoy2logs
    php教程|php手册xml文件php教程-php手册Linux下部署Symfoy2对appcache和applogs目录的权限设置,symfoy2logs黑色记事本源码,vsco ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 本文介绍了Linux系统中正则表达式的基础知识,包括正则表达式的简介、字符分类、普通字符和元字符的区别,以及在学习过程中需要注意的事项。同时提醒读者要注意正则表达式与通配符的区别,并给出了使用正则表达式时的一些建议。本文适合初学者了解Linux系统中的正则表达式,并提供了学习的参考资料。 ... [详细]
  • 目录浏览漏洞与目录遍历漏洞的危害及修复方法
    本文讨论了目录浏览漏洞与目录遍历漏洞的危害,包括网站结构暴露、隐秘文件访问等。同时介绍了检测方法,如使用漏洞扫描器和搜索关键词。最后提供了针对常见中间件的修复方式,包括关闭目录浏览功能。对于保护网站安全具有一定的参考价值。 ... [详细]
  • Apache Shiro 身份验证绕过漏洞 (CVE202011989) 详细解析及防范措施
    本文详细解析了Apache Shiro 身份验证绕过漏洞 (CVE202011989) 的原理和影响,并提供了相应的防范措施。Apache Shiro 是一个强大且易用的Java安全框架,常用于执行身份验证、授权、密码和会话管理。在Apache Shiro 1.5.3之前的版本中,与Spring控制器一起使用时,存在特制请求可能导致身份验证绕过的漏洞。本文还介绍了该漏洞的具体细节,并给出了防范该漏洞的建议措施。 ... [详细]
  • PHP组合工具以及开发所需的工具
    本文介绍了PHP开发中常用的组合工具和开发所需的工具。对于数据分析软件,包括Excel、hihidata、SPSS、SAS、MARLAB、Eview以及各种BI与报表工具等。同时还介绍了PHP开发所需的PHP MySQL Apache集成环境,包括推荐的AppServ等版本。 ... [详细]
  • 本文介绍了在无法联网的情况下,通过下载rpm包离线安装zip和unzip的方法。详细介绍了如何搜索并下载合适的rpm包,以及如何使用rpm命令进行安装。 ... [详细]
  • 程序员如何选择机械键盘轴体?红轴和茶轴对比
    本文介绍了程序员如何选择机械键盘轴体,特别是红轴和茶轴的对比。同时还介绍了U盘安装Linux镜像的步骤,以及在Linux系统中安装软件的命令行操作。此外,还介绍了nodejs和npm的安装方法,以及在VSCode中安装和配置常用插件的方法。最后,还介绍了如何在GitHub上配置SSH密钥和git的基本配置。 ... [详细]
  • Linux一键安装web环境全攻略
    摘自阿里云服务器官网,此处一键安装包下载:点此下载安装须知1、此安装包可在阿里云所有Linux系统上部署安装,此安装包包含的软件及版本为& ... [详细]
  • 本文主要介绍关于linux文件描述符设置,centos7设置文件句柄数,centos7查看进程数的知识点,对【Linux之进程数和句柄数】和【linux句柄数含义】有兴趣的朋友可以看下由【东城绝神】投 ... [详细]
author-avatar
淑富世廷789
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有