Cut Nginx Log

Source: Internet
Author: User


First, using logrotate cutting

Yum installed Nginx will automatically use logrotate this log management software for cutting, so this section we mainly introduce about logrotate related knowledge points.

[email protected] bin]# Yum install-y nginx


1.1 Logrotate Introduction

What is Logrotate? It is a management tool for Linux system logs. It can be cut, compressed and other software log file software.

We can install logrotate with the following command, as follows:

[Email protected] bin]# yum-y install Logrotate


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/80/25/wKiom1c5WvezhDbAAAC_nZM6DQE489.png "title=" 9iw$ 15E ' Idsae%wic1@3]zg.png "alt=" Wkiom1c5wvezhdbaaac_nzm6dqe489.png "/>

To view the Logrotate configuration file, use the following command:

[Email protected] bin]# RPM-QL logrotate


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/25/wKiom1c5W1eSjueEAAAjc-zslFE074.png "title=" {r_ vg7vm~% @LAW ' ~) u_aq3j.png "alt=" Wkiom1c5w1esjueeaaajc-zslfe074.png "/>


[Email protected] bin]# cat/etc/logrotate.conf |grep-v ' ^# '

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/80/23/wKioL1c5YpyibG07AAA3omEvvH0731.png "title=" 5.png " alt= "Wkiol1c5ypyibg07aaa3omevvh0731.png"/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/80/26/wKiom1c5YU2RPHheAAASMaguucY776.png "title=" 3.png " alt= "Wkiom1c5yu2rphheaaasmaguucy776.png"/>


We can obviously see that the Logrotate configuration file is/etc/logrotate.conf, and/etc/logrotate.d/is the directory used to store other profiles. All files in this directory are actively read into/etc/logrotate.conf.

1.2 logrotate Detailed configuration file

logrotate The default configuration options are relatively small, in this case, we will cut Nginx configuration file For example, the following configuration:

[Email protected] bin]# Vim/etc/logrotate.d/nginx


/var/log/nginx/*log {

Daily

Rotate 7

Missingok

Dateext

# Compress

Delaycompress

Notifempty

Create 640 Nginx ADM

Sharedscripts


Postrotate

[-f/var/run/nginx.pid] &&/bin/kill-usr1 ' cat/var/run/nginx.pid '

Endscript

}

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/23/wKioL1c5YH3y1O2QAAAoz5LYN9o271.png "title=" 1.png " alt= "Wkiol1c5yh3y1o2qaaaoz5lyn9o271.png"/>


In this configuration file, each parameter acts as follows:

/var/log/nginx/is the storage directory of Nginx log, can be modified according to the actual situation.

Daily: The log file will be followed by the day round.

Weekly: The log file will be followed by a weekly rotation.

Monthly: The log file will be round by month.

Missingok: During log rotation, any errors will be ignored, such as "files cannot be found".

Rotate 7: Store 7 log files at a time. For the 8th log file, the longest log file will be deleted.

Dateext: The definition log file suffix is a date format, that is, after the cut file is: xxx.log-20160402.gz such format. If the parameter is commented out, the cut is incremented by the number, which is the format of the xxx.log-1 mentioned earlier.

Compress: After a round-robin task is completed, the archived archive will be compressed using gzip.

Delaycompress: Always with the Compress option, the Delaycompress option indicates that logrotate does not compress the most recent archive, and compression will take place during the next round-robin cycle. This is useful when you or any software still needs to read the latest archive.

Notifempty: If it is an empty file, do not dump it.

Create 640 Nginx ADM: Creates a completely new log file with the specified permissions and book properties, while Logrotate renames the original log file.

Postrotate/endscript: After all other instructions have been completed, the commands specified inside postrotate and Endscript will be executed. In this case, the RSYSLOGD process will immediately read its configuration again and continue to run. Note: These two keywords must be in a separate row.



1.3 viewing logrotate default execution time

Logrotate is based on crontab operation, so this time point is controlled by crontab, specifically can query crontab configuration file/etc/anacrontab.



Logrotate cutting time is the default at 3:22 per day. This point in time can be seen through the crontab configuration file. As follows:


[Email protected] ~]# Cat/etc/anacrontab

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/80/23/wKioL1c5ZILDk6xdAABSmnUQO3k949.png "title=" 7.png " alt= "Wkiol1c5zildk6xdaabsmnuqo3k949.png"/>


If you want to have logrotate cut logs at a specified point in time, you can modify the Start_hours_range parameter for this profile.

Cut Nginx Log

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.