Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am serving a couple different domains through NginX on the same server and they each log to their own file. I need to set up a script to rotate, and compress these files and add it to cron.

I know I have to do something to get NginX to open a new log file once I move the old one. Can someone give me the procedure for safely rotating nginx log files? I'm guessing I need to use logrotate, how do I configure it?

System:

  • Ubuntu 9.04 server ed.
  • nginx/0.7.61
share|improve this question
add comment

migrated from stackoverflow.com Jun 28 '11 at 3:36

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 17 down vote accepted

It has become a sort-of informal semi-standard among Unix daemons that they flush and/or rotate their log files, when you send them a hangup signal (SIGHUP). Nginx doesn't follow this convention to the letter, but it responds to the USR1 signal the same way, as is documented on the Nginx website under the title NginxLogRotation.

So, you could try something like

kill -s USR1 `pidof nginx`
share|improve this answer
1  
another way, "pkill -USR1 -n -x nginx" –  Palani Jul 29 '12 at 6:36
add comment

logrotating nginx logs:

# 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 rails production log:

/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
}
share|improve this answer
    
In which file should I put this? –  Emil Stenström Dec 12 '12 at 11:47
    
if using ubuntu, you should put these lines of code into this file : /etc/logrotate.d/nginx . and it will take effect. –  Siwei Shen Jun 14 '13 at 0:40
add comment

If you use logrotate, add the following (with correct location) into nginx's section of logrotate.conf:

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

According to logrotate(8) man page

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.