Nginx Log Poll cut

Source: Internet
Author: User

nginx Log poll cut

By default, Nginx will generate all the access logs into a specified access log file Access*.log, but in this way, a long time will lead to log a single file is very large, not conducive to the analysis and processing of logs, it is necessary to the Nginx log by day or by the hour, etc. Specific to see the specific online business, so that it is divided into different files to save. Here we are cutting logs by day.

1. Configure the log cut script as follows:

[Email protected] ~]# mkdir-p/server/scripts/

[Email protected] ~]# cd/server/scripts/

[Email protected] scripts]# vim cut_nginx_log.sh

#!/bin/bash

#---------------------------------------

#Author: Jeff

#date: 2016-06-07

#Function: Cut Nginx logs every day

#Version: 1.0

#---------------------------------------

#定义相关变量

Nginx_dir=/usr/local/nginx

Nginx_log_dir=${nginx_dir}/logs

Date= ' Date +%y%m%d '

date3ago= ' Date +%y%m%d-d ' 3 days ago '

Nginx_cmd=${nginx_dir}/sbin/nginx


#对日志进行切割

[-D ${nginx_log_dir}] && CD ${nginx_log_dir} | | Exit 1

For log_name in Access_www Access_blog Access_bbs

Do

/BIN/MV ${log_name}.log ${log_name}_$date.log

Done

${nginx_cmd}-S Reload


#对3天前日志文件进行打包压缩, and keep only 15 days of log

For log_name in Access_www Access_blog Access_bbs

Do

[-f ${log_name}_${date3ago}.log] && \

/BIN/TAR-ZCF ${log_name}_${date3ago}.tar.gz ${log_name}_${date3ago}.log && \

/bin/rm-f ${log_name}_${date3ago}.log

/bin/find ${nginx_log_dir}-name "${log_name}_*.tar.gz"-mtime 15-exec rm-f {} \;

Done

2. Save this script and add it to the server side of the scheduled task configuration, so that the script executes 0 o'clock in the morning every day, you can implement the log

The daily partition function is as follows:

[Email protected] scripts]# CRONTAB-E

#!/bin/bash

# cut Nginx log by every day

0 0 * * */bin/sh/server/script/cut_nginx_log.sh >/dev/null 2>&1

3. Final log cut

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/82/79/wKiom1dWZ5GAa5eRAACmF3357PQ816.png-wh_500x0-wm_3 -wmp_4-s_4094808342.png "title=" log. PNG "alt=" Wkiom1dwz5gaa5eraacmf3357pq816.png-wh_50 "/>


This article is from the "Jeff Blog" blog, so be sure to keep this source http://jeff1.blog.51cto.com/4814928/1786931

Nginx Log Poll cut

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.