一,日志的切割
复制代码 代码示例:
#!/bin/bash
logs_path="/
nginx/logs/"
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /nginx/logs/nginx.pid`
其中 "/nginx/logs" 指的是nginx的log日志文件所在目录,生成的日志文件是以昨天日期命名的。
每天自动分割,在crontab中加入:
1 0 * * * sh /home/zyf/sh/cut_nginx_log.sh
每天的0点1分把nginx日志重命名为日期格式,并重新生成今天的新日志文件。
二,Awstats的配置
使用工具软件Awstats对切割好的文件进行分析。
Awstats的配置文件默认会存储在/etc/awstats/目录下,包括安装时设置的域名如:awstats.www.jb200.com.conf。
在配置文件中修改:
LogFile="/nginx/logs/access_%YYYY-24%MM-24%DD-24.log"
读取nginx昨天的日志文件。
关于后边%YYYY-24%MM-24%DD-24的设置,规则如下:
复制代码 代码示例:
# 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 4 digits year we were n hours ago
# %YY-n is replaced with 2 digits year we were n hours ago
# %MM-n is replaced with 2 digits month we were n hours ago
# %MO-n is replaced with 3 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 00:00 since 1970
# %WM-n is replaced with the week number in month (1-5)
# %Wm-n is replaced with the week number in month (0-4)
# %WY-n is replaced with the week number in year (01-52)
# %Wy-n is replaced with the week number in year (00-51)
# %DW-n is replaced with the day number in week (1-7, 1=sunday)
# use n=24 if you need (1-7, 1=monday)
# %Dw-n is replaced with the day number in week (0-6, 0=sunday)
# use n=24 if you need (0-6, 0=monday)
# Use 0 for n if you need current year, month, day, hour
三,开始分析、生成结果
使用命令:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.xxxxke.com
把结果生成到/var/lib/awstats 目录下 awstatsXXXX.www.XXXX.com.txt文件。
可以再用下面的命令来生成html页面,更加直观。
复制代码 代码示例:
perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -update
-config=www.xxxxoke.com -lang=cn
-dir=/html/awstats
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
这样就会在/html/awstats目录下生成很直观的分析结果页。
四,自动化
分享几个crontab配置的自动化命令:
复制代码 代码示例:
1 0 * * * sh /home/zyf/sh/cut_nginx_log.sh
0 1 * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.xxxxke.com
0 2 * * * perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.xxxxke.com -lang=cn -dir=/html/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
日志格式:
复制代码 代码示例:
LogFile="/usr/local/nginx/logs/%YYYY-24/%MM-24/expotia2.access_%YYYY-24%MM-24%DD-24.log"
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
您可能感兴趣的文章:
nginx 日志分析的实例学习
Nginx日志模块分析详解
Nginx关闭日志的方法
nginx日志配置、Nginx日志分割
有关nginx日志格式的设计分享
nginx日志统计访问时间的例子
nginx日志配置文件的格式说明
一个分割ngnix网站日志的Shell脚本
nginx日志报大量400错误的解决方法
nginx关闭favicon.ico日志记录的方法
nginx与apache日志格式的区别
nginx日志中记录cookie的实现方法
修改nginx访问日志的时间格式的方法
Nginx日志中过滤关键字的写法
nginx 日志分析实例