Managing Nginx log files with Logrotate

Source: Internet
Author: User

This article was reproduced from: http://linux008.blog.51cto.com/2837805/555829

Description:The Linux log file will fill the entire disk if it is not cleaned up regularly. This can be dangerous, so log management is one of the daily tasks of the system administrator. We can use "logrotate" to manage the Linux log files, it can achieve automatic scrolling log, log archiving and other functions. The following is an nginx log file to explain the use of logrotate.

Configuration:
1, in the/ETC/LOGROTATE.D directory to create a nginx configuration file "nginx" configuration content 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 '
Fi
Endscript
}


Save exit.

2, the implementation of Logrotate

#/usr/sbin/logrotate-f/etc/logrotate.d/nginx


The/usr/local/nginx/logs directory will produce
Error.log
Error.log.1
Description Logrotate configuration succeeded.

3. Let logrotate scroll once a day and add a line of timed scripts to the crontab.

#crontab-E
* * * */usr/sbin/logrotate-f/etc/logrotate.d/nginx


Log scrolling every 23:59

4. Configuration file Description
Daily: Log files are scrolled daily
Rotate: Retain the 5 scrolling log
Notifempty: Log file is empty and does not scroll
Sharedscripts: Run postrotate Script
Here is a script

Postrotate
if [-f/usr/local/nginx/logs/nginx.pid]; Then
KILL-USR1 ' Cat/usr/local/nginx/logs/nginx.pid '
Fi
Endscript


The script lets Nginx regenerate the log file.

============================ Simple Configuration ================================

More/etc/logrotate.d/nginx
/usr/local/nginx/logs/access.log {
Daily
Dateext
Rotate 10
Missingok
Notifempty
Copytruncate
}

Managing Nginx log files with Logrotate

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.