Nginx Log Rotation


One of my site is running on Nginx server, and the logs (access log and error log) is getting large very quick . I don’t want to turn it off, because sometime I need to check them. Without using log rotation, I think it’s not good for performance because write into a large file (says 1G) could be slow (correct me if you don’t agree), and another big issue is that it takes long time to open a large log file. So I wanted to rotate the logs.

I found that in fact, the log rotation was already enabled in my server, which is configured by “/etc/logrotate.d/nginx”. Open that file I see the following content,

/var/log/nginx/*log {
    rotate 10
        /etc/init.d/nginx reopen_logs

The reason my site’s log was not being rotated is because when I set up this site, the log path was not under “/var/log/nginx/.”

It’s easy to fix. Open “/etc/nginx/conf.d/mysite.conf”, and edit the paths of access_log and error_log to be under /var/log/nginx/ will fix the problem.

Note: my server is running ‘CentOS release 6.3’ and the nginx version is 1.2.5, so the path info mentioned in the article may not suitable to your system, but you should be able to do the similar settings.

Leave a Reply

Your email address will not be published. Required fields are marked *