TL;DR
nginxをガッツリを勉強していきたいと思っています。
それにあたりこの本を一冊読みとおしながら、気になったところはハンズオンで動かします。
今回はログローテーションです。
環境
githubにてdocker環境を公開してますので、お使いになりたい方はどうぞ。
試していく
/etc/logrotate.d/nginxという設定ファイルにて挙動を定義する。
設定項目の詳細についてはこちらのqiitaの記事を参照。
設定ファイルを変更したら、実際にログローテーションを実行する。
# logrotateがインストールされているか確認
which logrotate
#パスに見つからんと言われたらインストール
yum -y install logrotate
#実行する
yum -y install logrotate
成功すると、以下のようにログローテーションファイルが生成される。
-rw-rw-r-- 1 nginx root 0 Sep 28 11:52 example.access.log
-rw-r--r-- 1 root root 285 Sep 28 11:46 example.access.log.1.gz
-rw-rw-r-- 1 nginx root 0 Sep 28 11:52 example.error.log
-rw-r--r-- 1 root root 245 Sep 28 11:44 example.error.log.1.gz
あとはこのコマンドをcronとかに仕込んで定期実行しておけばOK。
ちなみに、ログローテーション前後でログに変更がなければローテーションは発生しない。
また、設定した世代以上のローテションが起こった場合は最後の世代が削除されている。
以下はその確認結果。
Sep 28 11:46 example.access.log.9.gzのファイルがSep 28 12:41 example.access.log.9.gzに変わっている。
-rw-rw-r-- 1 nginx root 0 Sep 28 12:43 example.access.log
-rw-rw-r-- 1 nginx root 183 Sep 28 12:43 example.access.log.1.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:43 example.access.log.2.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:43 example.access.log.3.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:43 example.access.log.4.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:43 example.access.log.5.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:42 example.access.log.6.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:42 example.access.log.7.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:41 example.access.log.8.gz
-rw-r--r-- 1 root root 285 Sep 28 11:46 example.access.log.9.gz
-rw-rw-r-- 1 nginx root 0 Sep 28 12:44 example.access.log
-rw-rw-r-- 1 nginx root 195 Sep 28 12:44 example.access.log.1.gz
-rw-r--r-- 1 root root 285 Sep 28 11:46 example.access.log.10.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:43 example.access.log.2.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:43 example.access.log.3.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:43 example.access.log.4.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:43 example.access.log.5.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:43 example.access.log.6.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:42 example.access.log.7.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:42 example.access.log.8.gz
-rw-rw-r-- 1 nginx root 183 Sep 28 12:41 example.access.log.9.gz