使用logrotate管理nginx日志文件
2016-04-11 09:50
666 查看
本文转载自:http://linux008.blog.51cto.com/2837805/555829
描述:linux日志文件如果不定期清理,会填满整个磁盘。这样会很危险,因此日志管理是系统管理员日常工作之一。我们可以使用"logrotate"来管理linux日志文件,它可以实现日志的自动滚动,日志归档等功能。下面以nginx日志文件来讲解下logrotate的用法。
配置:
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重新生成日志文件。
============================简易配置================================
more /etc/logrotate.d/nginx
/usr/local/nginx/logs/access.log {
daily
dateext
rotate 10
missingok
notifempty
copytruncate
}
描述:linux日志文件如果不定期清理,会填满整个磁盘。这样会很危险,因此日志管理是系统管理员日常工作之一。我们可以使用"logrotate"来管理linux日志文件,它可以实现日志的自动滚动,日志归档等功能。下面以nginx日志文件来讲解下logrotate的用法。
配置:
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重新生成日志文件。
============================简易配置================================
more /etc/logrotate.d/nginx
/usr/local/nginx/logs/access.log {
daily
dateext
rotate 10
missingok
notifempty
copytruncate
}
相关文章推荐
- HA专题: corosync+pacemaker实现nginx高可用
- Nginx服务器上搭建图片缓存服务的基本配置解析
- nginx忽略favicon.ico日志
- 查看nginx apache mysql php 编译参数
- [Nginx 1] Nginx简介
- [Nginx 1] Nginx简介
- CentOS 6.6编译安装Nginx1.6.2+MySQL5.6.21+PHP5.6.3(转)
- 转:Nginx 配置 location 总结及 rewrite 规则写法
- Nginx 限速模块一览
- nginx 关闭后启动失败
- modSecurity和Naxsi哪个更适合Nginx搭建WAF
- [Zabbix3.0 ]添加Nginx监控
- [Zabbix3.0 ]添加Nginx监控
- nginx 日志分析,python 实现
- 解决Nginx的connect() to 127.0.0.1:8080,selinux
- Nginx 基本配置(log_format,rewrite,proxy)(转)
- nginx配置文件nginx.conf解读
- linux下nginx、php、mysql环境搭建记录和问题汇总
- CentOS7上Nginx的使用
- Ngnix Proxy模块的应用之负载均衡