nginx日誌輪循小記

目錄

1. 概述

Nginx 是一個非常輕量的 Web 服務器,體積小、性能高、速度快等諸多優點。但不足的是也存在缺點,比如其產生的訪問日誌文件一直就是一個,如果是yum方式安裝的nginx,系統默認會自動通過logrotate這個日誌管理軟件,按天進行分割。而如果是源碼編譯方式安裝nginx其日誌不會自動地進行切割,如果訪問量很大的話,將導致日誌文件容量非常大,不便於管理。當然了,我們也不希望看到這麼龐大的一個訪問日誌文件,那需要手動對這個文件進行切割。

2. 切割方式介紹

  • 使用logrotate工具+系統自帶的crontab命令
    logrotate是什麼呢?它是一個linux系統日誌的管理工具。它可以切割、壓縮等其他軟件的日誌文件軟件。
  • 編寫Shell 腳本+系統自帶的crontab 命令

3. 使用logrotate工具+系統自帶的crontab命令

3.1 安裝logrotate工具

[yuki@myhost ~]$ sudo yum install -y logrotate

#查看是否安裝成功
[yuki@myhost ~]$ sudo rpm -qa logrotate
logrotate-3.8.6-17.el7.x86_64

3.2 查看logrotate的配置文件

[yuki@myhost ~]$ sudo rpm -ql logrotate
/etc/cron.daily/logrotate
/etc/logrotate.conf                       		#####
/etc/logrotate.d							 	#####
/etc/rwtab.d/logrotate
/usr/sbin/logrotate
/usr/share/doc/logrotate-3.8.6
/usr/share/doc/logrotate-3.8.6/CHANGES
/usr/share/doc/logrotate-3.8.6/COPYING
/usr/share/man/man5/logrotate.conf.5.gz
/usr/share/man/man8/logrotate.8.gz
/var/lib/logrotate
/var/lib/logrotate/logrotate.status

#logrotate的配置文件是/etc/logrotate.conf
#而/etc/logrotate.d/是用於存儲其他配置文件的目錄。該目錄裏的所有文件都會被主動的讀入 /etc/logrotate.conf中執行。

3.3 logrotate配置文件詳解

logrotate配置選項相對來說比較少,爲了切合本篇文章,在此我們以切割nginx的配置文件爲例,如下:

[yuki@myhost ~]$ cat /etc/logrotate.d/nginx
/home/nginx/logs/*.log{
	daily
	rotate 30
	dateext
	nocompress
	copytruncate  
	notifempty
	create 640 nginx adm
	missingok
	postrotate
    [ -f /home/nginx/logs/nginx.pid ] && /bin/kill -HUP `cat /home/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true
	endscript
}

在該配置文件中,每個參數作用如下:

  • /home/nginx/logs/ 爲nginx日誌的存儲目錄,可以根據實際情況進行修改。
  • daily:日誌文件將按天輪循;
  • weekly:日誌文件將按周輪循;
  • monthly:日誌文件將按月輪循。
  • missingok:在日誌輪循期間,任何錯誤將被忽略,例如“文件無法找到”之類的錯誤。
  • rotate 30:一次存儲30個日誌文件。對於第31個日誌文件,時間最久的那個日誌文件將被刪除。
  • dateext:定義日誌文件後綴是日期格式,也就是切割後文件是:xxx.log-20160402.gz這樣的格式。如果該參數被註釋掉,切割出來是按數字遞增,即前面說的 xxx.log-1這種格式。
  • compress:在輪循任務完成後,已輪循的歸檔將使用gzip進行壓縮。
  • delaycompress:總是與compress選項一起用,delaycompress選項指示logrotate不要將最近的歸檔壓縮,壓縮將在下一次輪循週期進行。這在你或任何軟件仍然需要讀取最新歸檔時很有用。
  • notifempty:如果是空文件的話,不進行轉儲。
  • create 640 nginx adm:以指定的權限和用戶屬性,創建全新的日誌文件,同時logrotate也會重命名原始日誌文件。
  • postrotate/endscript:在所有其它指令完成後,postrotate和endscript裏面指定的命令將被執行。在這種情況下,rsyslogd進程將立即再次讀取其配置並繼續運行。注意:這兩個關鍵字必須單獨成行。

3.4 查看logrotate默認執行時間

[yuki@myhost ~]$ sudo cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22    ###這個參數就是配置logrotate切割的時間點(默認凌晨3點22分)###

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

如果你想在指定時間點,讓logrotate切割日誌的話,可以修改此配置文件的START_HOURS_RANGE參數。但是經過多次實驗,我發現logrotate沒有按照我指定的時間進行切割日誌。所以,最後我將切割時間直接寫入/etc/crontab裏面!!!

3.5 編寫定時任務

[yuki@myhost ~]$ sudo cat /etc/crontab
59 23 * * * root /usr/sbin/logrotate  -f  /etc/logrotate.d/nginx

4. 編寫Shell 腳本+系統自帶的crontab 命令

4.1 編寫Shell 腳本

[yuki@myhost script ]$ cat /home/script/cut_del_nginx_logs.sh 
#!/bin/sh
source /etc/profile

#define variables

logs_path="/home/nginx/logs"
yesterday_date="`date -d 'yesterday' +%Y-%m-%d`"
today_date="`date +%Y-%m-%d`"
special_day_date="`date -d '60 day ago' +%Y-%m-%d`"

#logrotate by day

mv ${logs_path}/access.log   ${logs_path}/access.${yesterday_date}.log

#向nginx主進程發送USR1信號,重新打開日誌文件,否則會繼續往mv後的文件寫數據的。
#原因在於:linux系統中,內核是根據文件描述符來找文件的。如果不這樣操作導致日誌切割失敗。

kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'`

#刪除60天前的日誌

if [ -d ${logs_path}/access.${special_day_date}.log ];
        then
                rm -rf ${logs_path}/access.${special_day_date}.log
        else
                echo "$special_day_date這天沒有日誌"

fi

該shell腳本有兩個功能,第一個是切割nginx日誌,第二個是刪除60天前的nginx日誌。
在切割nginx日誌的功能中,我們要注意該shell腳本命名切割的日誌是以前一天的時間命名該日誌文件的。
所以我們在把該shell腳本放在crontab中執行時,建議在每天的0點0分執行。如下:

4.2 編寫定時任務

[yuki@myhost script ]$ sudo cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

00 00 * * * root /home/script/cut_del_nginx_logs.sh 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章