Detailed description of nginx log file splitting instances

Source: Internet
Author: User
Tags current time

The first step is to rename the log file, so you don't have to worry about losing the log because nginx cannot find the log file after renaming. Before you re-open the log file of the original name, nginx will write logs to the file you renamed. linux uses file descriptors instead of file names to locate the file.

The second step sends the USR1 signal to the nginx main process.

After receiving the signal, the nginx main process reads the log file name from the configuration file and re-opens the log file (named after the log name in the configuration file ), and take the user of the working process as the owner of the log file.

After the log file is re-opened, the nginx main process will close the log file with the same name and notify the working process to use the new log file.

The worker process immediately opens a new log file and closes the log file with a duplicate name.

Then you can process the old log file.

The script for automatic date cutting of nginx logs is as follows:

# Nginx log cutting script

The code is as follows: Copy code
 
#! /Bin/bash
# Set the log file storage directory
Logs_path = "/usr/local/nginx/logs /"
# Set the pid file
Pid_path = "/usr/local/nginx. pid"
 
# Rename a log file
Mv $ {logs_path} access. log $ {logs_path} access _ $ (date-d "yesterday" + "% Y % m % d"). log
 
# Send a signal to the nginx main process to re-open the log
Kill-USR1 'cat $ {pid_path }'

 

Save the above script nginx_log.sh, or click here to download

Crontab setting job

The code is as follows: Copy code

0 0 *** bash/usr/local/nginx/nginx_log.sh

In this way, the nginx log is renamed to the date format at 00:00 every day, and the new log file of today is regenerated.


Let's look at an instance.


My server generates an access. log with a size of 21 GB. Next I will introduce the solution to this problem.

For cutting on Linux, you need to use the date command to obtain the date of yesterday, use the kill command to send the signal to the Nginx process to re-open the log file, and crontab to set the task execution cycle.

Create a Shell script as follows:

Shell code

The code is as follows: Copy code
1 .#! /Bin/bash
2. ## execute the script
3.
4. ## Directory of the Nginx log file
5. LOGS_PATH =/usr/local/nginx/logs
6.
7. ## obtain yesterday's yyyy-MM-dd
8. YESTERDAY = $ (date-d "yesterday" + % Y-% m-% d)
9.
10. ## move a file
11. mv $ {LOGS_PATH}/access. log $ {LOGS_PATH}/access _ $ {YESTERDAY}. log
12.
13. ## send the USR1 signal to the Nginx main process. The USR1 signal re-opens the log file
14. kill-USR1 $ (cat/usr/local/nginx. pid)
#! /Bin/bash
# Execute the script
# Nginx log file directory
LOGS_PATH =/usr/local/nginx/logs
# Retrieve yesterday's yyyy-MM-dd
YESTERDAY = $ (date-d "yesterday" + % Y-% m-% d)
# Move a file
Mv $ {LOGS_PATH}/access. log $ {LOGS_PATH}/access _ $ {YESTERDAY}. log
# Send the USR1 signal to the Nginx main process. The USR1 signal re-opens the log file
Kill-USR1 $ (cat/usr/local/nginx. pid)

The last line in this script must send the USR1 signal to the Nginx process to re-open the log file, nginx will continue to write log information into access _ [yyyy-MM-dd]. this is obviously incorrect in the log file.

After the script is complete, save it to the sbin of the Nginx installation directory, named cut-log.sh, and then use crontab-e to add a scheduled task in which to execute the script:

Shell code

The code is as follows: Copy code
1.0 0 ***/bin/bash/usr/local/nginx/sbin/cut-log.sh
0 0 ***/bin/bash/usr/local/nginx/sbin/cut-log.sh

In Linux, the Nginx log cutting is complete. You can set crontab to a time closer to the current time for testing. Otherwise, the problem will not be solved at zero.

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.