Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/tladagio/article/details/88642755
A, yum install nginx
Second, the file path (/etc/nginx/nginx.conf)
1, the access log path: access_log /var/log/nginx/access.log main;
2, pid path: pid /run/nginx.pid
Third, script
1, create a script
[root@localhost ~]# pwd
/root
[root@localhost ~]# vim runlog.sh
#!/bin/bash
#日志文件所在路径
logpath=/var/log/nginx/access.log
#备份后的位置
basepath=/var/log/nginx/data
#备份后的文件名称(按年月日显示)
bak=$basepath/$(date -d yesterday +%Y%m%d%H%M).access.log
#移动备份文件
mv $logpath $bak
#新建空白日志文件
touch $logpath
#结束进程,通知nginx使用新的日志文件
kill -USR1 `cat /run/nginx.pid
2, grant execute permissions
[root@localhost ~]# chmod 777 runlog.sh
[root@localhost ~]# chown nginx:root runlog.sh
[root@localhost ~]# ll
-rwxrwxrwx. 1 nginx root 392 Mar 18 04:56 runlog.sh
3, create a directory after backup
[root@localhost nginx]# pwd
/var/log/nginx
[root@localhost nginx]# mkdir data
[root@localhost nginx]# chown nginx:root data
Fourth, create a scheduled task
[root@localhost ~]# crontab -e
#为了试验效果,每分钟执行一次
*/1 * * * * /bin/bash /root/runlog.sh
V. View Results