Nginx 使用 logrotate 進行日志滾動


Nginx 日志滾動(官方)

向 Nginx 主進程發送 USR1 信號。

USR1 信號量被 Nginx 自定義了,為重新打開日志;當 kill 命令發送 USR1時,nginx 會重新打開日志文件,並重新創建進程。

# nginx 官方提供的日志滾動方式
$ mv access.log access.log.0
$ kill -USR1 `cat master.nginx.pid`
$ sleep 1
$ gzip access.log.0    # do something with access.log.0

logrotate 管理 Nginx 日志

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

logrotate 是一個日志文件管理工具。用於分割日志,刪除舊的日志,並創建新的日志文件,起到日志滾動的作用。

logrotate 是基於 linux 的 CRON 來運行的,其腳本是 /etc/cron.daily/logrotate

安裝 logrotate

Linux 一般會默認安裝logrotate,它默認的配置文件在:

# 配置文件
$ cat /etc/logrotate.conf | grep -v '^#' | grep -v '^$'
weekly
rotate 4
create
dateext
include /etc/logrotate.d
/var/log/wtmp {
    monthly
    create 0664 root utmp
	minsize 1M
    rotate 1
}
/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# logrotate 配置文件目錄,在 /etc/logrotate.conf 中會引用該目錄下的所有文件
$ ls -lt /etc/logrotate.d/

安裝 logrotate:

$ yum install logrotate

配置 logrotate

# nginx logratate 配置文件
$ vi /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
    # 指定轉儲周期為每天
    daily
    # 使用當期日期作為命名格式
    dateext
    # 如果日志丟失,不報錯繼續滾動下一個日志
    missingok
    # 保留 31 個備份
    rotate 31
    # 不壓縮
    nocompress
    # 整個日志組運行一次的腳本
    sharedscripts
    # 轉儲以后需要執行的命令
    postrotate
        # 重新打開日志文件
        [ ! -f /usr/local/nginx/nginx.pid ] || kill -USR1 `cat /usr/local/nginx/nginx.pid`
    endscript
}

配置文件參數說明:

參數名稱 說明
daily 指定轉儲周期為每天
weekly 指定轉儲周期為每周
monthly 指定轉儲周期為每月
dateext 使用當期日期作為命名格式,如:access.log-20201121
dateformat .%s 配合dateext使用,緊跟在下一行出現,定義文件切割后的文件名,必須配合dateext使用,只支持 %Y %m %d %s 這四個參數
compress 通過gzip壓縮轉儲以后的日志
nocompress 不壓縮
copytruncate 用於還在打開中的日志文件,把當前日志備份並截斷
nocopytruncate 備份日志文件但是不截斷
create mode owner group 轉儲文件,使用指定的文件模式創建新的日志文件
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用時,轉儲的日志文件到下一次轉儲時才壓縮
nodelaycompress 覆蓋 delaycompress 選項,轉儲同時壓縮。
errors address 專儲時的錯誤信息發送到指定的Email 地址
ifempty 即使是空文件也轉儲,這個是 logrotate 的缺省選項。
missingok 如果日志丟失,不報錯繼續滾動下一個日志
notifempty 如果是空文件的話,不轉儲
mail address 把轉儲的日志文件發送到指定的E-mail 地址
nomail 轉儲時不發送日志文件
olddir directory 轉儲后的日志文件放入指定的目錄,必須和當前日志文件在同一個文件系統
noolddir 轉儲后的日志文件和當前日志文件放在同一個目錄下
sharedscripts 運行postrotate腳本,作用是在所有日志都輪轉后統一執行一次腳本。如果沒有配置這個,那么每個日志輪轉后都會執行一次腳本
prerotate/endscript 在轉儲以前需要執行的命令可以放入這個對,這兩個關鍵字必須單獨成行
postrotate/endscript 在轉儲以后需要執行的命令可以放入這個對,這兩個關鍵字必須單獨成行
rotate count 指定日志文件刪除之前轉儲的次數,0 指沒有備份,5 指保留5 個備份
size log-size 當日志文件到達指定的大小時才轉儲,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem),當日志文件 >= log-size 的時候就轉儲

logrotate 命令參數

logrotate [OPTION...] <configfile>
-d, --debug :debug模式,測試配置文件是否有錯誤。
-f, --force :強制轉儲文件。
-m, --mail=command :壓縮日志后,發送日志到指定郵箱。
-s, --state=statefile :使用指定的狀態文件。
-v, --verbose :顯示轉儲過程。

手動執行 logrotate

# '-d' 調試模式(不切分日志文件),並輸出詳細處理過程日志
$ logrotate -d -f /etc/logrotate.d/nginx

# '-f' 強制切分日志,'-v' 輸出詳細信息
$ logrotate -vf /etc/logrotate.d/nginx
reading config file nginx
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /usr/local/nginx/logs/*.log  forced from command line (100 rotations)
empty log files are rotated, old logs are removed
considering log /usr/local/nginx/logs/access.log
  log needs rotating
considering log /usr/local/nginx/logs/error.log
  log needs rotating
rotating log /usr/local/nginx/logs/access.log, log->rotateCount is 100
dateext suffix '-20201121'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
rotating log /usr/local/nginx/logs/error.log, log->rotateCount is 100
dateext suffix '-20201121'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /usr/local/nginx/logs/access.log to /usr/local/nginx/logs/access.log-20201121
renaming /usr/local/nginx/logs/error.log to /usr/local/nginx/logs/error.log-20201121
running postrotate script

# 切分后的日志文件
$ ls -lt /usr/local/nginx/logs
總用量 0
-rw-r--r-- 1 nginx root 0 11月 21 18:58 access.log
-rw-r--r-- 1 nginx root 0 11月 21 18:57 access.log-20201121
-rw-r--r-- 1 nginx root 0 11月 21 18:58 error.log
-rw-r--r-- 1 nginx root 0 11月 21 18:57 error.log-20201121
-rw-r--r-- 1 nginx root 0 11月 21 18:58 images.log
-rw-r--r-- 1 nginx root 0 11月 21 18:57 images.log-20201121

微信公眾號:daodaotest


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM