Use the built-in logrotate to cut nginx logs

Source: Internet
Author: User

Use the logrotate that comes with the system to cut the nginx log Summary: this stuff can cut any log, not just the NGINX log, it is very easy to use. Logrotate is a log file management tool. It is used to delete the old log file and create a new log file. We call it a "dump ". We can store logs based on the size of the log file or the number of days. This process is generally executed by the cron program. The logrotate program can also be used to compress log files and send logs to the specified email. The configuration file for log cutting that comes with the system is in/etc/logrotate. conf and/etc/logrotate. d/syslog. Observe. Purpose: If the log file is large and the MV takes time, logs may be lost during this time. Logrotate can cut logs without losing data. Vi/etc/logrotate. d/nginx/usr/local/nginx/logs /*. log {daily rotate 7 missingok then empty dateext sharedscripts postrotate if [-f/usr/local/nginx/logs/nginx. pid]; then kill-USR1 'cat/usr/local/nginx/logs/nginx. pid 'fi endscript} script explanation:/usr/local/nginx/logs /*. log {nginx log Path/usr/local/nginx/logs/www.qq.com. log error. log {You can specify the log and separate it with spaces. daily polls rotate 7 every day to keep the 7-day missingok. If the log is lost, continue rolling the next log. When the daily empty log is empty, do not scroll dateext to add the current date sharedscripts at the end of the file to run the postrotate script content, send a signal to NGINX and request to regenerate the log, if nginx is not enabled, do not operate if [-f/usr/local/nginx/logs/nginx. pid]; then kill-USR1 'cat/usr/local/nginx/logs/nginx. pid 'fi endscript} test. If successful, the configuration is successful/usr/sbin/logrotate-f/etc/logrotate. d/nginx configure crontab 00 00 ***/usr/sbin/logrotate-f/etc/logrotate. d/nginx can also be placed in/etc/cron. daily/Be sure to note: 1. Whether to test the Cutting Success. Make sure to back up the data first! If you want to keep all the logs, set a large value such as 3650. If you comment on rotate or do not add, the cut will not succeed. 2. The configuration file for running crond with logrotate is/etc/logrotate. conf. The main parameters are as follows: compress uses this parameter to compress nocompress logs after dumping through gzip without compression. copytruncate is used for log files that are still being opened, back up the current log and cut off nocopytruncate to back up the log file, but do not cut off the create mode owner group dump file, when the specified file mode is used to create a new log file nocreate without creating a new log file delaycompress and compress, the dumped log file will be compressed to the next dump before nodelaycompress overwrites the delaycompress option, the dump is compressed at the same time. The error message sent to the specified Email address ifempty even if it is an empty file is dumped. This is the default option of logrotate. Notifempty, if it is an empty file, do not dump the mail address to send the dump log file to the specified email address nomail dump. Do not send the log file after the olddir directory dump to the specified directory, it must be in the same directory as the current log file after the noolddir ddir dump and the current log file must be placed in the same directory. prerotate/endscript can be placed in this pair before the dump command to be executed, the two keywords must be in a separate line postrotate/endscript. The command to be executed after the dump can be placed in this pair, the two keywords must be separately set to "daily" to "weekly" to "weekly" to "monthly" to "monthly" to "rotate count" each month to specify the number of dump times before log files are deleted. "0" indicates no backup, 5: Keep five copies of tabootext [+] list so that logrotate does not Dump files with the specified extension. The default extension is. rpm-orig,. rpmsave, v, and ~ Size: dump a log file only when it reaches the specified Size. size can specify bytes (default), KB (sizek), or MB (sizem ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.