安装 Ubuntu 的时候,nginx 的 logrotate 的默认设置是错误的!
引言
在我的环境中,我使用fluentd来tail收集/var/log/nginx/access.log和/var/log/nginx/error.log。但是,在我将PPA添加到Ubuntu 14.04并安装nginx后,无法进行tail操作。
※ 在Ubuntu 14.04中不添加PPA并安装nginx时,可以进行tail操作。
查看日志文件后,发现应该输出到 /var/log/nginx/access.log 的日志输出到了 /var/log/nginx/access.log.1,应该输出到 /var/log/nginx/error.log 的日志输出到了 /var/log/nginx/error.log.1。
经过调查,发现在添加 PPA 并安装 nginx 时,logrotate 的默认设置似乎有误。
环境
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
nginx的版本
我添加了ppa:nginx/stable并安装了nginx。
$ nginx -v
nginx version: nginx/1.10.1
logrotate 的默认设置有哪些问题
默认设置
安装nginx时的logrotate默认设置如下:
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
invoke-rc.d nginx rotate >/dev/null 2>&1
endscript
}
默认设置的说明
daily
ログローテーションを日毎に行う。missingok
ログファイルが見つからなくてもエラーにしない。rotate n
n 世代までログをローテート。それ以上は削除。compress
ローテートしてログを圧縮。delaycompress
1つ前のファイルはまだ圧縮しない。それ以外を圧縮。notifempty
ログファイルが空ならローテーションしない。create
ローテーション後、新たにログファイルを作成する。権限、ユーザ、グループを指定。sharedscripts
スクリプト宣言文。以降に記述された処理をワイルドカードの指定に関わらず、1度だけ実行する。postrotate
ログローテーション実施後に実行される部分。哪里出错了?
postrotate的默认设置中的命令设置错误。
postrotate
invoke-rc.d nginx rotate >/dev/null 2>&1
endscript
即使对日志进行了轮转,nginx的日志输出文件不会发生改变。
即使access.log被轮转并重命名为access.log.1,日志的输出仍然是access.log.1。
因此,必须在postrotate中设置将输出目标更改为新的access.log。
然而,上述设定的指令在执行时会出现以下错误。
$ invoke-rc.d nginx rotate
initctl: invalid command: rotate
Try `initctl --help' for more information.
invoke-rc.d: initscript nginx, action "rotate" failed.
$ initctl --help
Usage: initctl [OPTION]... COMMAND [OPTION]... [ARG]...
Options:
--session use existing D-Bus session bus to connect to init daemon (for testing)
--system use D-Bus system bus to connect to init daemon
--dest=NAME destination well-known name on D-Bus bus
--user run in user mode (as used for user sessions)
-q, --quiet reduce output to errors only
-v, --verbose increase output to include informational messages
--help display this help and exit
--version output version information and exit
For a list of commands, try `initctl help'.
Report bugs to <upstart-devel@lists.ubuntu.com>
解决方案是将以下命令设置为执行。
$ sudo /etc/init.d/nginx rotate
* Re-opening nginx log files nginx
...done.
或者
$ sudo service nginx rotate
* Re-opening nginx log files nginx
...done.
正确的设置
那么,让我们设置执行上面解释的命令。
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
- invoke-rc.d nginx rotate >/dev/null 2>&1
+ /etc/init.d/nginx rotate >/dev/null 2>&1
endscript
}
设定后,使用logrotate命令进行调试。
使用-d –debug选项进入调试模式。
$ sudo logrotate -d /etc/logrotate.d/nginx
reading config file /etc/logrotate.d/nginx
Handling 1 logs
rotating pattern: /var/log/nginx/*.log after 1 days (14 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
log needs rotating
considering log /var/log/nginx/error.log
log does not need rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 14
随附的附件
既然如此,我们只需要在Ubuntu 14.04上安装nginx而不添加PPA存储库,然后查看logrotate的默认设置。
nginx的版本
$ nginx -v
nginx version: nginx/1.4.6 (Ubuntu)
默认设置
/var/log/nginx/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
[ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
endscript
}
看到postrotate命令后,我察觉它杀掉了nginx进程的PID。
所以才明白为什么它运行了!
可以提供的选项:
参考资料
文献参考
参考书目
- Nginxのログをlogrotateでローテーション – Carpe Diem