이상하게, 서버가 느려지는 것 같은 느낌이 들어서,

nginx 서버 튜닝을 알아보다 보니, log에 관련된 이야기가 나왔다.

그래서, 어떻게 되어있나 살펴보니.. 헉..

80만 라인이 넘어가고 있었다..

 

아아..

 

어떻게 해야 하나 찾아보다가

logrotate를 알게 되었다.

 

일단 logrotate를 설치하자.

sudo apt-get install logrotate

 

그리고, /etc/logrotate.d/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
}

## 나는 로그를 서브도메인별로 따로따로 저장하게 설정이 되어있어서,
## 이 부분까지 설정해 주었다.
/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
}

각각이 뜻하는 바를 알아보면,

  • daily : 하루치를 따로따로 보관하자.
  • missingok : 로그 파일이 없어도 에러를 내지말자.
  • rotate 52 : 로그 파일을 52개까지만 보관하자. 위에 daily로 설정되어 있으니, 52일치만 보관하고, 53일째 되는 로그는 삭제.
  • compress : 로그를 압축해서 보관하자.
  • notifempty: 로그파일이 비어있으면, 로테이트 하지 않습니다.
  • create 640 nginx adm : 로그파일은 새로 생성시에, 파일 권한을 640으로 생성하고, 소유자/그룹은 nginx로 하자.
  • postrotate : 로테이트 작업이 끝난후에, 실행할 스크립트를 입력합니다.

 

위 설정 후, 정상적으로 설정되었는지, 테스트하기 위해서 아래의 명령어를 실행합니다.

logrotate -d -f /etc/logrotate.d/nginx

 

정상적으로 설정되었다면, /etc/cron.daily/logrotate 파일로 인해서, 매일 자동 실행됩니다.

 

혹시, /etc/cron.daily/logrotate 파일이 없다면,

아래와 같이 만들어 주자.

 

#!/bin/sh

# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
    [ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

 

 

참고 : https://www.lesstif.com/system-admin/nginx-log-rotate-logrotate-75956229.html

+ Recent posts