Nginx logs cannot be split by configuration. to split, you must write a simple script to implement this function. I used to use the cp command for the past, for example: #/bin/bashnginxLog = "/usr/local/nginx/logs/access. log "logBakFile ="/usr/local/nginx/logs/"$ (date" + % F ") /"bak _" $ (date "+ % H-% M-% S"
Nginx logs cannot be split by configuration. to split, you must write a simple script to implement this function. I used to use the cp command for the past, for example:
#/bin/bashnginxLog="/usr/local/nginx/logs/access.log"logBakFile="/usr/local/nginx/logs/"$(date "+%F")/"bak_"$(date "+%H-%M-%S")".log"if [ ! -d ${logBakFile%/*} ]then mkdir -p ${logBakFile%/*}ficp $nginxLog $logBakFile 2>&1 >/dev/nullcat /dev/null > $nginxLog
The principle is very simple. you can use the cp command to copy the current log and clear the current log after the copy. Of course, if the nginx concurrency is large, it may cause log loss.
Today, I accidentally saw another log splitting method, mainly through the mv command. I think it may be better than my previous implementation. I would like to record it here. The preceding script can be modified:
#/bin/bashnginxLog="/usr/local/nginx/logs/access.log"logBakFile="/usr/local/nginx/logs/"$(date "+%F")/"bak_"$(date "+%H-%M-%S")".log"if [ ! -d ${logBakFile%/*} ]then mkdir -p ${logBakFile%/*}fimv $nginxLog $logBakFile 2>&1 >/dev/nullkill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
Back up logs with the mv command, and then use kill-USR1 to re-open the log file with nginx. Although logs may be lost in this way, the mv command execution must be cp fast, so it is quite safe.
You can add the preceding script to crontab to split logs by day, hour, and other time rules.