码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx日志切割

时间:2017-06-15 00:35:22      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:nginx日志切割

Nginx日志切割

Nginx日志切割脚本:

[root@LHQ vhosts]# vim /usr/local/sbin/nginx_logrotate.sh

#!/bin/bash

d=`data -d "-1 day" +%F`  (d表示时间,作为日志切割完成之后的一个前缀)

[ -d /tmp/nginx_log ] || make /tmp/nginx_log (判断一个目录是否存在,两个竖线表示,前面命令如果成功后面的命令不去执行,如果前面白骨成功,后面继续执行)

mv /tmp/access.log /tmp/nginx_log/$d.log(移动重命名)

/etc/init.d/nginx reload > /dev/null(重新加载配置文件,产生的输出,重定向)

cd /tmp/nginx_log/(如果日志比较大,进入到日志下压缩)

gzip -f $d.log (-f:如果有同名文件,不去提醒,强制覆盖)

保存,退出

执行脚本:

[root@LHQ vhosts]# sh -x /usr/local/sbin/nginx_logrotate.sh (-x显示执行过程)

查看日期:

[root@LHQ vhosts]# date

扩展:

编写脚本:

vim  /usr/local/sbin/logrotate.sh  //加入

#! /bin/bash

datedir=`date +%Y%m%d`

/bin/mkdir  /home/logs/$datedir >/dev/null 2>&1

/bin/mv /home/logs/*.log /home/logs/$datedir

/bin/kill -HUP `cat /var/run/nginx.pid`

日志格式 accesss的日志

log_format main ‘$remote_addr - $remote_user [$time_local] $request ‘

                   ‘"$status" $body_bytes_sent "$http_referer" ‘

                   ‘"$http_user_agent" "$http_x_forwarded_for"‘;

log_format main1 ‘$proxy_add_x_forwarded_for - $remote_user [$time_local] ‘

                     ‘"$request" $status $body_bytes_sent ‘

                     ‘"$http_referer" "$http_user_agent"‘;  //此日志格式为,ip不仅记录代理的ip还记录远程客户端真实IP。

1、日志格式说明解释

1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;

2.$remote_user :用来记录客户端用户名称;

3.$time_local : 用来记录访问时间与时区;

4.$request : 用来记录请求的url与http协议;

5.$status : 用来记录请求状态;成功是200,

6.$bytes_sent :记录发送客户端的总字节数;

7.$http_referer :用来记录从那个页面链接访问过来的;

8.$http_user_agent :记录客户端浏览器的相关信息;

$connection 连接的序列号。

$connection_requests 当前通过一个连接获得的请求数量。

$msec 日志写入时间。单位为秒,精度是毫秒。

$pipe 如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。

$request_length 请求的长度(包括请求行,请求头和请求正文)。

技术分享

技术分享

下面介绍下2者的差别:

1、request_time

官网描述:request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client 。

指的就是从接受用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出

响应数据时间。

2、upstream_response_time

官网描述:keeps times of responses obtained from upstream servers; times are kept in seconds with a milliseconds resolution. Several response times are separated by commas and colons like addresses in the $upstream_addr variable

是指从Nginx向后端(php-cgi)建立连接开始到接受完数据然后关闭连接为止的时间。

从上面的描述可以看出,$request_time肯定比$upstream_response_time值大,特别是使用POST方式传递参数时,因为Nginx会把request body缓存住,接受完毕后才会把数据一起发给后端。所以如果用户网络较差,或者传递数据较大时,$request_time会比$upstream_response_time大很多。

所以如果使用nginx的accesslog查看php程序中哪些接口比较慢的话,记得在log_format中加入$upstream_response_time。

6. 静态文件不记录日志,配置缓存

       location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

       {

               expires      30d;

               access_log off;

       }

       location ~ .*\.(js|css)$

       {

               expires      12h;

               access_log off;

       }


本文出自 “12350027” 博客,谢绝转载!

Nginx日志切割

标签:nginx日志切割

原文地址:http://12360027.blog.51cto.com/12350027/1936777

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!