nginx 로그 절단 (logrotate)

9911 단어 ubuntuCentOSnginx
참고:https://www.cnblogs.com/kevingrace/p/6307298.html
일 지 를 자 르 는 것 은 내 가 모 르 기 전에 항상 그것 이 매우 강하 다 고 생각 했다. 사실은 이 물건 은 매우 간단 하 다. 위 에 쓴 글 을 열심히 보면 하루 에 볼 수 있 는 차이 가 많 지 않 고 모두 간단 한 것 이 니 이름 에 놀 라 지 마라!
1. logrotate 로그 절단 은 Liux 자체 테이프 입 니 다.
2. logrotate 설정 파일 은 주 설정 과 하위 설정 으로 나 뉜 다.
주 설정: / etc / logrotate. conf
하위 설정: / etc / logrotate. d / 아래 파일
3. logrotate 는 CRON 을 기반 으로 실 행 됩 니 다. 스 크 립 트 는 / etc / cron. daily / logrotate 입 니 다.
[root@localhost ~]# cat /etc/cron.daily/logrotate 
#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

실제 실행 시 설정 파일 / etc / logrotate. conf 를 호출 합 니 다.
4. logrotate 는 CRON 을 기반 으로 실행 되 기 때문에 실행 시간 은 CRON 에서 제어 합 니 다.
ubuntu 보기 / etc / crontab
centos 보기 / etc / anacrontab
root@qqq:/etc/logrotate.d# cat /etc/crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user	command
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

맞습니다. run - parts 는 디 렉 터 리 에 있 는 모든 스 크 립 트 나 프로그램 을 실행 합 니 다. -- report 의 기능 은 -- verbose 기능 과 유사 하여 스스로 깨 달 습 니 다.
맞습니다. logrotate 스 크 립 트 는 / etc / cron. daily / 디 렉 터 리 아래 스 크 립 트 입 니 다. CRON 은 매일 06: 25 에 / etc / cron. daily / 디 렉 터 리 아래 의 모든 스 크 립 트 를 실행 합 니 다.
5. logrotate 명령, 아래 복사 붙 여 넣 기:
logrotate    :
logrotate [OPTION...] 
-d, --debug :debug  ,           。
-f, --force :      。
-m, --mail=command :     ,         。
-s, --state=statefile :         。
-v, --verbose :      。

6. logrotate 설정 파일 의 일반적인 매개 변 수 를 복사 하여 붙 여 넣 습 니 다.
compress                                     gzip          
nocompress                                  gzip    
copytruncate                                          ,          ;          ,             ,           。
nocopytruncate                                      
create mode owner group                          , create 0777 nobody nobody
nocreate                                             
delaycompress                            compress      ,                 
nodelaycompress                           delaycompress   ,      。
missingok                                       ,            
errors address                                         Email   
ifempty                                                  ,   logrotate     。
notifempty                                       ,     
mail address                                           E-mail   
nomail                                               
olddir directory                                        ,                 
noolddir                                                          
sharedscripts                             postrotate  ,                    。        ,                 
prerotate                                  logrotate           ,            ;      
postrotate                                logrotate           ,       (kill -HUP)     !      
daily                                                
weekly                                             
monthly                                           
rotate count                                           ,0      ,5    5    
dateext                                              
dateformat .%s                         dateext  ,        ,           ,    dateext  ,    %Y %m %d %s      
size( minsize) log-size                            ,log-size   bytes(  ) KB (sizek) MB(sizem).
      >= log-size       。        :(              )
size = 5   size 5 (>= 5       )
size = 100k   size 100k
size = 100M   size 100M

