DevOps/nginx

[nginx] 서비스 점검공지 올리는 방법

bandal-gom 2022. 10. 5. 13:53
2019-03-04-nginx-점검-페이지.md 라는 이름으로 github.io 블로그에 올렸던 내용을 가져온 내용입니다.

 

nginx 에서 간단하게 503 에러코드를 사용하여 점검페이지를 띄워보자

서비스 점검 공지

서비스 점검 공지 페이지가 사용되는 경우가 종종 있음.

  • 장애
  • 무중단이 불가능한 빅 업데이트
    내가 속한 팀에서는 주로 위와 같은 사유로 점검페이지를 사용함.

Nginx 를 L4 뒤, application 앞에 두는 구조이기 때문에 점검 페이지를 nginx 쪽에서 관리하고있음.
실제 점검 페이지는 html 파일로, 마크업/디자인 팀에서 전달받은 파일을 nginx 서버에 업로드 하여 사용함.

nginx 설정

nginx에서 점검 페이지를 관리하려면 conf 파일에 수정을 해야함.
http status code 503 을 사용하여 에러 페이지로 redirect 하도록 다음과 같이 설정.

server {
    ...
    location / {
        error_page 503 /maintenance_on.html;
        if (-f [서비스경로]/maintenance_on.html) {
            return 503;
        }
        ...
    }
    # Error pages.  
    location = /maintenance_on.html {
        root [서비스경로];
    }
    ...
}
  • 점검 페이지가 사용되지 않을때는 maintenance_off.html 로 파일명을 유지.
  • 점검페이지를 띄우는 스크립트 (maintenance.py) 가 maintenance_on.html 로 점검페이지 파일명 변경.
  • maintenance_on.html 파일이 존재하는 경우 503 error code 를 반환.
  • 503 error code를 maintenance_on.html 로 매핑.
  • 요청이 /maintenance_on.html 로 들어오는 경우, 지정된 경로의 파일로 요청 redirect.

점검페이지 ON/OFF 스크립트

해당 설정을 빠르게 하기위해서 간단한 스크립트를 작성하였다.
사용 방법은 다음과 같다.

python3 maintenance.py [서비스명] [on or off]
'''
$> python3 maintenance.py [app_name] [on/off]

status = [on, off]
app_name = [서비스명]
@author yeji im
'''
import sys, os, time, os.path

CONSTRUCTION_BASE="[maintenance file path]"

def construction_site():
    app_name = sys.argv[1]
    status = sys.argv[2]
    CONSTRUCTION_BASE =  CONSTRUCTION_BASE + app_name + "/"

    new_file_name = "maintenance_" + status + ".html"
    old_file_name = "maintenance_";

    if status == "off":
        old_file_name = old_file_name + "on.html"
    else:
        old_file_name = old_file_name + "off.html"

    # change maintenance file
    os.rename(CONSTRUCTION_BASE + old_file_name, CONSTRUCTION_BASE + new_file_name)

construction_site()
반응형