1. 程式人生 > >nginx error.log定期清理

nginx error.log定期清理

遇到一個Linux伺服器磁碟報警,du -sh * |gre G進去一看是/etc/nginx/logs/error.log佔了將近300G, 又用tail -f error.log讓我大吃一驚,日誌顯示有兩個IP用飛快的速度一直往這個error.log裡面寫東西,具體原因不知道,但是伺服器要清理,於是就把nginx.conf裡面的錯誤日誌重新指向了黑洞。

vim nging.conf

error_log  /dev/null crit;

其實這樣是不可取的,應該找到源頭徹底解決問題。

或者用下面指令碼定時清理

#!/bin/bash
#切割nginx的日誌,然後定期刪除
source /etc/profile
log_path=/etc/nginx/logs
d=`date +%Y-%m-%d`
d90=`date -d'1 day ago' +%Y-%m-%d`   ##1天前
cd ${log_path} && cp error.log $log_path/backuplog/errorlog$d.log
gzip -f $log_path/backuplog/errorlog$d.log
echo > error.log
rm -rf $log_path/backuplog/errorlog${d90}.log.gz

crontab -e

59 23 * * * /bin/sh /etc/nginx/logs/nginx_error.log_clean.sh