ningx access log cutting

By default, Nginx access logs are always appended to a file. This method is not conducive to the query of access records within a time period. The read and write resources of large files can be expensive. Therefore, the log is generally divided by day and the Linux plan is used. task to operate the task.

  1. Set up Linux scheduled tasks, the homepage is executed
    [root@iZ25gef96i2Z ~]# crontab -e
    

      Add to task list:

    55 23 * * * /root/cmd/split_log.sh
    

      

  2. Write the shell script split_log.sh to split the log, the content is:
    ## The directory where the Nginx log files are located
    LOGS_PATH=/home/wwwlogs
    ## Get yesterday's yyyy-MM-dd
    YESTERDAY=$(date  +"%Y%m%d" -d  "-24hour")
    ## move files
    mv ${LOGS_PATH}/yulinapp.com.log ${LOGS_PATH}/history/access_${YESTERDAY}.log
    ## Send the USR1 signal to the Nginx master process. USR1 signal is to reopen the log file
    kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
    

      Pay attention to the path of split_log.sh, the sample script is placed under /home/wwwlogs/history/.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324817049&siteId=291194637