logrotate切割nginx日志
2017-06-27 10:11
381 查看
链接
配置
测试
添加定时任务
CSDN: blog.csdn.net/alex_my
假设服务器上有两个网站的nginx配置分别如下:
去除其它配置信息,只保留了日志相关
A网站
B网站
在/etc/logrotate.d/下创建一个配置文件 nginx, 内容如下:
需要注意的是你们的nginx.pid位置,不一定是在/usr/local/nginx/nginx.pid
配置说明
然后到相应的日志目录下查看 (/data/logs/a.com/, /data/logs/b.com/)
应该会有类似以下的文件:
access.log
access.log-20170626
error.log
error.log-20170626
在终端运行 crontab -e
插入以下语句
配置
测试
添加定时任务
1 链接
个人博客: alex-my.xyzCSDN: blog.csdn.net/alex_my
2 配置
使用系统自带的logrorate来切个nginx日志,位于/usr/sbin/logrotate假设服务器上有两个网站的nginx配置分别如下:
去除其它配置信息,只保留了日志相关
A网站
... access_log /data/logs/a.com/access.log; error_log /data/logs/a.com/error.log; ...
B网站
... access_log /data/logs/b.com/access.log; error_log /data/logs/b.com/error.log; ...
在/etc/logrotate.d/下创建一个配置文件 nginx, 内容如下:
# 这里可以添加你想切个的目录,也可以直接使用正则表达式 /data/logs/a.com/*.log /data/logs/b.com/*.log { daily rotate 30 missingok dateext compress delaycompress notifempty sharedscripts postrotate if [ -f /usr/local/nginx/nginx.pid ]; then kill -USR1 `cat /usr/local/nginx/nginx.pid` fi endscript }
需要注意的是你们的nginx.pid位置,不一定是在/usr/local/nginx/nginx.pid
配置说明
配置 | 说明 |
---|---|
daily | 指定转储周期为每天 |
weekly | 指定转储周期为每周 |
monthly | 指定转储周期为每月 |
rotate | 转储次数,超过将会删除最老的那一个 |
missingok | 忽略错误,如“日志文件无法找到”的错误提示 |
dateext | 切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期 |
compress | 通过gzip 压缩转储旧的日志 |
delaycompress | 当前转储的日志文件到下一次转储时才压缩 |
notifempty | 如果日志文件为空,不执行切割 |
sharedscripts | 只为整个日志组运行一次的脚本 |
prerotate/endscript | 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行 |
postrotate/endscript | 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行 |
3 测试
执行以下命令进行测试logrotate -vf /etc/logrotate.d/nginx
然后到相应的日志目录下查看 (/data/logs/a.com/, /data/logs/b.com/)
应该会有类似以下的文件:
access.log
access.log-20170626
error.log
error.log-20170626
4 添加定时任务
每日0点执行脚本在终端运行 crontab -e
插入以下语句
0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx
相关文章推荐
- logrotate 切割 nginx php mysql 日志
- logrotate日志管家来切割日志(tomcat,nginx,httpd的日志)
- 利用logrotate切割nginx日志
- 使用logrotate做的nginx日志切割
- nginx利用logrotate日志切割
- nginx日志切割工具logrotate若干问题
- 利用Centos6系统自带的logrotate切割nginx日志
- 切割日志(mysql,nginx,php tomcat)使用logrotate
- nginx日志切割
- LNMP架构 (3) 之 Nginx访问日志、日志切割、静态文件不记录日志和过期时间
- nginx日志按日期自动切割脚本
- nginx日志按天切割脚本
- python切割nginx日志脚本
- Nginx访问日志、Nginx日志切割 、静态文件不记录日志和过期时间
- logrotate管理分割nginx日志无效
- nginx 日志切割配置
- nginx 切割日志文件
- Nginx实战(三) 日志配置与切割
- nginx日志切割
- Nginx日志每日切割