logrotateの確認

2022/03 の作業


NGINX が動いているけど、 logrotate が動いた後、access.log が出力されなくなった。

logrotate の NGINX の設定は、 インストール時に作成されたものをそのまま使っている。


現在の設定値は、

$ sudo cat /etc/logrotate.d/nginx
/var/log/nginx/*log {
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}


logrotate が動く前は、

-rw-r--r-- 1 root  root  481 Mar 22 15:55 access.log
-rw-r--r-- 1 root  root  418 Mar 22 15:33 error.log

こんな感じで、動いた後は、

-rw-r--r-- 1 nginx root   0 Mar 22 16:02 access.log
-rw-r--r-- 1 root  root 242 Mar 22 15:55 access.log.1.gz
-rw-r--r-- 1 nginx root   0 Mar 22 16:02 error.log
-rw-r--r-- 1 root  root 141 Mar 22 15:33 error.log.1.gz


こんな感じで、アクセスしてもアクセスログに出力されない。

ログが無い所から起動したら root のファイルだったのに、ローテしたら、nginx のファイルになっている。


プロセスと inode を確認。

$ sudo systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
・・・・
 Main PID: 4101 (nginx)
   CGroup: /system.slice/nginx.service
           4101 nginx: master process /usr/sbin/nginx
           4102 nginx: worker process
・・・・

$ sudo lsof -p 4101
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF     NODE NAME
・・・・
nginx   4101 root    2w   REG              202,1      418  2149199 /var/log/nginx/error.log
nginx   4101 root    5w   REG              202,1      418  2149199 /var/log/nginx/error.log
nginx   4101 root   10w   REG              202,1        0 2026974 /var/log/nginx/access.log
・・・・

$ sudo lsof -p 4102
COMMAND  PID  USER   FD      TYPE             DEVICE SIZE/OFF     NODE NAME
・・・・
nginx   4102 nginx    2w      REG              202,1      418  2149199 /var/log/nginx/error.log
nginx   4102 nginx    4w      REG              202,1      418  2149199 /var/log/nginx/error.log
nginx   4102 nginx    5w      REG              202,1     3902 2149201 /var/log/nginx/access.log.1 (deleted)
・・・・


子プロセスの access.log の inode らしきものが、ローテ前の時の方のファイルになっているみたいだ。
( 親プロセスと違う NODE になっている )


/etc/logrotate.d/nginx ファイルの postrotate から endscript のコマンドで、nginx は USR1 のシグナルで出力ファイルを開きなおすらしいが子プロセスがアクセスできていないみたいだ。

nginx ユーザが書けるか試してみる。

$ sudo su -s /bin/bash - nginx -c "echo b >> /var/log/nginx/access.log"
-bash: /var/log/nginx/access.log: Permission denied


出力できない。

起動後は出力されていたのにローテ後、出力できないのは不思議だ。

( root でオープンして、fork したとき渡しているから大丈夫なのか? )

drwx------  2 root   root                86 Mar 22 17:14 nginx

から

drwx--x--x  2 nginx   root                86 Mar 22 17:16 nginx

にして、


NGINX を再起動して、

$ sudo systemctl restart nginx.service

アクセスして、access.log が出力されるのを確認して、


強制ローテして、

$ sudo logrotate -f /etc/logrotate.d/nginx

バックアップができて、0バイトの access.log、error.log ができているのを確認して、

アクセスして、access.log に出力されているので、大丈夫そう。