Nginx 日志滾動(官方)
向 Nginx 主進(jìn)程發(fā)送 USR1
信號。
USR1
信號量被 Nginx 自定義了钝侠,為重新打開日志茅糜;當(dāng) kill 命令發(fā)送 USR1
時,nginx 會重新打開日志文件片习,并重新創(chuàng)建進(jìn)程。
# 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
是一個日志文件管理工具蹬叭。用于分割日志藕咏,刪除舊的日志,并創(chuàng)建新的日志文件秽五,起到日志滾動的作用孽查。
logrotate
是基于 linux 的 CRON 來運(yùn)行的,其腳本是 /etc/cron.daily/logrotate
坦喘。
安裝 logrotate
Linux 一般會默認(rèn)安裝logrotate
盲再,它默認(rèn)的配置文件在:
# 配置文件
$ 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 {
# 指定轉(zhuǎn)儲周期為每天
daily
# 使用當(dāng)期日期作為命名格式
dateext
# 如果日志丟失瓣铣,不報錯繼續(xù)滾動下一個日志
missingok
# 保留 31 個備份
rotate 31
# 不壓縮
nocompress
# 整個日志組運(yùn)行一次的腳本
sharedscripts
# 轉(zhuǎn)儲以后需要執(zhí)行的命令
postrotate
# 重新打開日志文件
[ ! -f /usr/local/nginx/nginx.pid ] || kill -USR1 `cat /usr/local/nginx/nginx.pid`
endscript
}
配置文件參數(shù)說明:
參數(shù)名稱 | 說明 |
---|---|
daily | 指定轉(zhuǎn)儲周期為每天 |
weekly | 指定轉(zhuǎn)儲周期為每周 |
monthly | 指定轉(zhuǎn)儲周期為每月 |
dateext | 使用當(dāng)期日期作為命名格式答朋,如:access.log-20201121 |
dateformat .%s | 配合dateext使用,緊跟在下一行出現(xiàn)棠笑,定義文件切割后的文件名梦碗,必須配合dateext使用,只支持 %Y %m %d %s 這四個參數(shù) |
compress | 通過gzip壓縮轉(zhuǎn)儲以后的日志 |
nocompress | 不壓縮 |
copytruncate | 用于還在打開中的日志文件,把當(dāng)前日志備份并截斷 |
nocopytruncate | 備份日志文件但是不截斷 |
create mode owner group | 轉(zhuǎn)儲文件洪规,使用指定的文件模式創(chuàng)建新的日志文件 |
nocreate | 不建立新的日志文件 |
delaycompress 和 compress | 一起使用時印屁,轉(zhuǎn)儲的日志文件到下一次轉(zhuǎn)儲時才壓縮 |
nodelaycompress | 覆蓋 delaycompress 選項,轉(zhuǎn)儲同時壓縮斩例。 |
errors address | 專儲時的錯誤信息發(fā)送到指定的Email 地址 |
ifempty | 即使是空文件也轉(zhuǎn)儲雄人,這個是 logrotate 的缺省選項。 |
missingok | 如果日志丟失念赶,不報錯繼續(xù)滾動下一個日志 |
notifempty | 如果是空文件的話础钠,不轉(zhuǎn)儲 |
mail address | 把轉(zhuǎn)儲的日志文件發(fā)送到指定的E-mail 地址 |
nomail | 轉(zhuǎn)儲時不發(fā)送日志文件 |
olddir directory | 轉(zhuǎn)儲后的日志文件放入指定的目錄,必須和當(dāng)前日志文件在同一個文件系統(tǒng) |
noolddir | 轉(zhuǎn)儲后的日志文件和當(dāng)前日志文件放在同一個目錄下 |
sharedscripts | 運(yùn)行postrotate腳本晶乔,作用是在所有日志都輪轉(zhuǎn)后統(tǒng)一執(zhí)行一次腳本珍坊。如果沒有配置這個,那么每個日志輪轉(zhuǎn)后都會執(zhí)行一次腳本 |
prerotate/endscript | 在轉(zhuǎn)儲以前需要執(zhí)行的命令可以放入這個對正罢,這兩個關(guān)鍵字必須單獨(dú)成行 |
postrotate/endscript | 在轉(zhuǎn)儲以后需要執(zhí)行的命令可以放入這個對,這兩個關(guān)鍵字必須單獨(dú)成行 |
rotate count | 指定日志文件刪除之前轉(zhuǎn)儲的次數(shù)驻民,0 指沒有備份翻具,5 指保留5 個備份 |
size log-size | 當(dāng)日志文件到達(dá)指定的大小時才轉(zhuǎn)儲,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem)回还,當(dāng)日志文件 >= log-size 的時候就轉(zhuǎn)儲 |
logrotate 命令參數(shù)
logrotate [OPTION...] <configfile>
-d, --debug :debug模式裆泳,測試配置文件是否有錯誤。
-f, --force :強(qiáng)制轉(zhuǎn)儲文件柠硕。
-m, --mail=command :壓縮日志后工禾,發(fā)送日志到指定郵箱。
-s, --state=statefile :使用指定的狀態(tài)文件蝗柔。
-v, --verbose :顯示轉(zhuǎn)儲過程闻葵。
手動執(zhí)行 logrotate
# '-d' 調(diào)試模式(不切分日志文件),并輸出詳細(xì)處理過程日志
$ logrotate -d -f /etc/logrotate.d/nginx
# '-f' 強(qiáng)制切分日志癣丧,'-v' 輸出詳細(xì)信息
$ 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