Fix Nginx Logrotate Not Writing To New File | Use Old log1 File

After you have built a new nginx server, you might experience that its Nginx not writing log files to new files even if the logrotate working at schedule time. In my situation, the nginx server writes log files to “.log1” file, even if there’s a new “.log” file newly created.

This issue can happen as a result of improper permission given. In this tutorial I’m going to show how to correct this issue on Ubuntu server.

When you run the following command, if it gives Permission denied output, you have to fix the several permission on your server.

/usr/sbin/invoke-rc.d: 524: /usr/sbin/invoke-rc.d: /etc/init.d/nginx: Permission denied
invoke-rc.d: initscript nginx, action “rotate” failed.

Nginx Logo

Check logs directory and log file has the necessary permission.

Nginx default log file directory located at “/var/log/nginx”, if you have custom configuration get the location and run the below command.

First run below command.

Then run following command.

It will correct the above directories necessary rwxr-xr-x permission and change the group and user as “www-data [33]“.  If your nginx configuration has customized settings you can view them by running “nginx -V” command.

Check init.d/nginx Executable.

If your server does not require permission to execute the below command, you have to change the /etc/init.d/nginx/ directory permission as follow.

Run below command in SSH.

Next, check the following command and execute on your server.

If it gives bellow output you are good to go.

Logrotator sucess output

Apart from that, you can add “copytruncate” to your logrotate file in “/etc/logrotate.d” directory.

Run logrotate manually

Finally, run the run logrotate manually to check still nginx logrotate use old log or new one.