7. 생산 환경의 몇 가지 프로필 을 첨부 합 니 다. 제 가 쓴 것 이 아니 라 좋 고 나 쁨 은 따로 계산 합 니 다.
nginx 의 이것 은 기본 일 수 있 습 니 다. 잘 모 르 겠 습 니 다.
root@web1:/etc/logrotate.d# cat nginx
/var/log/nginx/*.log {
	daily
	missingok
	rotate 14
	compress
	delaycompress
	notifempty
	create 0640 www-data adm
	sharedscripts
	prerotate
		if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
			run-parts /etc/logrotate.d/httpd-prerotate; \
		fi \
	endscript
	postrotate
		invoke-rc.d nginx rotate >/dev/null 2>&1
	endscript
}

zabbix 이거.
root@web1:/etc/logrotate.d# cat zabbix-agent 
/var/log/zabbix/zabbix_agentd.log {
    weekly
    rotate 12
    compress
    delaycompress
    missingok
    notifempty
    create 0640 zabbix zabbix
}

php 이 느 린 조회 와 오류 로그
root@web1:/etc/logrotate.d# cat php7.0-fpm 
/var/log/php7.0-fpm.log {
	rotate 12
	weekly
	missingok
	notifempty
	compress
	delaycompress
	postrotate
		/usr/lib/php/php7.0-fpm-reopenlogs
	endscript
}

/var/log/php/*.log {
        rotate 12
        daily
        missingok
        notifempty
        compress
        delaycompress 
}

8. 저 는 centos 7 에서 테스트 를 했 습 니 다. 방금 설치 한 시스템 은 이 디 렉 터 리 아래 에 아래 몇 개의 프로필 만 있 습 니 다.
[root@uscwifi logrotate.d]# ls
bootlog  chrony  syslog  wpa_supplicant  yum

내 가 yum 에 nginx 를 설치 한 후에 nginx 설정 파일 이 하나 더 생 겼 다 는 것 을 알 게 되 었 습 니 다. 그래서 이 물건 은 자주 사용 하지 않 을 것 입 니 다. 그 자체 의 logrotate 는 Liux 가 가지 고 있 는 것 입 니 다. 그리고 소프트웨어 업 체 는 로그 절단 설정 파일 도 제공 합 니 다. 기본적으로 간단하게 수정 하면 됩 니 다.
[root@uscwifi yum.repos.d]# yum install nginx
     :fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
        
-->       
--->     nginx.x86_64.1.1.15.5-1.el7_4.ngx      
-->         

      

=============================================================================================================
 Package                                                                                             
=============================================================================================================
    :
 nginx                x86_64                1:1.15.5-1.el7_4.ngx                  nginx                759 k

    
=============================================================================================================
    1    

    :759 k
    :2.7 M
Is this ok [y/d/N]: y
Downloading packages:
nginx-1.15.5-1.el7_4.ngx.x86_64.rpm                                                   | 759 kB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
          : 1:nginx-1.15.5-1.el7_4.ngx.x86_64                                                        1/1 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
           : 1:nginx-1.15.5-1.el7_4.ngx.x86_64                                                        1/1 

   :
  nginx.x86_64 1:1.15.5-1.el7_4.ngx                                                                          

  !
[root@uscwifi logrotate.d]# ls
bootlog  chrony  nginx  syslog  wpa_supplicant  yum

이 nginx 내용 보기
[root@uscwifi logrotate.d]# cat nginx 
/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}
[root@uscwifi logrotate.d]# cat nginx 
/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

이 logtotate 를 배 우 는 데 거의 하루 가 걸 렸 습 니 다. 한 번 에 다 배 울 수 없습니다!
맨손으로 때 려!
 
9. 로그 절단 을 수행 하 는 시간 이 있 습 니 다. 앞에서 말 했 듯 이 logrotate 는 cron 에 의 해 제어 되 고 logrotate 의 스 크 립 트 는 Ubuntu 에서 run - parts / etc / cron. daily 작업 의 실행 시간 을 수정 하면 됩 니 다.
매일 아침 6 시 25 분
root@web1:/etc/logrotate.d# cat /etc/crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user	command
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

좋은 웹페이지 즐겨찾기