LoginSignup
89
78

More than 5 years have passed since last update.

Nginxログローテートの設定の実例

Last updated at Posted at 2015-12-02

設定

実行権限の確認

ll /etc/logrotate.d/nginx
-rw-r--r-- 1 root root 403 Nov 26 19:36 /etc/logrotate.d/nginx

logroteの設定

/etc/logrotate.d/nginx
# Do not modify this file by hand!

"/var/log/nginx/access.log" "/var/log/nginx/error.log" {
  missingok
  notifempty
  daily
  rotate 7
  create 644 www-data root # permission と owner は適宜注意
  compress
  delaycompress
  notifempty
  sharedscripts
  postrotate
      [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
  endscript
}

解説

プロパティ 役割
missingok ログファイルが見つからなくてもエラーにしない。
rotate n n世代までログをローテート。それ以上は削除
compress ローテートしてログを圧縮
delaycompress 1つ前のファイルはまだ圧縮しない。それ以外を圧縮
notifempty ログファイルが空ならローテーションしない
create ローテーション後、新たにログファイルを作成する。権限、ユーザ、グループを指定。
sharedscripts スクリプト宣言文。以降に記述された処理をワイルドカードの指定に関わらず、1度だけ実行する
postrotate ログローテーション実施後に実行される部分
  • 注意点として、ログを単純にローテートしても、Nginxのログの書き込み先は変わらない。
  • そのためaccess.log.1とリネームして、新しくaccess.logを作ったとしても、ログはaccess.log.1に書き込まれ続ける。
  • 書き込み先を新しいaccess.logにする処理をpostrotateに設定する。

    • この処理はnginx をインストールした際に設定されるものをそのまま使っている。
    • 権限周りでうまくログローテート出来ないなどあるため実行権限は一度確認する
  • kill option

次のコマンドは指定されたシグナルを指定されたプロセスに送ります。

kill -USR1 1103

このコマンドは SIGUSR1 シグナルをプロセス 1103 に送ります。 SIGUSR1 シグナ
ルでとられるアクションは,ユーザが実行中の特定のアプリケーションによって定
義されます。

logrotate コマンド

編集したnginxファイルをdebugしたい場合は-dオプションを使う

logrotate -d /etc/logrotate.d/nginx 
reading config file /etc/logrotate.d/nginx

Handling 1 logs

rotating pattern: "/var/log/nginx/access.log" "/var/log/nginx/error.log"  after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  log does not need rotating
considering log /var/log/nginx/error.log
  log does not need rotating
not running postrotate script, since no logs were rotated

手動で実行したい場合は-fを使う

logrotate -f /etc/logrotate.d/nginx 

参考

89
78
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
89
78