操作系统: CentOS 8
nginx版本: 1.16.1
1、使用shell脚本分割
对已有日志进行分割
#!/bin/bash
#system:Centos 8 Minimal install
#purpose:自动分割nginx日志文件
#by:波仔
#url:yisca.cn
#Date:2020年1月2日
#set -x
function Find_Log () {
[ -s /etc/nginx/nginx.conf ] && Nginx_Conf=/etc/nginx/nginx.conf
[ -s /usr/local/nginx/conf/nginx.conf ] && Nginx_Conf=/usr/local/nginx/conf/nginx.conf
[ -s /usr/sbin/nginx ] && Nginx_Bin=/usr/sbin/nginx
[ -s /usr/local/nginx/sbin/nginx ] && Nginx_Bin=/usr/local/nginx/sbin/nginx
Access_Log=`grep access_log ${Nginx_Conf} |awk '{print $2}'`
mv ${Access_Log} ${Access_Log}.bak
}
###按天划分
function Cut_Nginx_log_Day () {
Find_Log
###筛选出所有已经记录的时间
Date=`cat ${Access_Log}.bak | awk -F "[" '{print $2}' |awk -F ":" '{print $1}' | uniq`
for Day in ${Date}
do
Arr=(`echo ${Day}|sed 's/\// /g'`)
grep "${Day}" ${Access_Log}.bak >> `echo "${Access_Log}-${Arr[2]}年${Arr[1]}月${Arr[0]}日"`
###重新打开nginx日志文件
${Nginx_Bin} -s reopen
done
rm -f ${Access_Log}.bak
}
###按小时划分
function Cut_Nginx_log_Hour () {
Find_Log
Date=`cat ${Access_Log}.bak | awk -F "[" '{print $2}' |awk -F "]" '{print $1}' | awk '{print $1}' | sed 's/:/\//'| awk -F ":" '{print $1}' |uniq`
for Hour in ${Date}
do
Arr=(`echo ${Hour}|sed 's/\// /g'`)
Log_Name=`echo "${Access_Log}-${Arr[2]}年${Arr[1]}月${Arr[0]}日${Arr[3]}时"`
Grep_Name=`echo "${Arr[0]}/${Arr[1]}/${Arr[2]}:${Arr[3]}"`
grep "${Grep_Name}" ${Access_Log}.bak >> ${Log_Name}
###重新打开nginx日志文件
${Nginx_Bin} -s reopen
done
rm -f ${Access_Log}.bak
}
###按分钟分割
function Cut_Nginx_log_Min () {
Find_Log
Date=`cat ${Access_Log}.bak | awk -F "[" '{print $2}' |awk -F "]" '{print $1}' | awk '{print $1}' | sed 's/:/\//'| sed 's/:/\//'| awk -F ":" '{print $1}' |uniq`
for Min in ${Date}
do
Arr=(`echo ${Min}|sed 's/\// /g'`)
Log_Name=`echo "${Access_Log}-${Arr[2]}年${Arr[1]}月${Arr[0]}日${Arr[3]}时${Arr[4]}分"`
Grep_Name=`echo "${Arr[0]}/${Arr[1]}/${Arr[2]}:${Arr[3]}:${Arr[4]}"`
grep "${Grep_Name}" ${Access_Log}.bak >> ${Log_Name}
###重新打开nginx日志文件
${Nginx_Bin} -s reopen
done
rm -f ${Access_Log}.bak
}
###按秒分割
function Cut_Nginx_log_Sec () {
Find_Log
Date=`cat ${Access_Log}.bak | awk -F "[" '{print $2}' |awk -F "]" '{print $1}' | awk '{print $1}' | sed 's/:/\//'| sed 's/:/\//'| sed 's/:/\//'| awk -F ":" '{print $1}' |uniq`
for Sec in ${Date}
do
Arr=(`echo ${Sec}|sed 's/\// /g'`)
Log_Name=`echo "${Access_Log}-${Arr[2]}年${Arr[1]}月${Arr[0]}日${Arr[3]}时${Arr[4]}分${Arr[5]}秒"`
Grep_Name=`echo "${Arr[0]}/${Arr[1]}/${Arr[2]}:${Arr[3]}:${Arr[4]}:${Arr[5]}"`
grep "${Grep_Name}" ${Access_Log}.bak >> ${Log_Name}
###重新打开nginx日志文件
${Nginx_Bin} -s reopen
done
rm -f ${Access_Log}.bak
}
Cut_Nginx_log_Day
###然后定期执行shell脚本进行分割即可
2、使用nginx配置文件分割
在nginx 配置文件的server 字段添加如下内容
###按天分割
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
}
###按秒分割
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})"){
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
set $minutes $5;
set $seconds $6;
}
根据需求更改配置文件即可,然后重新加载nginx配置文件即可
systemctl reload nginx
3、使用logrotate进行日志分割
logrotate命令 一般在CentOS中都自带有,如果没有使用yum install logrotate -y 安装即可。
用于对系统日志进行轮转、压缩和删除,也可以将日志发送到指定邮箱。使用logrotate指令,可让你轻松管理系统所产生的记录文件。每个记录文件都可被设置成每日,每周或每月处理,也能在文件太大时立即处理。您必须自行编辑,指定配置文件,预设的配置文件存放在/etc/logrotate.conf
文件中。
在/etc/logrotate.d 新建一个名为nginx 的配置文件,输入以下内容
/usr/local/nginx/log/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}