logrotate切割nginx日志
2024-09-01 00:07:31
1 配置
- 使用系统自带的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
}
- 配置说明:
daily | 指定转储周期为每天 |
weekly | 指定转储周期为每周 |
monthly | 指定转储周期为每月 |
rotate | 转储次数,超过将会删除最老的那一个 |
missingok | 忽略错误,如“日志文件无法找到”的错误提示 |
dateext | 切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期 |
compress | 通过gzip 压缩转储旧的日志 |
delaycompress | 当前转储的日志文件到下一次转储时才压缩 |
notifempty | 如果日志文件为空,不执行切割 |
sharedscripts | 只为整个日志组运行一次的脚本 |
prerotate/endscript | 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行 |
postrotate/endscript | 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行 |
2、 测试
- 执行以下命令进行测试
logrotate -vf /etc/logrotate.d/nginx
- 然后到相应的日志目录下查看 (/data/logs/a.com/, /data/logs/b.com/)
- 应该会有类似以下的文件:
- access.log
- access.log-20170626
- error.log
- error.log-20170626
3、 添加定时任务
每日0点执行脚本
- 在终端运行 crontab -e
- 插入以下语句
* * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx
最新文章
- iOS-----程序异常处理----- 断言NSAssert()和NSParameterAssert区别和用处
- iOS-UI-UI控件概述
- Vs2013中通过Nuget添加不同版本jQuery
- FTPS链接服务器
- (gridcontrol等)通用导出excel z
- 获取byte数组的实际使用长度
- 25 个超棒的 HTML5 &; JavaScript 游戏引擎开发库
- 浅谈JavaScript中的apply,call和bind
- thinkphp实现自动登录
- Unable to chmod /system/build.prop.: Read-only file system
- Go语言打造以太坊智能合约测试框架(level3)
- C# IQueryable和IEnumerable的区别
- Django框架详细介绍---请求流程
- nDPI-dev分析
- Confluence 6 PostgreSQL 测试你的数据库连接
- java.lang.UnsatisfiedLinkError:no dll in java.library.path
- lnmp+zabbix 3.2 的编译安装
- webdriver+expected_conditions二次封装
- BZOJ 1022 [SHOI2008]小约翰的游戏John AntiNim游戏
- 本地blast的安装