행위

"Nginx 로그 일자별 기록하기"의 두 판 사이의 차이

라이언의 꿀팁백과

(새 문서: == 문제점 == 대부분의 일반적인 프로그램은 로그(log)<ref>다양한 IT 장비와 응용 소프트웨어(SW)에서 일어나는 일을 발생 시간과 사건 내역으로 기록하는 데이터</ref>를 남긴다. 오픈소스 웹서버인 nginx 또한 로그를 남기는데 기본적으로 해당 프로그램은 1개의 파일에 로그를 기록하기 때문에 관리에 어려움이 발생한다. == 해결방법 == 이러한 문제점을 해결하기 위해...)
 
 
(같은 사용자의 중간 판 2개는 보이지 않습니다)
27번째 줄: 27번째 줄:
}
}
</syntaxhighlight>
</syntaxhighlight>
== logrotate 설정 설명 ==
{| class="wikitable"
|+
!라인수
!옵션
!설명
|-
|2
|create 0644 nginx web
|로그 파일을 만들 때 permission 은 0644, user 및 group은 각각 nginx, web 으로 설정
|-
|3
|daily
|매일 logrotate 가 실행됨 (weekly, monthly, yearly 까지 총 4개 옵션 있음)
|-
|4
|missingok
|로그 파일이 없더라도 오류 처리를 하지 않음
|-
|5
|rotate 365
|로그 파일은 365개 까지 저장하고 나머지는 삭제함
|-
|6
|compress
|로그 파일을 압축함
|-
|7
|delaycompress
|다음 logrotate 시 때까지 로그파일 압축을 미룬다.
Delaycompress parameter would be useful for the application servers which requires writing to the logs continuously and delay compression for a particular amount of time. ([https://bobcares.com/blog/logrotate-delaycompress/ Link])
|-
|8
|notifempty
|로그 파일 크기가 0 이면
|-
|9
|dateformat .%Y-%m-%d.log
|로그 파일명에 .YYYY-MM-DD.log 파일을 항상 붙이도록 설정
|-
|10
|dateext
|로그 파일명에 YYYYMMDD 형식을 추가함(dateformat 옵션으로 형식 변경 가능)
|-
|11
|sharedscripts
|postrotate ~ endscript 내 명령이 한 번만 실행되도록 함
(이 명령어가 없으면 관리하는 로그 수만큼 해당 명령어가 실행됨)
|-
|12 ~ 16
|postrotate
if [ -f /app/nginx/run/nginx.pid ]; then
kill -USR1 `cat /app/nginx/run/nginx.pid`
fi
endscript
|만약 nginx process 가 실행중이면 해당 프로세스에 USR1 시그널을 보냄
nginx는 USR1 시그널을 수신하면 로그 파일을 다시 open 함 ([http://nginx.org/en/docs/control.html Link])
|}
== 주석 ==
[[분류:IT 전문가]]
[[분류:IT 전문가]]
[[분류:IT]]
[[분류:IT]]
[[분류:리눅스]]
[[분류:리눅스]]
[[분류:컴퓨터]]
[[분류:컴퓨터]]

2022년 2월 19일 (토) 10:01 기준 최신판

1 문제점[편집 | 원본 편집]

대부분의 일반적인 프로그램은 로그(log)[1]를 남긴다. 오픈소스 웹서버인 nginx 또한 로그를 남기는데 기본적으로 해당 프로그램은 1개의 파일에 로그를 기록하기 때문에 관리에 어려움이 발생한다.

2 해결방법[편집 | 원본 편집]

이러한 문제점을 해결하기 위해서는 로그를 의미 있는 단위로 나누어서 관리하는 것이다. 가장 일반적인 방법은 일자로 로그를 나누는 것인데, 이를 위해 여기서는 logrotate[2] 라는 프로그램을 사용하고자 한다.

3 logrotate 관련 설정[편집 | 원본 편집]

아래와 같이 /etc/logrotate.d/nginx 라는 파일을 생성한다.

/app/nginx/logs/*.log {
    create 0644 nginx web
    daily
    missingok
    rotate 365
    compress
    delaycompress
    notifempty
    dateformat .%Y-%m-%d.log
    dateext
    sharedscripts
    postrotate
        if [ -f /app/nginx/run/nginx.pid ]; then
            kill -USR1 `cat /app/nginx/run/nginx.pid`
        fi
    endscript
}

4 logrotate 설정 설명[편집 | 원본 편집]

라인수 옵션 설명
2 create 0644 nginx web 로그 파일을 만들 때 permission 은 0644, user 및 group은 각각 nginx, web 으로 설정
3 daily 매일 logrotate 가 실행됨 (weekly, monthly, yearly 까지 총 4개 옵션 있음)
4 missingok 로그 파일이 없더라도 오류 처리를 하지 않음
5 rotate 365 로그 파일은 365개 까지 저장하고 나머지는 삭제함
6 compress 로그 파일을 압축함
7 delaycompress 다음 logrotate 시 때까지 로그파일 압축을 미룬다.


Delaycompress parameter would be useful for the application servers which requires writing to the logs continuously and delay compression for a particular amount of time. (Link)

8 notifempty 로그 파일 크기가 0 이면
9 dateformat .%Y-%m-%d.log 로그 파일명에 .YYYY-MM-DD.log 파일을 항상 붙이도록 설정
10 dateext 로그 파일명에 YYYYMMDD 형식을 추가함(dateformat 옵션으로 형식 변경 가능)
11 sharedscripts postrotate ~ endscript 내 명령이 한 번만 실행되도록 함

(이 명령어가 없으면 관리하는 로그 수만큼 해당 명령어가 실행됨)

12 ~ 16 postrotate

if [ -f /app/nginx/run/nginx.pid ]; then

kill -USR1 `cat /app/nginx/run/nginx.pid`

fi

endscript

만약 nginx process 가 실행중이면 해당 프로세스에 USR1 시그널을 보냄

nginx는 USR1 시그널을 수신하면 로그 파일을 다시 open 함 (Link)

5 주석[편집 | 원본 편집]

  1. 다양한 IT 장비와 응용 소프트웨어(SW)에서 일어나는 일을 발생 시간과 사건 내역으로 기록하는 데이터
  2. 로그 관리 프로그램