Requirements: Use logrotate to cut nginx logs daily, automatically delete logs older than 60 days, and compress and archive historical logs. Prevent a single nginx log file from being too large and causing disk space to run out or read less efficiently.
Review:
logrotate
In Linux environments, logrotate is a very useful log cutting tool for automating the management and rotation of log files to avoid running out of disk space or reducing read efficiency due to the large size of a single log file. Linux already comes with a logrotate tool. If not, you can install it with the following command:
The parameters are as follows:
Usage: logrotate [OPTION...] <configfile> -d, --debug Don't do anything, just test and print debug messages -f, --force Force file rotation -m, --mail=command Command to send mail (instead of `/bin/mail') -s, --state=statefile Path of state file -v, --verbose Display messages during rotation -l, --log=logfile Log file or 'syslog' to log to syslog --version Display version information
Help options: -?, --help Show this help message --usage Display brief usage message
logrotate configuration
Manually create a logrotate configuration file for Nginx under the /etc/logrotate.d/ directory (/etc/logrotate.d/nginx), which is configured as follows:
To test if the configuration is correct, the command is as follows:
Enforce it once with the following command:
As shown below:
To view the last successful run of the task, the command is as follows:
As shown below:
logrotate execution time
logrotate is based on the crond service, and its script for the crond service is/etc/cron.daily/logrotate, you need to pay attention to when logrotate performs log splitting/etc/anacrontaband /var/lib/logrotate/logrotate.status. It is a configuration file for anacron that ensures that periodic tasks (such as log rotation) are missed on systems that boot irregularly (e.g. laptops, servers that occasionally shut down).Supplementary execution。
Reference:
The hyperlink login is visible.
The hyperlink login is visible. |