How to configure nginx log scheduled backup and deletion

WBOY
Release: 2023-06-03 10:59:17
forward
1496 people have browsed it

一旦开启了nginx日志功能,每天nginx都会生成一定大小的日志文件,如果系统稳定运行,没有任何问题,那么日志基本上不会去查看。但这些日志如不及时清理,日渐积累,对服务器的磁盘空间占用也将是比较恐怖的。为了解决这个问题,利用shell脚本对nginx日志文件定时备份和删除,只保留一段时间。

图1:

How to configure nginx log scheduled backup and deletion

#!/bin/bash
#auth:lzq
#desc:把当前日志按日期备份,重新生成第二天的日志文件
#date:2016-09-18

date=`date +%y%m%d`
nginx_pid=`cat /var/run/nginx.pid`
#如果当前nginx没有运行就退出
if [ "$?" != 0 ]
then
    exit 1;
fi

#nginx 日志所在的路径
log_path='/usr/local/nginx/logs/'
log_name='access.log'
mv ${log_path}${log_name} ${log_path}${log_name}$date

#删除7天前旧的备份文件
function deloldbak()
{
    olddate=`date +"%y%m%d" -d "-$1 day"`
    if [ -e "${log_path}${log_name}$olddate" ]
    then
        rm -f ${log_path}${log_name}$olddate
        echo "${log_path}${log_name}$olddate del ok"
    fi
}

#重载nginx配置,重新生成nginx日志文件
kill -usr1 $nginx_pid

if [ "$?" == 0 ]
then
    deloldbak 7
    exit 0;
fi
Copy after login

The above is the detailed content of How to configure nginx log scheduled backup and deletion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template