Online nginx Access Log cutting script

Source: Internet
Author: User


1. Description

With the increase of time, Nginx's access log will be more and more large, is the newly deployed online Zabbix monitoring website running for about more than 10 days to generate access logs up to 213M.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8B/49/wKioL1hJDW2AYqn8AAA0SQ0ucqY181.png "title=" 1.png " alt= "Wkiol1hjdw2ayqn8aaa0sq0ucqy181.png"/>


Therefore, log splitting is required, as follows:

1, daily log to generate a separate file

2. Keep the 30-day access log


2. Writing scripts

vim/usr/local/nginx/logs/nginx_log_rotate.sh

#! /bin/bash

Logs_path= "/usr/local/nginx/logs/"

Log_name= "Access.log"

Pid_path= "/usr/local/nginx/logs/nginx.pid"

# paths stored in the log file set

[-d/usr/local/nginx/logs/access_log] | | Mkdir/usr/local/nginx/logs/access_log

Access_logs_path= "/usr/local/nginx/logs/access_log/"

# Move Log

MV ${logs_path}${log_name} ${access_logs_path}$ (date--date= "Yesterday" + "%y-%m-%d") _${log_name}

# generate a new Access.log file

KILL-USR1 ' Cat ${pid_path} '

# Delete logs from one months ago

CD ${access_logs_path}

Find. -ctime +30-name "*access.log" |xargs rm-f

Exit 0

Scripts Increase executable Permissions

chmod a+x/usr/local/nginx/logs/nginx_log_rotate.sh

3. Join the Scheduled Task

Run the script every morning

Crontab-e

XX * * * */usr/local/nginx/logs/nginx_log_rotate.sh

Restart Crond Service

Service Crond Restart



Running the script generates the access_log folder, which is all the logs up to today

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/8B/49/wKioL1hJEAqSE7H6AABooLD1hZY944.png "title=" Qq20161208154634.png "alt=" Wkiol1hjeaqse7h6aaboold1hzy944.png "/>





This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1880769

Online nginx Access Log cutting script

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.