nginx日志切割

摘要:
下面来说说nginx日志切割。关于nginx相关日志配置参见:《nginx日志配置》一文。如需转载请注明出处:http://www.ttlsa.com/html/3166.html在linux环境下,我希望能够每天到0点切换nginx日志,前一天的日志使用日期后缀,并且压缩。-f/somedir2/nginx.pid]||kill-USR1`cat/somedir2/nginx.pid`endscript}其中somedir1需要替换成存放nginx日志的目录,somedir2需要替换成系统存放nginx.pid文件的目录。

http://www.ttlsa.com/nginx/nginx-log-cutting/

nginx日志默认情况下统统写入到一个文件中,文件会变的越来越大,非常不方便查看分析。以日期来作为日志的切割是比较好的,通常我们是以每日来做统计的。下面来说说nginx日志切割。
关于nginx相关日志配置参见:《nginx日志配置》一文。logrotate用法参见《logrotate日志管理工具》。

1. 定义日志轮滚策略

# vim nginx-log-rotate

/data/weblogs/*.log {
nocompress
daily
copytruncate
create
notifempty
rotate 7
olddir /data/weblogs/old_log
missingok
dateext
postrotate
/bin/kill-HUP `cat/var/run/nginx.pid 2> /dev/null` 2> /dev/null ||true
endscript
}
/data/weblogs/*.log使用通配符时,/data/weblogs/目录下的所有匹配到的日志文件都将切割。如果要切割特定日志文件,就指定到该文件。

2. 设置计划任务

# vim /etc/crontab

59 23 * * * root ( /usr/sbin/logrotate -f /PATH/TO/nginx-log-rotate)

这样每天23点59分钟执行日志切割。

如需转载请注明出处:http://www.ttlsa.com/html/3166.html

在linux环境下,我希望能够每天到0点切换nginx日志,前一天的日志使用日期后缀,并且压缩。

步骤:
1.
需要制作一个nginxlogrotate文件,放在目录/etc/logrotate.d/下,
文件内容如下:
/somedir1/*.log {
daily
missingok
rotate 65535
compress
dateext
notifempty
sharedscripts
postrotate
[ ! -f /somedir2/nginx.pid ] || kill -USR1 `cat /somedir2/nginx.pid`
endscript
}
其中somedir1需要替换成存放nginx日志的目录,somedir2需要替换成系统存放nginx.pid文件的目录。
2.
定时启动logrotate
设置定时任务,crontab -e
0 0 * * * nohup /usr/sbin/logrotate /etc/logrotate.conf > /dev/null 2>&1 &
3.隔天查看nginx日志目录,发现生成了如下的文件
access.log-20121008.gz
error.log-20121008.gz

参见:
http://drumcoder.co.uk/blog/2012/feb/03/nginx-and-logrotate/
nginx and Logrotate

February 3, 2012

I wanted to start cycling and compressing my previous nginx log files, as they were getting a little large. NGINX doesn't have built in support for log rotation, there is a system level linux command called logrotate that handles this on behalf of many applications. I'm doing this on a Debian squeeze.

/etc/logrotate.d

In/etc/logrotate.dyou'll find a file for each application that is using logrotate. There should be one in here callednginxthat contains the following:

/var/log/nginx/*.log {
    daily
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
            [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
    endscript
}

This is the default rotate process for nginx files. I store my logs in/home/drumcoder/logs, so we will specify another block in this same file, using many of the same options.

/home/drumcoder/log/*.access.log {
    daily
    missingok
    rotate 52
    mail drumcoder@here.com
    compress
    delaycompress
    notifempty
    sharedscripts
    postrotate
            [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
    endscript
}

The following options are used:

  • daily- rotates the log files daily
  • missingok- it's fine if there is no log to rotate
  • rotate 52- rotate logs 52 times, and then delete the oldest
  • mail drumcoder@here.com- before you delete an old log, send it to this mail address
  • compress- use gzip on old logs
  • delaycompress- leave one rotated log that isn't compressed so the process can still write to it if needed
  • notifempty- don't rotate empty file
  • sharedscripts- when telling nginx that logs have been rotated, only do it once rather than once for each file group

The bottompostrotateblock sends a signal to nginx to tell it that logs have been rotated and it should pick up the file handles for the new ones.

References

免责声明:文章转载自《nginx日志切割》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇session的属性设置mac安装MySQL笔记下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

nginx匹配规则说明以及匹配的优先级

location 匹配规则语法规则    location [=|~|~*|^~] /uri/ { … }模式    含义location = /uri    = 表示精确匹配,只有完全匹配上才能生效location ^~ /uri    ^~ 开头对URL路径进行前缀匹配,并且在正则之前。location ~ pattern    开头表示区分大小写的正...

nginx正向代理 (带着请求头)

当你获得云服务器之后, 你有这样一个需求:当你要访问一个url的时候,这个URL只能在人家的云服务器上访问(比如百度),所以你要买百度的BCC,你可能在想在BCC起服务,那样有点麻烦,直接使用nginx代理就可以解决问题了,因为url涉及到验证,所以要把请求头带上。 首先下载nginx apt-get install nginx 最后配置nginx配置文件...

TiDB配置HAProxy负载均衡

1、简介 HAProxy是一个C语言编写的免费的负载均衡软件,可以运行于大部分主流的Linux操作系统上。 HAProxy提供了L4(TCP)和L7(HTTP)两种负载均衡能力,具备丰富的功能。 2、配置使用 2.1、下载安装 https://www.haproxy.org/download/1.9/src/ 此处使用的是1.9版本 解压安装:tar -...

Linux 安装Nginx+PHP+MySQL教程

一、安装nginx 通过yum安装openssl: yum -y install openssl openssl-devel 通过yum安装pcre: yum -y install pcre-devel 通过yum安装zlib: yum -y install zlib-devel tar zxvf nginx-1.8.0.tar.gz cd...

Python机器学习(1)——决策树分类算法

1、决策树算法 决策树用树形结构对样本的属性进行分类,是最直观的分类算法,而且也可以用于回归。不过对于一些特殊的逻辑分类会有困难。典型的如异或(XOR)逻辑,决策树并不擅长解决此类问题。 决策树的构建不是唯一的,遗憾的是最优决策树的构建属于NP问题。因此如何构建一棵好的决策树是研究的重点。 J. Ross Quinlan在1975提出将信息熵的概念引入决策...

springboot整合log4j2

前言   这里日志分两种。一种是tomcat的输出(系统)日志,一种是自己定义的日志。 环境   springboot版本1.5.6。(springboot1.4以上则使用log4j2,一定要注意版本) 1、springboot默认日志logback的日志输出样式如下: 2、排除springboot的默认日志logback,使用exclusion排除默认...