How to set a maximum file size for log files using Logrotate

Set a maximum file size value for log files using Logrotate.

Using Logrotate, you can set a maximum file size for the log files before they are rotate (i.e. a new log file is created). In our example below, we're using Nginx log files:

  1. Edit the logrotate configuration file:

     $ nano /etc/logrotate.d/nginx
  2. Use the size parameter and enter the value in MB. For example, 100M is the maximum file size for the logs before they rotate.

     /var/log/nginx/*.log {
        ...
        size 100M
        ...
     }

    Logrotate configuration with size parameter to set the maximum file size for logs

  3. Save the /etc/logrotate.d/nginx file changes.