博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用logrotate配置Nginx日志轮替
阅读量:7006 次
发布时间:2019-06-27

本文共 2894 字,大约阅读时间需要 9 分钟。

hot3.png

使用logrotate配置Nginx日志轮替

Nginx作为HTTP服务器,每天记录的日志很多,如果不善加管理,没用多久就会把磁盘充满。Apache有rotatelogs程序帮助轮替,而Nginx没有。好在我们的Linux带了logrotate程序帮助我们完成这个任务。

一、实验环境

  1. 操作系统:CentOS 6.6 x64 ( Linux 2.6.32-431.23.3.el6.x86_64 )

  2. logrotate版本:3.7.8-17.el6.x86_64

  3. Nginx版本:nginx/1.6.2

二、logrotate介绍

logrotate是一款专门用来管理日志轮替的程序,各大Linux发行版在安装的时候就已经内置了此程序。

使用方法:

logrotate [OPTION...] 
-d, --debug 测试,但不会真正运行(已包含-v选项) -f, --force 强制对文件轮替 -m, --mail=command 后接参数,参数是发邮件命令(替代`/bin/mail`) -s, --state=statefile 状态文件的路径 -v, --verbose 输出轮替过程中的信息到标准输出

三、操作步骤

  1. 创建/etc/logrotate.d/nginx配置文件,写入配置。这里我们nginx日志是位于/data/logs/nginx_access.log/data/logs/nginx_error.log

    shell$ sudo vim /etc/logrotate.d/nginx /data/logs/nginx_access.log /data/logs/nginx_error.log { missingok #如果日志文件不存在,则不报错,直接忽略 notifempty #如果日志文件为空则不执行轮替操作 daily #频次为每天运行 rotate 30 #保留30天的日志 sharedscripts #日志共享脚本,也就是说等access和error两个日志都rotate之后再执行下面的脚本 postrotate #设置脚本在rotate之后执行,对应的有一个选项是prerotate if [ -f /service/nginx/logs/nginx.pid ]; then #设置rotate之后nginx需重新载入配置文件,否则nginx不会将日志写入新的日志文件中 /service/nginx/sbin/nginx -s reload fi endscript }

  2. 测试是否成功。

    shell$ sudo logrotate -vf /etc/logrotate.conf #logrotate.conf中有include /etc/logrotate.d reading config file /etc/logrotate.conf including /etc/logrotate.d reading config file dracut reading config info for /var/log/dracut.log reading config file nginx reading config info for /data/logs/nginx_access.log /data/logs/nginx_error.log reading config file psacct reading config info for /var/account/pacct reading config file syslog reading config info for /var/log/cron /var/log/maillog /var/log/messages /var/log/secure /var/log/spooler

    reading config file yum reading config info for /var/log/yum.log reading config info for /var/log/wtmp reading config info for /var/log/btmp

    Handling 7 logs ...... rotating pattern: /data/logs/nginx_access.log /data/logs/nginx_error.log forced from command line (30 rotations) empty log files are not rotated, old logs are removed considering log /data/logs/nginx_access.log log needs rotating considering log /data/logs/nginx_error.log log needs rotating rotating log /data/logs/nginx_access.log, log->rotateCount is 30 dateext suffix '-20150827' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' glob finding old rotated logs failed rotating log /data/logs/nginx_error.log, log->rotateCount is 30 dateext suffix '-20150827' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' glob finding old rotated logs failed renaming /data/logs/nginx_access.log to /data/logs/nginx_access.log-20150827 creating new /data/logs/nginx_access.log mode = 0644 uid = 0 gid = 0 renaming /data/logs/nginx_error.log to /data/logs/nginx_error.log-20150827 creating new /data/logs/nginx_error.log mode = 0644 uid = 0 gid = 0 running postrotate script ......

从以上输出中我们可以看出,两个日志都得到了rotate。

转载于:https://my.oschina.net/plutonji/blog/498319

你可能感兴趣的文章
淘宝前端技术巡礼
查看>>
问题-Delphi不能进行调试模式
查看>>
$.extend()的实现源码 --(源码学习1)
查看>>
Docker 监控的一点想法
查看>>
从U-Boot显示Logo到Android
查看>>
基于 HTML5 Canvas 的简易 2D 3D 编辑器
查看>>
WINPE下如何直接删除联想隐藏分区?
查看>>
通过NTP协议进行时间同步
查看>>
测试的境界
查看>>
flash中NetConnection与NetStream知识整理
查看>>
SQL 问题:已知主键id 和排序条件 这条记录的上一条或下一条记录
查看>>
【转】NSNotificationCenter用法总结
查看>>
jquery 添加节点的几种方法
查看>>
Android WebView 总结
查看>>
创建存储过程和函数
查看>>
[zz]Linux下虚拟化技术深入探讨
查看>>
ASP.NET Cache的一些总结
查看>>
类型名称了解typename的双重意义
查看>>
深入理解计算机系统(1.2)---hello world的程序是如何运行的
查看>>
Json与数组
查看>>