NginX日志旋转

我在同一台服务器上通过NginX服务于几个不同的域,他们每个都login到自己的文件。 我需要设置一个脚本来旋转,并压缩这些文件,并将其添加到cron。

我知道我必须做一些事情,让NginX打开一个新的日志文件,一旦我移动旧的。 有人可以给我安全地旋转nginx日志文件的程序吗? 我猜我需要使用logrotate,我该如何configuration它?

系统:

  • Ubuntu 9.04服务器编辑。
  • nginx的/ 0.7.61

在Unix守护进程中,当它们发送一个挂断信号( SIGHUP )时,它们会刷新和/或旋转它们的日志文件,这已经成为一种非正式的半非标准的方式。 Nginx并没有遵循这个约定,而是以相同的方式对USR1信号作出响应,正如Nginx网站上的Log Rotation标题所logging的那样 。

所以,你可以尝试类似的东西

 kill -s USR1 `pidof nginx` 

logrotating nginx日志:

 # nginx SIGUSR1: Re-opens the log files. /opt/nginx/logs/access.log { missingok notifempty delaycompress sharedscripts postrotate test ! -f /opt/nginx/logs/nginx.pid || kill -USR1 `cat /opt/nginx/logs/nginx.pid` endscript } /opt/nginx/logs/error.log { missingok notifempty delaycompress sharedscripts postrotate test ! -f /opt/nginx/logs/nginx.pid || kill -USR1 `cat /opt/nginx/logs/nginx.pid` endscript } 

logrotating导轨生产日志:

 /home/app_user/apps/railsapp/log/production.log { missingok notifempty delaycompress sharedscripts postrotate test ! -f /opt/nginx/logs/nginx.pid || kill -USR1 `cat /opt/nginx/logs/nginx.pid` endscript } 

如果你使用logrotate,把以下内容(使用正确的位置)添加到logrotate.conf的nginx部分:

 postrotate kill -s USR1 `cat /location/of/nginx.pid` endscript 

根据logrotate(8)手册页