Logrotate配置nginx日志切割

一、Logrotate介绍

logrotateLinux系统下的一个日志管理工具,用于简化系统日志文件的管理。它可以自动对日志文件进行轮转、压缩、删除和邮件通知等操作,根据时间或文件大小触发处理,防止日志文件过大。通过配置文件设置规则,通常由cron定时任务触发执行。

二、配置详解

1.主配置文件

bash 复制代码
[root@test etc]# vim /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d   # 自定义的日志轮转配置都放在这里,本次以nginx为例


# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

2.创建nginx轮转配置

bash 复制代码
[root@test etc]# cd /etc/logrotate.d/
[root@test logrotate.d]# vim nginx
/opt/web_app/nginx-1.16.1/logs/*.log {
    daily
    missingok
#    size 100M
    rotate 14
    dateyesterday
    dateext
    dateformat -%Y-%m-%d-%s
    compress
    delaycompress
    notifempty
    create 640 nginx nginx
    sharedscripts
    postrotate
        if [ -f /opt/web_app/nginx-1.16.1/logs/nginx.pid ]; then
            kill -USR1 `cat /opt/web_app/nginx-1.16.1/logs/nginx.pid`
        fi
    endscript
}

3.配置详解

/opt/web_app/nginx-1.16.1/logs/*.log # 想切割的日志绝对路径

daily # 切割时间频率,可选值:daily,weekly,monthly,yearly

missingok # 日志不存在,不报错

size 100M # 当日志到达此大小后切割,一般情况下,与时间频率冲突

rotate 14 # 保留过去14天的日志

dateyesterday # 切割完后保存时间是前一天,这个配置在nginx的轮转配置中相当重要

dateext # 以时间为后缀命名

dateformat -%Y-%m-%d-%s # 定义切割日志的后缀,该参数要配合dateext使用,单独配置不生效

compress # 启用gzip压缩

delaycompress # 下次轮转时压缩文件

notifempty # 空日志文件不切割

create 640 nginx nginx # 创建新文件时的权限、所有者和所属组

sharedscripts # 确保在匹配到多个日志文件时,postrotate脚本只执行一次,这个配置在nginx日志轮转中非常重要,可避免nginx多次重新加载

postrotate # 重新打开日志的脚本

if -f /opt/web_app/nginx-1.16.1/logs/nginx.pid ; then

kill -USR1 `cat /opt/web_app/nginx-1.16.1/logs/nginx.pid`

fi

endscript

4.检查配置文件

bash 复制代码
[root@test logrotate.d]# logrotate --debug /etc/logrotate.d/nginx
reading config file /etc/logrotate.d/nginx
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /opt/web_app/nginx-1.16.1/logs/*.log  after 1 days (14 rotations)
empty log files are not rotated, old logs are removed
considering log /opt/web_app/nginx-1.16.1/logs/access.log
  log does not need rotating (log has been rotated at 2026-5-27 11:15, that is not day ago yet)
considering log /opt/web_app/nginx-1.16.1/logs/error.log
  log does not need rotating (log has been rotated at 2026-5-27 10:0, that is not day ago yet)
not running postrotate script, since no logs were rotated

上面的检查是没有问题的,现在我们修改一个值,看看错误的是什么样的!

bash 复制代码
[root@test logrotate.d]# vim nginx
#    create 640 nginx nginx
    create 840 nginx nginx
# 把640权限改成840,再debug

[root@test logrotate.d]# logrotate --debug /etc/logrotate.d/nginx
reading config file /etc/logrotate.d/nginx
error: /etc/logrotate.d/nginx:12 extra arguments for create
error: found error in /opt/web_app/nginx-1.16.1/logs/*.log , skipping
removing last 1 log configs
Allocating hash table for state file, size 15360 B

Handling 0 logs

# debug提示 文件第12行有错误

三、定时执行

第二部分介绍了logrotate的配置,有了配置之后,就可以执行了,但如何执行呢?

我们可以直接在crontab里面配置精确的执行时间和命令

1.配置crontab

bash 复制代码
[root@test etc]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

0 0 * * * root logrotate -f /etc/logrotate.d/nginx

直接写入/etc/crontab配置文件,nginx轮转配置已经放在logrotate下了,而且logrotate会自动周期性执行,那么为什么一定要在crontab里面配置?

2.浅谈anacrontab 和 crontab

anacrontab是周期性补偿调度,适合可能关机或不常运行的设备

crontab 是精确时间点调度器,适合服务器常年开机的场景

简单来说:anacrontab是crontab的补偿方案,anacrontab并不会很准时的执行,如果想在每天0点0分执行切割,那么建议还是选择crontab。

具体区别如下:

特性 crontab anacrontab
全称 Cron Table Anacron Table
设计初衷 确切的时间点执行任务 确保任务在指定周期内至少执行一次
适用场景 服务器、24/7 运行的系统 笔记本电脑、偶尔关机的桌面系统
时间精度 精确到分钟(如每天 02:00) 粗略周期(如每 1 天、每 7 天)
是否依赖系统持续运行 是(若关机则错过任务) 否(开机后检查并补执行)
配置文件位置 /var/spool/cron//etc/crontab /etc/anacrontab
典型用途 备份、日志轮转、监控脚本 系统维护、更新检查、清理临时文件

※所以需要配置准确时间执行任务时,还是选择crontab配置比较稳妥

相关推荐
ping某13 小时前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
大树882 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠2 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质2 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工3 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智3 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_3 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉3 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
AC赳赳老秦3 天前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw
java_cj3 天前
深入kube-apiserver认证机制:从Bearer Token到mTLS的完整认证链解析
linux·运维·服务器·云原生·容器·kubernetes