Nginx log cutting

Source: Internet
Author: User
The log file of nginx does not have the rotate function. If you do not process it, the log file will become larger and larger. Fortunately, we can write an nginx log cutting script to automatically cut the log file. 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 .... The log file of nginx does not have the rotate function. If you do not process it, the log file will become larger and larger. Fortunately, we can write an nginx log cutting script to automatically cut the log file. 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. Nginx log automatic cut script by date is as follows # nginx log cut script # author: http://www.nginx.cn #! /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 the 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} 'and save the above script nginx_log.sh crontab to set the job 0 0 **** bash/usr/local/nginx /nginx_log.sh rename the nginx log to the date format at 00:00 every day, and generate a new log file.

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.