Awstats分析Nginx日志
1.nginx日志格式设定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
参数说明:
$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
$remote_user :用来记录客户端用户名称;
$time_local : 用来记录访问时间与时区;
$request : 用来记录请求的url与http协议;
$status : 用来记录请求状态;成功是200,
$body_bytes_s ent :记录发送给客户端文件主体内容大小;
$http_referer :用来记录从那个页面链接访问过来的;
$http_user_agent :记录客户毒啊浏览器的相关信息;
2.nginx日志切割脚本
#!/bin/bash logs_path="/home/wwwlogs/" #设置日志文件存放目录 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主进程发信号重新打开日志
添加定时任务,每晚00:00切割,crontab -e
* * * bash /usr/local/nginx/nginx_log.sh
3.安装和配置awstats
wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz
tar -zxvf awstats-7.0.tar.gz
mv awstats-7.0 /usr/local/awstats
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
执行 tools 目录中的 awstats_configure.pl 配置向导,创建一个新的统计
cd /usr/local/awstats/tools
./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 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)
-----> 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 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:
>nginx #这里输入你要分析的域名,或是随便一个你易记的配置名并回车
接下来要定义你的配置文件存放的路径,可用默认
-----> 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'
Config file /etc/awstats/awstats.nginx.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=nginx
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.nginx.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=nginx
You can also build static report pages for 'nginx' with command: > perl awstats.pl -output=pagetype -config=nginx
Press ENTER to finish... #回车完成配置文件的创建
默认会生成一个名为awstats.nginx.conf配置文件在/etc/awstats/目录下,修改该配置文件的日志位置
vi /etc/awstats/awstats.nginx.conf
LogFile="/home/wwwlogs/access_%YYYY-0%MM-0%DD-24.log"
日志说明
# "LogFile" contains the web, ftp or mail server log file to analyze.
# You can also use tags in this filename if you need a dynamic file name
# depending on date or time (Replacement is made by AWStats at the beginning
# of its execution). This is available tags :
# %YYYY-n is replaced with digits year we were n hours ago
# %YY-n is replaced with digits year we were n hours ago
# %MM-n is replaced with digits month we were n hours ago
# %MO-n is replaced with letters month we were n hours ago
# %DD-n is replaced with day we were n hours ago
# %HH-n is replaced with hour we were n hours ago
# %NS-n is replaced with number of seconds at : since
# %WM-n is replaced with the week number in month (-)
# %Wm-n is replaced with the week number in month (-)
# %WY-n is replaced with the week number in year (-)
# %Wy-n is replaced with the week number in year (-)
# %DW-n is replaced with the day number in week (-, =sunday)
# use n= if you need (-, =monday)
# %Dw-n is replaced with the day number in week (-, =sunday)
# use n= if you need (-, =monday)
# Use for n if you need current year, month, day, hour...
n表示时间间隔,我这里就是分析距离当前今天24小时前(昨日)的日志,改成48就是前两天的日志
创建一个awstats用于记录数据的目录
mkdir -p /var/lib/awstats
然后运行awstats的wwwroot目录中的awatsts.pl来测试一下
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx
如果看到类似下面的提示就说明配置文件都正确了
Create/Update database for config "/etc/awstats/awstats.nginx.conf" by AWStats version 7.0 (build 1.964) From data
in log file "/home/wwwlogs/access.log_20130727"... Phase : First bypass old records, searching new record...
Direct access after last parsed record (after line ) Jumped lines in file: Found already parsed records.
Parsed lines in file:
Found dropped records,
Found comments,
Found blank records,
Found corrupted records,
Found old records,
Found new qualified records
4.Nginx 对 Perl 支持并不好,所以这里利用 awstats 的工具将统计的结果生成静态文件
首先在 webroot 目录下创建一个文件夹。例:/home/www/awstats
mkdir -p /home/www/awstats/nginx
写一个脚本,定期执行让 Awstats 把静态页面生成到该目录中,vi /usr/local/nginx/sbin/awstats.sh
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update
\ -config=nginx -lang=cn -dir=/home/www/awstats/nginx
\ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
/usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 静态页面生成工具
-update -config=nginx 更新配置项
-lang=cn 语言为中文
-dir=/home/www/awstats 统计结果输出目录
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径
添加脚本执行权限
chmod +x /usr/local/nginx/sbin/awstats.sh
运行脚本如果出现生成一堆网页类似
Launch update process : "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx -update -configdir= ......
Build keywords page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx -staticlinks
-lang=cn -output=keywords Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl"
-config=nginx -staticlinks -lang=cn -output=errors404 files built.
Main HTML page is 'awstats.nginx.html'.
则说明成功了,接下来就可以加入定时任务了crontab -e
* * * /usr/lcoal/nginx/sbin/awstats.sh #每天凌晨1:00执行
5.修改nginx配置文件,添加日志分析结果站点
server
{
listen ;
server_name 192.168.75.28;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/awstats;
location ^~ /icon/ {
root /usr/local/awstats/wwwroot;
index index.html;
access_log off;
}
autoindex on;
access_log off;
}
这样访问http://192.168.75.28就可以看到统计结果静态页面了。
Awstats分析Nginx日志的更多相关文章
- 烂泥:利用awstats分析nginx日志
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 昨天把nginx的日志进行了切割,关于如何切割nginx日志,可以查看<烂泥:切割 ...
- 使用awstats分析nginx日志
1.awstats介绍 本文主要是记录centos6.5下安装配置awstats,并统计nginx访问日志 1.1 awstats介绍 awstats是一款日志统计工具,它使用Perl语言编写,可统计 ...
- Nginx配置Awstats分析Nginx日志笔记
1.修改Nginx日志格式: log_format json '$remote_addr - $remote_user [$time_local] "$request" ' ...
- awstat分析nginx日志
awstat分析nginx日志 http://lxw66.blog.51cto.com/5547576/1323712 server{ listen ; server_name localhost; ...
- elk实战分析nginx日志文档
elk实战分析nginx日志文档 架构: kibana <--- es-cluster <--- logstash <--- filebeat 环境准备:192.168.3.1 no ...
- elk平台分析nginx日志的基本搭建
一.elk套件介绍 ELK 由 ElasticSearch . Logstash 和 Kiabana 三个开源工具组成.官方网站: https://www.elastic.co/products El ...
- 一天,python搞个分析NGINX日志的脚本
准备给ZABBIX用的. 统计接口访问字次,平均响应时间,4XX,5XX次数 以后可以再改进.. #!/usr/bin/env python # coding: utf-8 ############# ...
- 利用python分析nginx日志
最近在学习python,写了个脚本分析nginx日志,练练手.写得比较粗糙,但基本功能可以实现. 脚本功能:查找出当天访问次数前十位的IP,并获取该IP来源,并将分析结果发送邮件到指定邮箱. 实现前两 ...
- hive分析nginx日志之UDF清洗数据
hive分析nginx日志一:http://www.cnblogs.com/wcwen1990/p/7066230.html hive分析nginx日志二:http://www.cnblogs.com ...
随机推荐
- OS X 使用技巧——在Finder窗口标题栏上显示路径
Finder窗口默认显示当前文件夹的名称或当前所在的模式(例如AirDrop).如果想要显示路径(用User/[当前用户账号名称]/Documents 替代以前显示的Documents),打开终端并运 ...
- PP生产订单的BADI增强 WORKORDER_UPDATE
METHOD if_ex_workorder_update~before_update. *---------------------->增强1 开始* "当生产订单类型为PP01时, ...
- 1.项目开发-->Memcached之ASP.NET实现
1.Memcached安装 第一步:下载windows稳定版Memcached 1.2.1 win32,解压后,将memcached.exe文件放到自己想存放的盘符内,例如C:\Windows\Sys ...
- MyEclipse 关闭拼写检查、JavaScript的检查Build、xml、JSP的Bulid检查
前言 MyEclipse 的拼写检查.JavaScript的检查Build.xml.JSP的Bulid检查很讨厌,有时不仅会一直build卡住,而且明明是对的它却报错,示例: 关闭方法 1.关闭拼写检 ...
- JS--中的 Cookie 与存储
Cookie 主要是在客户端进行一些简单的数据存储等,使用来提供本地化存储的脚本功能.Cookie 的处理环境本身是需要在服务器下进行的,但是现在的大部分浏览器都已经支持Cookie本地化的存储于处理 ...
- C++ Tempatet之模板模型
模板一共有三种类型: 1.第一种包含模型:包含模型是讲模板的定义和声明都放在头文件里(注:一般我们写的代码是将声明放在头文件里,实现放在cpp里,防止产生两份实现代码) 缺点:包含模型会增加代码的量. ...
- 【BZOJ】【3007】拯救小云公主
思路题 我的naive的做法是二分答案+判定是否有路径可走……但是没有正确理解[走的方向任意]这句话…… 其实就是说想咋走咋走= =360°无死角乱走…… 所以其实是个平面上的问题…… 我们可以换个方 ...
- 【bzoj1009】[HNOI2008]GT考试
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3018 Solved: 1856[Submit][Statu ...
- 户外物理渗透:终端机,客户端的web测试思路
现在的客户端界面越做越好看了,很多用到了web技术,轻便.界面炫.更新快,但是这样web的缺点也就出来了,就是不稳定,容易受用户等因素影响. 因为很多客户端web是内嵌的,内部通信,所以很多对安全的考 ...
- Environment.SpecialFolder.CommonApplicationData
private void button1_Click(object sender, EventArgs e) { var path=Environment.GetFolderPath(Environm ...