nginx日誌隨着時間而增加一直挺使人頭疼,其實咱們能夠向nginx主進程發送信號USR1
來生成新的日誌文件,而後將舊的日誌文件歸檔,下面是我用來切分日誌的腳本。nginx
[root@master ~]# cat /home/test/bin/nginx_reload_logfile.sh #!/bin/bash # log_path base_path='/usr/local/nginx-1.12.1/logs' # get year and month year_month=$(date -d yesterday +"%Y%m") # get yesterday day=$(date -d yesterday +"%d") # create backup directory mkdir -p $base_path/$year_month # copy logfile to backup mv $base_path/access.log $base_path/$year_month/access_$day.log # echo $base_path/$year_month/access_$day.log # reload logfile kill -USR1 `cat /usr/local/nginx-1.12.1/logs/nginx.pid`
crontab中添加定時任務,在天天23:59分執行切分日誌的腳本shell
# nginx access log backup 59 23 * * * sh /home/ysyf/bin/nginx_reload_logfile.sh
切分後的效果以下,固然你也能夠自定義你喜歡的路徑,修改一下腳本便可。bash
[root@master /usr/local/nginx-1.12.1/logs]#ll 總用量 490828 drwxr-xr-x 2 root root 4096 10月 1 23:59 201909 drwxr-xr-x 2 root root 4096 10月 31 23:59 201910 -rw-r--r-- 1 nobody root 2477169 11月 1 23:24 access.log -rw-r--r-- 1 nobody root 499617441 11月 1 23:24 error.log -rw-r--r-- 1 root root 6 12月 31 2018 nginx.pid [root@master /usr/local/nginx-1.12.1/logs]#cd 201910 [root@master /usr/local/nginx-1.12.1/logs/201910]#ll 總用量 64348 -rw-r--r-- 1 nobody root 956103 10月 2 23:58 access_01.log -rw-r--r-- 1 nobody root 955541 10月 3 23:53 access_02.log -rw-r--r-- 1 nobody root 1328787 10月 4 23:46 access_03.log -rw-r--r-- 1 nobody root 533591 10月 5 23:54 access_04.log -rw-r--r-- 1 nobody root 1048041 10月 6 23:59 access_05.log -rw-r--r-- 1 nobody root 402311 10月 7 23:56 access_06.log -rw-r--r-- 1 nobody root 3036879 10月 8 23:58 access_07.log -rw-r--r-- 1 nobody root 2848916 10月 9 23:48 access_08.log -rw-r--r-- 1 nobody root 2814881 10月 10 23:58 access_09.log -rw-r--r-- 1 nobody root 3424007 10月 11 23:51 access_10.log