扫码关注官方订阅号
Nginx的log都是写在一个文件里的,我需要每天一个Nginx Log 网上看到很多办法,都是写脚本,定式切割Log文件 Nginx自己不能做日志切割这件事吗?
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
确认cron在运行
service crond status
修改配置文件
vi /etc/crontab
确认定时任务
vi /etc/cron.daily/logrotate
编写logrotate配置文件 vi /etc/logrotate.d/nginx
/var/log/nginx/*.log { #指定转储周期为每天 daily missingok #指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份 rotate 5 #compress #delaycompress #如果是空文件的话,不转储 notifempty #create 640 root adm sharedscripts postrotate [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid` endscript }
测试配置
/usr/sbin/logrotate -f /etc/logrotate.d/nginx
系统管理员的日志需求千变万化,Nginx为了自己的轻量,确实就不操心日志管理的事了。
官方的LogRotation Wiki页面
估计你也查到了,可以用自己的脚本或者logrotate。
最简单的办法就是直接在配置文件中进行配置:
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") { set $year $1; set $month $2; set $day $3; } access_log /home/wwwlogs/$year-$month-$day-bbs-access.log access;
这个方法经常会生成不带日期的文件,已经弃用
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
确认cron在运行
修改配置文件
确认定时任务
编写logrotate配置文件 vi /etc/logrotate.d/nginx
/var/log/nginx/*.log { #指定转储周期为每天 daily missingok #指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份 rotate 5 #compress #delaycompress #如果是空文件的话,不转储 notifempty #create 640 root adm sharedscripts postrotate [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid` endscript }测试配置
系统管理员的日志需求千变万化,Nginx为了自己的轻量,确实就不操心日志管理的事了。
官方的LogRotation Wiki页面
估计你也查到了,可以用自己的脚本或者logrotate。
最简单的办法就是直接在配置文件中进行配置:
这个方法经常会生成不带日期的文件,已经弃用