Nginx log file does not have the rotate function. If you do not process, log files will become larger and better, fortunately we can write an nginx log cutting script automatically cut log files.
The first step is to rename the log file, without worrying about renaming the log file and losing the log. When you do not reopen the original name of the log file, Nginx will still write to the file you renamed, Linux by the file descriptor rather than the file name location files.
The second step sends the USR1 signal to the Nginx main process.
After the Nginx main process receives the signal, it reads the log file name from the configuration file, re-opens the log file (named after the log name in the configuration file), and takes the user of the worker process as the owner of the log file.
After reopening the log file, the Nginx master process closes the log file with the same name and notifies the worker process to use the newly opened log file.
The worker process immediately opens a new log file and closes the log file with the name of the duplicate.
Then you can handle the old log files.
Nginx Log Automatic cutting script by date is as follows
#nginx日志切割脚本 #author:http://www.nginx.cn#!/bin/bash# Set the log file to store the directory Logs_path="/usr/local/nginx/logs/"#设置pid文件pid_path="/usr/local/nginx/nginx.pid"#重命名日志文件MV${logs_path}access.log ${logs_path}access_$ (Date-D"Yesterday"+"%y%m%d"). log# sends a signal to the Nginx main process to reopen the logKill-USR1 'Cat${pid_path} '
Save the above script nginx_log.sh
Crontab Setting up Jobs
0 0 * * * bash/usr/local/nginx/nginx_log.sh
In this way, the Nginx log is renamed to date format 0:0 every day and the new log file is rebuilt today.
Nginx Log Cutting script