nginx日志切割实现方法教程
首先在/usr/local/nginx/conf/vhost/中*.conf加入
access_log /data/httpd/www/isaru-log/www.isaru.com.log;
error_log /data/httpd/www/isaru-log/error.log crit;
(这个不明白可以自行百度)
日志文件如下,保留日志30天,脚本文件可以放到/data/httpd/www/isaru-log/目录下命名为iasru.sh(权限自行赋予)
crontab中加入
00 00 * * * /data/httpd/www/isaru-log/iasru.sh >/dev/null
isaru.sh脚本如下:
##by-小酱沫-https://jinjiajin.cn/783.html
#!/bin/bash
##Nginx logs rotate
rotate() {
LOGS_PATH=/data/httpd/www/isaru-log
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${LOGS_PATH}/isaru.log ${LOGS_PATH}/isaru_${YESTERDAY}.log
echo `pgrep nginx|head -n 1`>/usr/local/nginx/logs/nginx.pid
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
}
remove() {
LOGS_PATH=/data/httpd/www/isaru-log
DEL=$(date -d "-30 days" +%Y-%m-%d)
rm -rf ${LOGS_PATH}/isaru${DEL}.log
}
rotate
remove