网站首页 > 技术教程 正文
Nginx 日志滚动(官方)
向 Nginx 主进程发送 USR1 信号。
USR1 信号量被 Nginx 自定义了,为重新打开日志;当 kill 命令发送 USR1时,nginx 会重新打开日志文件,并重新创建进程。
# nginx 官方提供的日志滚动方式
$ mv access.log access.log.0
$ kill -USR1 `cat master.nginx.pid`
$ sleep 1
$ gzip access.log.0 # do something with access.log.0
logrotate 管理 Nginx 日志
logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.
logrotate 是一个日志文件管理工具。用于分割日志,删除旧的日志,并创建新的日志文件,起到日志滚动的作用。
logrotate 是基于 linux 的 CRON 来运行的,其脚本是 /etc/cron.daily/logrotate。
安装 logrotate
Linux 一般会默认安装logrotate,它默认的配置文件在:
# 配置文件
$ cat /etc/logrotate.conf | grep -v '^#' | grep -v '^#39;
weekly
rotate 4
create
dateext
include /etc/logrotate.d
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# logrotate 配置文件目录,在 /etc/logrotate.conf 中会引用该目录下的所有文件
$ ls -lt /etc/logrotate.d/
安装 logrotate:
$ yum install logrotate
配置 logrotate
# nginx logratate 配置文件
$ vi /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
# 指定转储周期为每天
daily
# 使用当期日期作为命名格式
dateext
# 如果日志丢失,不报错继续滚动下一个日志
missingok
# 保留 31 个备份
rotate 31
# 不压缩
nocompress
# 整个日志组运行一次的脚本
sharedscripts
# 转储以后需要执行的命令
postrotate
# 重新打开日志文件
[ ! -f /usr/local/nginx/nginx.pid ] || kill -USR1 `cat /usr/local/nginx/nginx.pid`
endscript
}
配置文件参数说明:
logrotate 命令参数
logrotate [OPTION...] <configfile>
-d, --debug :debug模式,测试配置文件是否有错误。
-f, --force :强制转储文件。
-m, --mail=command :压缩日志后,发送日志到指定邮箱。
-s, --state=statefile :使用指定的状态文件。
-v, --verbose :显示转储过程。
手动执行 logrotate
# '-d' 调试模式(不切分日志文件),并输出详细处理过程日志
$ logrotate -d -f /etc/logrotate.d/nginx
# '-f' 强制切分日志,'-v' 输出详细信息
$ logrotate -vf /etc/logrotate.d/nginx
reading config file nginx
Allocating hash table for state file, size 15360 B
Handling 1 logs
rotating pattern: /usr/local/nginx/logs/*.log forced from command line (100 rotations)
empty log files are rotated, old logs are removed
considering log /usr/local/nginx/logs/access.log
log needs rotating
considering log /usr/local/nginx/logs/error.log
log needs rotating
rotating log /usr/local/nginx/logs/access.log, log->rotateCount is 100
dateext suffix '-20201121'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
rotating log /usr/local/nginx/logs/error.log, log->rotateCount is 100
dateext suffix '-20201121'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /usr/local/nginx/logs/access.log to /usr/local/nginx/logs/access.log-20201121
renaming /usr/local/nginx/logs/error.log to /usr/local/nginx/logs/error.log-20201121
running postrotate script
# 切分后的日志文件
$ ls -lt /usr/local/nginx/logs
总用量 0
-rw-r--r-- 1 nginx root 0 11月 21 18:58 access.log
-rw-r--r-- 1 nginx root 0 11月 21 18:57 access.log-20201121
-rw-r--r-- 1 nginx root 0 11月 21 18:58 error.log
-rw-r--r-- 1 nginx root 0 11月 21 18:57 error.log-20201121
-rw-r--r-- 1 nginx root 0 11月 21 18:58 images.log
-rw-r--r-- 1 nginx root 0 11月 21 18:57 images.log-20201121
猜你喜欢
- 2024-10-10 通过什么方式来读取和监控nginx的日志呢
- 2024-10-10 linux运维之nginx日志之 log_format详解
- 2024-10-10 Nginx 日志自动切割(Centos6.8)(nginx日志切割配置)
- 2024-10-10 nginx - linux上日志分割(nginx日志级别顺序)
- 2024-10-10 练习Nginx定时备份日志及正则配置
- 2024-10-10 nginx启动时报错的日志 端口查看(nginx启动日志在哪儿)
- 2024-09-16 Nginx奇技淫巧之:按日期自动生成日志文件
- 2024-09-16 nginx的日志切割(nginx的日志切割脚本)
- 2024-09-16 ELK日志系统之使用Rsyslog快速方便的收集Nginx日志
- 2024-09-16 nginx 部署的项目查看错误日志并处理
你 发表评论:
欢迎- 08-06linux 和 windows文件格式互相转换
- 08-06谷歌 ChromeOS 已支持 7z、iso、tar 文件格式
- 08-06Linux下比较文件内容的6种方法
- 08-06文件格式及功能汇总
- 08-0610个Linux文件内容查看命令的实用示例
- 08-06Linux-如何区分不同文件类型
- 08-06Zabbix技术分享——监控windows进程资源使用情况
- 08-06Linux系统卡顿?学会ps命令这三招,轻松定位问题进程
- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)