Nginx log segmentation

Source: Internet
Author: User
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.


Related Article

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.