nginx日志路径
篇一:nginx日志研究
日志切割
1概论
服务器程序运行时会产生各种日志,如错误日志,登陆日志等。 这些日志作用很大,errlog可以分析服务器运行状况,出现的bug等。Accesslog可以做数据挖掘,用户行为分析。
这些数据随着时间的增长会变得很大,根据需要可以做相应的切割。 2原理
服务器的代表:APACHE、 NGINX。就拿它们为例。
在httpd.conf 和nginx.conf中可以配置使它们在运行的时候自动产生日志。
当服务器运行一段时间后,日志会变得很大,不容易查找和分析。我们可以每天备份一份,或者按大小备份。
日志切割的原理其实是将:当前的日志更名保存,然后服务器重载配置文件,重新生成日志文件。
3实例
拿NGINX为例。
3.1首先
将日志名按照格式更改,可以按照时间,也可以按照大小。这里有两
种方式对日志进行更改
第一种:linux指令
Mv access_log access_log_2013/05/20
第二种方式:用logrotate进行更改。logrotate是对日志轮询的工具,轮询则意味着备份,可以按照名称,亦可以按照大小等等。其生效是依靠配置文件来的。在/etc/logrotate.conf ,这是主配置文件,记录了大概的配置,更细的配置则在/etc/logrotate.d/目录下,需要新建一个相应的配置文件,这里我们建立名为nginx的文件。在logrotate.conf里有 include /logrotate.d/nginx 将自己写的配置文件包括进去。 Logrotate 还可以将日志进行压缩,记录转存的计数。更详细的配置在http://5iqiong.blog.51cto.com/2999926/790908
对文件进行更名的配置是:dateext; logrotate 会根据这个命令将文件按照时间进行更名。然后再发送 消息让nginx重载配置文件,重新生成日志。
重要的一点:logrotate是一个程序,不是一项服务,他可以将文件更名,但不能每天都这样做,因为它不是服务,会持续性的运行着。如果每天切割日志,则需要定时任务的配合。后面后讲到cron的应用。
3.2然后
发信号给nginx主进程。这里是:kill ?USR1 `cat /usr/local/nginx/logs/nginx.pid`
Kill 并不是杀死进程,是发信号的意思,根据后面的参数不同产生不同的效果。 USR1是用户自定义的信号,一般没什么特定的作用,当用户发送USR1信号给apache或者nginx主进程的时候,主进程此时的响应会是先停止处理用户请求,将数据写入硬盘,然后重载配置文件,继续写入日志。注意,此时的继续写入日志是如果原来的access_log存在,则继续写入,如果不存在,则生成名为access_log的文件。如果日志切割的话,则意味着在此之前就得对日志进行更名。 到此,日志切割的步骤基本上完成了。如果需要每天切割一次,则需要些定时任务。
3.3定时任务
Linux系统的定时任务分为两块,一个是系统任务,一个是用户任务。
系统任务:系统每天,每星期,每个月都会定时执行的任务。需要配置文件才能生效。配置文件在 /etc/crontab ,修改之,就能生效。 01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
系统会根据这四行,每小时,每天,每星期,每月执行里面的任务。 /etc/cron.daily等里面是 要执行的任务指令。比如
/usr/sbin/logrotate -f /etc/logrotate.d/nginx
或者
/bin/bash /usr/local/nginx/nginx.sh
Nginx.sh 是自己写的将文件更名的脚本指令:
Mv /usr/local/nginx/logs/access.log access_log_${date ?d +”%Y”+”%M”}
用户任务则相对简单些,直接用命令:crontab ?e
打开用户计划任务,在里面写入你要执行的任务就行了。 Logrotate ?f /etc/logrotate.d/nginx
或者
Usr/local/nginx/nginx.sh
为了每天执行,则需要在前面写上时间,于是,整个的指令如下: 00 00 * * * /usr/local/nginx/nginx.sh
*表示每
- 代表从哪到哪 1-3 从一到3
/ 表示每隔
一共五位,依次表示 分钟/ 小时/ 天/ 月/ 星期
具体的介绍见http://blog.csdn.net/tianlesoftware/article/details/5315039
篇二:nginx日志分析解决方案
nginx日志分析解决方案- Awstats
很多PHP搭建的网站都在由apache转向了nginx。nginx的日志信息如何分析呢?当然你可以自己写一个,但是这里也推荐一款结果信息非常详尽的开源工具——Awstats,它基于perl编写,它的介绍如下:
AWStats is a free powerful and featureful tool that generates advanced web, streaming, ftp or mail server statistics, graphically. This log analyzer works as a CGI or from command line and shows you all possible information your log contains, in few graphical web pages. It uses a partial information file to be able to process large log files, often and quickly. It can analyze log files from all major server tools like Apache log files (NCSA combined/XLF/ELF log format or
common/CLF log format), WebStar, IIS (W3C log format) and a lot of other web, proxy, wap, streaming servers, mail servers and some ftp servers.
点击这里去官方网站
第一步,日志的处理。
最好每天分割一下日志,一开始没注意这个工作,结果不久日志文件就上G了,很痛苦。分割日志很简单,首先把日志文件复制到别的地方,然后再通知nginx重新生成日志就可以了。shell脚本如下:
#!/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..conf。在这个配置文件中修改这个地方:
LogFile="/nginx/logs/access_%YYYY-0%MM-0%DD-24.log"
这个意思是要去读取nginx昨天的日志文件,关于后边%YYYY-0%MM-0%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=
这个命令会把结果生成到/var/lib/awstats目录下 awstatsXXXX..txt文件。 当然啦,这样看起来不太方便哦,呵呵,可以再用下面的命令来生成html页面,相当漂亮:
perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -update \
-config= -lang=cn \
-dir=/html/awstats \
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
这样就会在/html/awstats目录下生成很漂漂的分析结果页,很暴力很强大。
第四步,自动化。
要是每天都去服务器上运行几条命令肯定是件令人烦燥的事情,所以呢,linux的世界里有crontab这样的好东东,很简单,下面是我的crontab
1 0 * * * sh /home/zyf/sh/cut_nginx_log.sh
0 1 * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=
0 2 * * * perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config= -lang=cn -dir=/html/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
-------------------------------------------------------------------------
大功告成,打完收功……
篇三:nginx日志
nginx日志
日志对于统计排错来说非常有利的。本文总结了nginx日志相关的配置如access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。
nginx有一个非常灵活的日志记录模式。每个级别的配置可以有各自独立的访问日志。日志格式通过log_format命令来定义。ngx__log_module是用来定义请求日志格式的。
1. access_log指令
语法: access_log path [format [buffer=size [flush=time]]];
access_log path format gzip[=level] [buffer=size] [flush=time];
access_log syslog:server=address[,parameter=value] [format];
access_log off;
默认值: access_log logs/access.log combined;
配置段: , server, location, if in location, limit_except
gzip压缩等级。
buffer设置内存缓存区大小。
flush保存在缓存区中的最长时间。
不记录日志:access_log off;
使用默认combined格式记录日志:access_log logs/access.log 或 access_log logs/access.log combined;
2. log_format指令
语法: log_format name string ?;
默认值: log_format combined “?”;
配置段:
name表示格式名称,string表示等义的格式。log_format有一个默认的无需设置的combined日志格式,相当于apache的combined日志格式,如下所示:
IP地址了。 $remote_addr获取反向代理的IP地址。反向代理服务器在转发请求的头信息中,可以增加X-Forwarded-For信息,用来记录 客户端IP地址和客户端请求的服务器地址。PS: 获取用户真实IP 参见://.ttlsa.com/html/2235.html如下所示:
日志格式允许包含的变量注释如下:
[warning]发送给客户端的响应头拥有“sent__”前缀。 比如$sent__content_range。[/warning]
实例如下:
语法: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
默认值: open_log_file_cache off;
配置段: , server, location
对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off),格式如下: 参数注释如下:
max:
设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
inactive:设置存活时间,默认是10s
min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次
valid:设置检查频率,默认60s
off:禁用缓存
实例如下:
4. log_not_found指令
语法: log_not_found on | off;
默认值: log_not_found on;
配置段: , server, location
是否在error_log中记录不存在的错误。默认是。
5. log_subrequest指令
语法: log_subrequest on | off;
默认值: log_subrequest off;
配置段: , server, location
是否在access_log中记录子请求的访问日志。默认不记录。
6. rewrite_log指令
由ngx__rewrite_module模块提供的。用来记录重写日志的。对于调试重写规则建议开启。 Nginx重写规则指南
语法: rewrite_log on | off;
默认值: rewrite_log off;
配置段: , server, location, if
启用时将在error log中记录notice级别的重写日志。
7. error_log指令
语法: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert
| emerg];默认值: error_log logs/error.log error; 配置段: main, , server, location 配置错误日志。
篇四:Nginx自动日志切割
nginx自动日志功能与日志自动切割脚本
关于nginx包含变量的自动日志:
使用自动日志需要注意以下:
1、nginx日志目录权限,以我的安装路径为例/usr/local/nginx,那我的日志目录就是在/usr/local/nginx/logs里;使用了包含变量
自动日志,那么这个目录nginx的用户需要有写入的权限,否则将不可能生成日志。默认情况下,nginx的运行用户为nobady。 我的
习惯是安装一个程序然后给他分配一个专用账户使用。
2、缓存将不会被使用,因为使用了变量的原因吧(这一点我还没有完全理解)
3、每条日志,要先打开文件,然后写入,最后关闭文件,如此循环。此时,需要开启nginx的open_log_file_cache功能,缓存经常
被使用的日志文件描述符缓存。open_log_file_cache 默认为off
open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
max 最大的文件描述符数量,最大的缓存数量
inactive 在多少时间内不活动,就会被删除
min_uses 如果在inactive时间内,使用超过该参数定义的次数,就会被缓存 valid 多少时间检查一次。
http {
…………
log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/$server_name.access.log main;
open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
…………省去了其他行
}
最后,日志文件名会以在
svr3.labexam.com.access.log
4、关于日志切割:
完成以上三项后,并不是工作的结束,只是又一个开始。日志会随着访问量的增大急速增长,所以需要做日志的切割!
以下是一个脚本:
#!/bin/bash
#made by cheng
#blog:myhat.blog.51cto.com
#!/bin/bash
#made by cheng
#blog:myhat.blog.51cto.com
#last_time=2011/08/16
#your logs store
log_path=/data
nginx_pid=/usr/local/nginx/logs/nginx.pid
nginx_log=/usr/local/nginx/logs
yesterday=`date -d "yesterday" +%Y%m%d`
host=`ls -l $nginx_log/*.access.log | awk -F " " '{print $9}' | awk -F ".access" '{print $1}' | awk -F "$nginx_log/" '{print $2}'`
for i in $host
do
if [ ! -d $log_path/$i/$yesterday ];then server_name中定义的名字开始。例:
mkdir -p $log_path/$i/$yesterday
else
if [ ! -f $log_path/$i/$yesterday/$yesterday.access.log ];then
mv $nginx_log/$i.access.log \
$log_path/$i/$yesterday/$yesterday.access.log
kill -USR1 `cat $nginx_pid`
else
mv -bf $nginx_log/$i.access.log \
$log_path/$i/$yesterday/$yesterday.access.log_1 kill -USR1 `cat $nginx_pid`
fi
fi
done
最后需要做的是,把这个脚本放到crontab里,让其00:00时运行!
运行该脚本后,会自动根据虚拟主机的主机名建立主目录,在该目录下层放新建前一天的日期目录,然后把昨天的日志放到这个目录里。实现每天一次日志切割。
*******该脚本运行的前提是,使用了自动日志的功能。