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