使用logrotate切割nginx日志

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:

配置:
1、在/etc/logrotate.d目录下创建一个nginx的配置文件"nginx"配置内容如下

#vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
daily
rotate 5
missingok
notifempty
sharedscripts
postrotate
    if [ -f /usr/local/nginx/logs/nginx.pid ]; then
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
    fi
endscript
}


保存退出。

2、执行logrotate

#/usr/sbin/logrotate -f /etc/logrotate.d/nginx


在/usr/local/nginx/logs目录中会产生
error.log
error.log.1
说明logrotate配置成功。

3、让logrotate每天进行一次滚动,在crontab中添加一行定时脚本。

#crontab -e
59 23 * * *  /usr/sbin/logrotate -f /etc/logrotate.d/nginx


每天23点59分进行日志滚动

4、配置文件说明
daily:日志文件每天进行滚动
rotate:保留最5次滚动的日志
notifempty:日志文件为空不进行滚动
sharedscripts:运行postrotate脚本
下面是一个脚本

postrotate
    if [ -f /usr/local/nginx/logs/nginx.pid ]; then
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
    fi
endscript


脚本让nginx重新生成日志文件。


参考博文:http://linux008.blog.51cto.com/2837805/555829/


     本文转自1321385590 51CTO博客,原文链接:http://blog.51cto.com/linux10000/1925954,如需转载请自行联系原作者





相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
3天前
|
应用服务中间件 nginx
nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
【8月更文挑战第27天】nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
16 6
|
8天前
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
|
13天前
|
存储 监控 应用服务中间件
查看nginx日志文件
器性能和提高网站可用性。掌握日志文件的路径、查看方法和基本分析技能对于任何服务器管理员来说都是必备技能。
32 1
|
16天前
|
存储 Ubuntu 应用服务中间件
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
13 4
|
19天前
|
监控 关系型数据库 Linux
Linux日志管理工具:Logrotate(二)
Linux日志管理工具:Logrotate(二)
18 2
|
17天前
|
应用服务中间件 Linux nginx
Nginx log 日志文件较大,按日期生成 实现日志的切割
Nginx log 日志文件较大,按日期生成 实现日志的切割
79 0
|
19天前
|
存储 监控 安全
Linux日志管理工具:Logrotate(一)
Linux日志管理工具:Logrotate(一)
56 0
|
23天前
|
应用服务中间件 nginx
[nginx]日志中记录自定义请求头
[nginx]日志中记录自定义请求头
|
24天前
|
应用服务中间件 Shell nginx
shell分析nginx日志的一些指令
shell分析nginx日志的一些指令
|
Linux 应用服务中间件 nginx
下一篇
云函数