Nginx日志切割
0点定时切割日志(安全审计一般需要至少保留半年的日志)
0 0 * * * /usr/bin/bash /app/sh/cut_nginx_log.sh
脚本:
#!/bin/bash # This script run at 00:00 # The Nginx logs path logs_path="/data/nginx/logs/" mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/ mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log mv ${logs_path}error.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/error_$(date -d "yesterday" +"%Y%m%d").log kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` gzip ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log gzip ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/error_$(date -d "yesterday" +"%Y%m%d").log