Log rotation on Passenger + Nginx

There are three different kinds of log files:

  1. The Nginx webserver log files.
  2. The Passenger log files.
  3. The application log files.

Passenger & webserver log rotation

Rotation via logfile change

Passenger logs to the global webserver error log, which is opened once. If you change this file during rotation (e.g. rename it for archival, and create a new one), then you need to instruct Passenger to open the new logfile by running:

passenger-config reopen-logs

The webserver should also be made aware of the rotation, if it isn't already.

Rotation using a pipe

A trick to avoid having to signal Passenger and/or webservers is to use logging via an intermediate: a named pipe. The pipe stays the same towards Passenger, and can be signaled itself to redirect logging to a new file.

This can be accomplished as follows:

  1. Save this script as: /usr/local/bin/pipetool
  2. Give it executable permissions: sudo chmod +x /usr/local/bin/pipetool
  3. Create a named pipe (FIFO) somewhere on the filesystem: mkfifo $HOME/passenger.pipe
  4. Run the pipetool on this pipe, and have it forward data to a log file: nohup pipetool $HOME/passenger.log < $HOME/passenger.pipe &

With the pipe in place, you can configure Nginx:

passenger_debug_log_file /home/app/passenger.pipe;

Finally, log rotation can be achieved via a logfile rename and pipe signal, for example:

mv $HOME/passenger.log $HOME/passenger.log.1
killall -HUP pipetool

Renaming and signaling can be automated with the logrotate tool, for example with this configuration:

/home/app/passenger.log {
    missingok
    notifempty
    compress
    copytruncate
    daily
    rotate 8
    create 0644 nobody nobody
    postrotate
           killall -HUP pipetool
    endscript
}

Application log rotation

Applications can have their own logfiles (e.g. Rails production.log), which are not related to - or controlled by - Passenger. If the logfile is rotated without the application being aware of the process, you'll need to restart it:

passenger-config restart-app <path to app>

Some applications just log to STDOUT, which is captured by Passenger and logged into the Passenger logfile (i.e. the global webserver error log). So if this is the case the application doesn't need a restart and you can just follow the log rotation procedure for Passenger.