tid=50612& 本帖最後由 IT_man 於 2016-11-3 21:24 編輯
" `2 }5 W# ^5 s8 I k6 O7 C4 i7 t, _0 z3 T
CentOS下nginx的log檔放在/var/log/nginx,但查看卻沒有被自動切割並壓縮.; B% F6 `+ F( j' A
找了google才發現原來缺少了/etc/logrotate.d/nginx檔,照理安裝nginx時應該會自動產生此檔,為何我的不會自動產生,可能是安裝時出了差錯.
. M2 h0 V' Q# ?+ D2 e於是乎我從別台server複製過來,搞定.
' d# m, c0 }" G6 v8 {0 l3 j
4 a$ Y; ]- d7 ]4 y3 R2 M7 m6 R0 Q* j
5 `) }) {, G/ _4 w6 A! i1 ~1 A x以下說明日誌管理服務logrotate運作原理:" Z9 U: s4 S" A. P4 p3 _4 O0 e# p: W
& k9 ~8 P# X0 j1 d x* G: A ?3 f2 _3 P7 g
因為Nginx在安裝時加入了一個logrotate任務: /etc/logrotate.d/nginx ) y4 A, A$ v4 x' e+ }" T% x! F
/var/log/nginx/*.log {
, s# s; C; Q( V0 H. P daily 0 y: {- Q/ Z1 K2 k$ H& r: W1 b
missingok
& i/ ]0 B# \' ]* ], i" Z rotate 52
; \" Y' \. S6 @9 a# ~" N; `7 Q compress 6 r. k5 s' J, t$ p5 d
delaycompress
4 g4 _8 Z/ U' \4 Z$ ]0 r notifempty
' ?: a9 y _. F create 640 nginx adm
b, g1 E+ u: f' L. R$ l sharedscripts * e. C9 f8 j* S
postrotate / e, _5 |- c: o' S) W
[ -f /var/run /nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid` ' U3 Y3 v4 Q) t0 ?. \) o, x
endscript 5 a2 z2 L2 ~, ]: ?5 M2 n4 u0 g
}
4 p" ]* H3 }3 q$ p( cLinux日誌管理服務logrotate會定時讀取該文件,根據設置按日誌大小或天數刪除/歸檔舊日誌,建立新日誌.
9 |* V; p+ d( c2 w% p( d+ j: |3 ]# Q: T9 n% t" d# C3 m7 W# {
3 P( J% k: }5 Z9 D: r
配置說明:
# H3 a: \, n6 ~/ y l+ I& j4 Fdaily:日誌文件每天進行滾動
$ a7 R$ [1 L) v- d) q2 }0 Gmissingok:如果找不到這個log檔案,就忽略過去 % F% r# e3 l D
rotate:保留最近52次滾動的日誌 % }3 p, E: c! R- S
compress:透過gzip壓縮轉儲以後的日誌 ( e$ y) \+ C6 o& n9 w
delaycompress:和compress一起使用時,轉儲的日誌文件到下一次轉儲時才壓縮 5 s; [3 D7 ~1 j$ f
notifempty如果是空文件的話,不轉儲
7 b, c4 Z: b7 P5 i1 J( Hcreate mode owner group:轉儲文件,使用指定的文件模式建立新的日誌文件
/ c) P0 q5 \ b7 @5 Zsharedscripts:運行postrotate腳本(該腳本作用為讓nginx重新生成日誌文件)
1 o. f2 V# b, I- g E; j; `postrotate/endscript:在轉儲以後需要執行的命令可以放入這個敘述對據裏,這兩個關鍵字必須單獨成行
9 h h8 a% c$ K
) E* L9 c8 v& f$ n6 Y8 P7 Ylogrotate預設被加入到cron的/etc/cron.daily中作為每日任務執行,配置定義在: ( x u8 s& _9 ]: X
/etc/cron.daily/logrotate
$ P2 s2 u( k3 X+ H3 A" Z/ c& p: R( o
要測試寫好的設定檔可以用以下命令:( i1 m# {6 S$ e1 P6 a e
sudo logrotate -vf /etc/logrotate.d/your-conf-file$ }- j. {7 Z$ Q; g
8 e# r$ M* @4 U* J/ p% U( D0 E, [2 J- x6 P5 v
0 m* m% U& m+ W, n
註:5 p9 m! [$ \6 |# h( c% y
參考: https://blog.toright.com/posts/4 ... AD%B8%E6%AA%94.html
* o: [# r, @! v# V+ R3 I% P
6 [, u" d# @& F, ]5 w. G, A |