说明:

Nginx安装目录:/usr/local/nginx/
Nginx日志目录:/var/log/nginx/error/*.log /var/log/nginx/access/w1/*.log /var/log/nginx/access/w2/*.log /var/log/nginx/access/w3/*.log

nginx配置文件:nginx.conf

user  www www;

.......

error_log  /var/log/nginx/error/error.log error;

http {
......

log_format  main  '$server_name $remote_addr - $remote_user [$time_local] "$request"
                              '$status $body_bytes_sent "$http_referer" '
                              ' "$http_user_agent" "$http_x_forwarded_for" "$request_time" ';

access_log  logs/access.log  main;

.......

server {

......

access_log  /var/log/nginx/access/w1/access.log  main;
    }

include /usr/local/nginx/conf/vhost/*.conf;
}

其他站点只需要在server内增加 access_log  /var/log/nginx/access/w2/access.log  main; 即可,
然后在相应的日志路径建文件夹并授权www

1、添加nginx日志切割脚本
cd  /etc/logrotate.d  #进入目录
vi   /etc/logrotate.d/nginx   #编辑脚本

/var/log/nginx/error/*.log /var/log/nginx/access/w1/*.log /var/log/nginx/access/w2/*.log /var/log/nginx/access/w3/*.log{
daily
rotate 15
missingok
notifempty
dateext
compress
delaycompress
create 600 www www
sharedscripts
postrotate
    if [ -f /usr/local/nginx/logs/nginx.pid ]; then
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
    fi
endscript
}
:wq!  #保存退出
chmod 644  /etc/logrotate.d/nginx  #添加执行权限

2、执行脚本
/usr/sbin/logrotate -vf  /etc/logrotate.d/nginx

3、添加定时任务
crontab  -e  #添加以下代码
0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx  #每天凌晨定时执行脚本

至此,Linux下nginx日志每天定时切割教程完成。

备注:logrotate相关参数说明
missingok:忽略错误,如“日志文件无法找到”的错误提示。
dateext:切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期,没有这个配置项会附加一个小数点加一个数字序号
notifempty:如果日志文件为空,不执行切割。
daily:按天切割日志。可用值月:monthly 周:weekly 年:yearly
rotate 7:保留最近7天的日志记录
sharedscripts:只为整个日志组运行一次的脚本
postrotate和endscript:里面指定的命令将被执行。
compress::在轮循任务完成后,已轮循的归档将使用gzip进行压缩。
delaycompress::总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。
create 644 www www: 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件。

使用Linux自带的命令logrotate对Nginx日志进行切割的更多相关文章

  1. 使用logrotate做nginx日志分割

    版权申明:转载请注明出处. 文章来源:http://bigdataer.net/?p=266 背景 nginx是一款非常优秀的网络代理工具,但是其日志管理有点缺憾:nginx的access_log会无 ...

  2. logrotate 进行nginx日志分割

    http://www.williamsang.com/archives/1254.html 日志分割常用方法: 自己写脚本分割 使用linux自带的logrotate 前者灵活,可以应对各种需求,自定 ...

  3. 使用logrotate管理nginx日志文件

    本文转载自:http://linux008.blog.51cto.com/2837805/555829 描述:linux日志文件如果不定期清理,会填满整个磁盘.这样会很危险,因此日志管理是系统管理员日 ...

  4. Linux自带神器logrotate详解

    Linux自带神器logrotate详解 散尽浮华 运维 3天前   作者:散尽浮华 链接:https://www.cnblogs.com/kevingrace/p/6307298.html 对于 L ...

  5. Nginx日志切割之Logrotate篇

    不管是什么日志文件,都是会越来越大的,大到一定程度就是个可怕的事情了,所以要及早的做处理,方法之一就是按时间段来存储,不过linux系统提供了Logrotate的日志管理工具,很好用,不用写计划任务脚 ...

  6. Linux下添加shell脚本使得nginx日志每天定时切割压缩

    Linux下添加shell脚本使得nginx日志每天定时切割压缩一 简介 对于nginx的日志文件,特别是access日志,如果我们不做任何处理的话,最后这个文件将会变得非常庞大 这时,无论是出现异常 ...

  7. 用 Linux自带的logrotate 来管理日志

    大家可能都有管理日志的需要,比如定时压缩日志,或者当日志超过一定大小时就自动分裂成两个文件等.最近就接到这样一个小任务.我们的程序用的是C语言,用log4cpp的library来实现日志记录.但是问题 ...

  8. 利用Linux自带的logrotate管理日志

    日常运维中,经常要对各类日志进行管理,清理,监控,尤其是因为应用bug,在1小时内就能写几十个G日志,导致磁盘爆满,系统挂掉. nohup.out,access.log,catalina.out 本文 ...

  9. 用Linux自带的Logrotate来管理日志

    Logrotate是由cron控制,cron在规定的时间执行 " logrotate  /etc/logrotate.conf "命令.将对象日志进行转储,删除,压缩等操作... ...

随机推荐

  1. Extjs学习笔记——Ext.data.JsonStore使用说明

    Ext.data.JsonStore继承于Ext.data.Store.使得从远程JSON数据创建stores更为方便的简单辅助类. JsonStore合成了Ext.data.HttpProxy与Ex ...

  2. c++中cin的基本使用方法

    一.最主要的使用方法cin>> 接收一个数字.字符.字符串.遇"空格"."TAB"."回车"都结束 比如: <span s ...

  3. css3 动态背景

    动态背景 利用多层背景的交替淡入淡出,实现一种背景在不停变换的效果,先看图. 效果图: DEMO地址 步骤 1.利用css的radial-gradient创建一个镜像渐变的背景.当中的80% 20%为 ...

  4. hdu 1695(莫比乌斯反演)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. C语言实现字符串拼接

    #include <stdio.h>#include <stdlib.h>#include <string.h> char* str_contact(const c ...

  6. 0505 php-数组、控制语句、函数

    数 组 (定义.使用.赋值.遍历.分类.冒泡排序) 1.数组包括元素.下标.数组长度 2.php中的数组长度用$len = count("$数组名"); 3.定义一个数组:$arr ...

  7. Appium - 命令行参数

    1.cmd端口输入,appium -help参考帮助信息 2.Appium - 命令行参数 参数 默认 描述 举个例子 --shell 空值 进入REPL模式   --ipa 空值 (仅限IOS)ab ...

  8. Spring Boot (11) mybatis 关联映射

    一对多 查询category中的某一条数据,同时查询该分类下的所有Product. Category.java public class Category { private Integer id; ...

  9. 拖入浏览器读取文件demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. js基础---数组方法

    数组数据的排序及去重 sort无形参的排序方式 arr1=[2,12,3,15]; var a=arr1.sort();console.log(arr1);console.log(a);//排序会改变 ...