核心内容摘要
谁能想到,一场“黑土狂吃鸣人钢筋”的误会,竟是拯救世界的序曲?
nginx日志如果不切分会导致access.log一直很大如果开启了debug 输出request_body日志会刷的特别快很可能很快达到几十个G配置低的服务器可能磁盘就100%了接口504 game over了所以对于nginx的access和error日志都需要做每天的日志切分对于历史日志做定期清理。
1在/etc/logrotate.d目录下增加nginx配置文件cd /ec/logrotate.dvim nginx添加如下内容#具体nginx日志路径依项目而定 /usr/local/nginx/nginx-
1.
1
1/logs/*.log { daily # 轮转频率每天 missingok # 如果日志不存在不报错 rotate 14 # 保留14个旧日志文件 compress # 压缩旧日志gzip delaycompress # 延迟压缩下一个周期压缩 notifempty # 空日志不轮转 create 646 nginx adm # 创建新日志的权限和所有者 sharedscripts # 所有日志处理完再执行脚本 postrotate nginx -s reload #重启nginx endscript }/etc/logrotate.d/是Linux 系统中管理日志轮转的核心目录。
位置/etc/logrotate.d/是 logrotate 服务的配置目录主配置/etc/logrotate.conf是全局配置文件子配置/etc/logrotate.d/包含各个应用程序的独立配置文件用途自动管理日志文件防止日志过大占满磁盘
轮转频率bashdaily # 每天轮转 weekly # 每周轮转 monthly # 每月轮转 size 100M # 大小达到100M时轮转
保留策略bashrotate 7 # 保留7个旧日志文件 maxage 30 # 删除超过30天的日志 maxsize 100M # 单个日志最大100M
压缩设置bashcompress # 使用gzip压缩 nocompress # 不压缩 compresscmd /bin/bzip2 # 指定压缩工具 compressext .bz2 # 指定扩展名 delaycompress # 延迟一个周期压缩
文件处理bashcreate 0640 user group # 创建新日志的权限 copytruncate # 复制后清空不需要重启服务 nocreate # 不创建新日志 dateext # 使用日期作为后缀 dateformat .%Y%m%d # 日期格式
脚本钩子bashprerotate # 轮转前执行的命令 endscript postrotate # 轮转后执行的命令如重启服务 endscript firstaction # 第一次轮转时执行 endscript2添加定期清理日志的crontab定时任务crontab -e 编辑定时任务添加如下命令0 1 * * * /usr/bin/find /usr/local/nginx/nginx-
1.
1