Nginx 自己没有处理日志的滚动问题,它把这个球踢给了使用者。一般情况下,你可以使用 logrotate 工具来完成这个任务,或者如果你愿意,你可以写各式各样的脚本完成同样的任务。本文笔者介绍如何滚动运行在 docker 中的 nginx 日志文件(下图来自互联网)。

思路
Nginx 官方其实给出了如何滚动日志的说明:
Rotating Log-files
In order to rotate log files, they need to be renamed first. After that USR1 signal should be sent to the master process. The master process will then re-open all currently open log files and assign them an unprivileged user under which the worker processes are running, as an owner. After successful re-opening, the master process closes all open files and sends the message to worker process to ask them to re-open files. Worker processes also open new files and close old files right away. As a result, old files are almost immediately available for post processing, such as compression.
这段说明的大意是:
- 先把旧的日志文件重命名
- 然后给 nginx master 进程发送 USR1 信号
- nginx master 进程收到信号后会做一些处理,然后要求工作者进程重新打开日志文件
- 工作者进程打开新的日志文件并关闭旧的日志文件
其实真正需要我们做的工作只有前面两点!
创建测试环境
假设你的系统中已经安装好了 docker,这里我们直接运行一个 nginx 容器:
$ docker run -d \ -p 80:80 \ -v $(pwd)/logs/nginx:/var/log/nginx \ --restart=always \ --name=mynginx \ nginx:1.11.3
注意,我们把 nginx 的日志绑定挂载到了当前目录下的 logs 目录下。
把下面的内容保存到 test.sh 文件中:
#!/bin/bash for ((i=1;i<=100000;i++)) do curl http://localhost > /dev/null sleep 1done
然后运行这个脚本,就可以模拟产生连续的日志记录。
创建滚动日志的脚本
创建 rotatelog.sh 文件,其内容如下:
#!/bin/bash getdatestring() { TZ='Asia/Chongqing' date "+%Y%m%d%H%M" } datestring=$(getdatestring) mv /var/log/nginx/access.log /var/log/nginx/access.${datestring}.log mv /var/log/nginx/error.log /var/log/nginx/error.${datestring}.log kill -USR1 `cat /var/run/nginx.pid`

