This article is a mirror article of machine translation, please click here to jump to the original article.

View: 231|Reply: 0

[Web] Linux servers cut nginx logs based on logrotate

[Copy link]
Posted on 2025-10-12 19:26:53 | | | |
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:
Nginx log splitting and deletion
https://www.itsvse.com/thread-9259-1-1.html

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.




Previous:ASP.NET Core (32) depends on injecting KeyedServices key services
Next:A brief introduction to Hong Kong digital (virtual) banks
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com