Use logrotate to manage nginx log files

Source: Internet
Author: User
Description: If linux log files are cleared occasionally, the entire disk is filled. This is dangerous, so log management is one of the daily work of the system administrator. We can use logrotate to manage linux log files, which can implement automatic log scrolling, log archiving, and other functions. The following describes n...: If linux log files are not cleaned regularly, the entire disk will be filled. This is dangerous, so log management is one of the daily work of the system administrator. We can use "logrotate" to manage linux log files, which can implement automatic log scrolling, log archiving, and other functions. The following uses the nginx log file to describe how to use logrotate.
 
Configuration:
1. create an nginx configuration file "nginx" under the/etc/logrotate. d Directory. the configuration content is as follows:
 
# Vim/etc/logrotate. d/nginx
/Usr/local/nginx/logs/*. log {
Daily
Rotate 5
Missingok
Notifempty
Sharedscripts
Postrotate
If [-f/usr/local/nginx/logs/nginx. pid]; then
Kill-USR1 cat/usr/local/nginx/logs/nginx. pid cat to the end there is a floating point number, that is, in the upper left corner of the keyboard
Fi
Endscript
}
Save and exit.
 
2. execute logrotate
 
#/Usr/sbin/logrotate-f/etc/logrotate. d/nginx
In the/usr/local/nginx/logs Directory
Error. log
Error. log.1
Logrotate is successfully configured.
 
3. set logrotate to scroll once a day and add a regular script to the crontab.
 
# Crontab-e
59 23 ***/usr/sbin/logrotate-f/etc/logrotate. d/nginx
Log scrolling at every day
 
4. configuration file description
Daily: log files are scrolled every day.
Rotate: Keep up to 5 rolling logs
Receivempty: the log file is empty and does not scroll.
Sharedscripts: run the postrotate script
Below is a script
 
Postrotate
If [-f/usr/local/nginx/logs/nginx. pid]; then
Kill-USR1 cat/usr/local/nginx/logs/nginx. pid'
Fi
Endscript
Script for nginx to regenerate the log file

From http://baoz.net/zz-%e4%bd%bf%e7%94%a8logrotate%e7% AE %a1%e7%90%86nginx%e6%97%a5%e5%bf%97%e6%96%87%e4%bb%b6/
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.