NGINX stub_status 모듈 및 Prometheus Grafana 모니터링

NGINX는 많이 사용되고 있는 웹 서버이자 리버스 프록시 서버입니다.
그러나 이를 효과적으로 운영하기 위해서는 실시간 모니터링이 필수적입니다. 특히 클라이언트 연결 수, 요청 상태, 리소스 사용률 등 주요 지표를 파악함으로써 성능 저하와 장애를 예방할 수 있습니다.

NGINX 모니터링의 핵심 도구로 ngx_http_stub_status_module을 활용할 수 있으며, 이를 Prometheus Exporter 및 Grafana와 연동하여 효율적인 모니터링 대시보드를 구축할 수 있습니다.

목차

1. ngx_http_stub_status_module이란?
2. ngx_http_stub_status_module구성
3. NGINX Prometheus Exporter 배포
4. Prometheus & Grafana 배포 및 연동
5. 결론

1. ngx_http_stub_status_module이란?

NGINX의 기본 상태 데이터 페이지를 보여주는 module입니다.

컴파일 설치의 경우 --with-http_stub_status_module 설정 매개변수를 이용하여 설치해야합니다.

모듈을 통해 NGINX-Prometheus-Exporter를 사용하여 Prometheus 전용 Metric으로 변경할 수 있습니다.

구문 : 	        stub_status;
기본 값: 	—
Context: 	server, location

location에서 지정하여 기본 상태 페이지를 설정할 수 있습니다.

2. ngx_http_stub_status_module 구성

ngx_http_stub_status_module 모듈이 설치되어 있는 NGINX를 준비합니다.

NGINX 구성을 변경합니다.

server {
    listen       80;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location /metrics { # NGINX의 현재 상태를 확인할 stub_status 경로를 지정합니다.
        stub_status on; # stub_status 를 활성화 합니다.
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

stub_status 경로로 접속하여 결과를 확인합니다.

Metrics에 대한 설명은 NGINX Status 모듈 비교: NGINX OSS vs NGINX Plus 포스트에서 확인할 수 있습니다.

3. NGINX Prometheus Exporter 배포

NGINX Prometheus Exporter를 통해 Prometheus에서 사용할 수 있는 Metrics로 변경 할 수 있습니다.

Docker Pull 명령어를 통해 nginx-prometheus-exporter 이미지를 가져옵니다.
(이전에 이미 이미지를 가져왔기 때문에 새로 다운로드 하지 않았습니다.)

nginx-prometheus-exporter를 Docker로 배포합니다.

포트를 9113을 사용할 수 있도록 vm의 9113 포트에 매핑합니다.
–nginx.scrape-uri의 인자 값을 이전에 구성했던 NGINX stub_status 의 uri로 지정하여 실행합니다.

docker run -d -p 9113:9113 --name nginx-prometheus-exporter 
nginx/nginx-prometheus-exporter --nginx.scrape-uri=http://[NGINX stub_status]/metrics

9113 포트로 접속하여 NGINX Prometheus Exporter를 확인합니다.

NGINX Prometheus Exporter의 메인 페이지를 확인할 수 있습니다.

Metrics를 클릭합니다.

Metrics를 확인할 수 있습니다.

NGINX Prometheus Exporter에서 확인할 수 있는 NGINX OSS 메트릭은 아래와 같습니다.

이름설명
nginx_connections_accepted클라이언트 연결이 수락된 횟수를 나타냅니다.
nginx_connections_active현재 활성화된 클라이언트 연결의 수를 나타냅니다.
nginx_connections_handled처리된 클라이언트 연결의 총 횟수를 나타냅니다.
nginx_connections_readingNGINX가 요청 헤더를 읽고 있는 연결 수를 나타냅니다.
nginx_connections_waiting유휴 상태인 클라이언트 연결 수를 나타냅니다.
nginx_connections_writingNGINX가 응답을 클라이언트에게 보내고 있는 연결 수를 나타냅니다.
nginx_http_requests_total총 HTTP 요청 수를 나타냅니다.

NGINX Prometheus Exporter와 Prometheus를 연동합니다.

4. Prometheus & Grafana 배포 및 연동

Docker을 이용하여 Prometheus를 NGINX Prometheus Exporter의 Metrics를 받을 수 있도록 배포합니다.

Docker 배포 전 먼저 Prometheus 구성 파일을 생성합니다.

global: 
  scrape_interval: 10s # metric을수집하는 주기
  evaluation_interval: 10s
scrape_configs:
  - job_name: "nginx_metrics"

    static_configs:
      - targets: ["192.168.200.25:9113"] # NGINX Prometheus Exporter를 배포 한 IP:Port

Docker Pull 명령어를 통해 prom/prometheus 이미지를 가져옵니다.
(이전에 이미 이미지를 가져왔기 때문에 새로 다운로드 하지 않았습니다.)

docker pull prom/prometheus

Prometheus를 Docker로 배포합니다.

이전에 구성했던 Prometheus 구성 파일을 마운트합니다.

docker run -d -p 9090:9090 --name prometheus -v /stub_status/config.yml:/etc/prometheus/prometheus.yml prom/prometheus

Prometheus에 접속합니다.

접속하여 Status > Target health로 이동합니다.

NGINX의 상태를 확인 할 수 있는 것을 볼 수 있습니다.

Query로 이동하여 nginx의 Metrics를 확인합니다.

NGINX의 메트릭을 확인할 수 있습니다.

Metric을 이용하여 Grafana로 시각화합니다.

Docker Pull을 통해 grafana/grafana 이미지를 가져옵니다.
(이전에 이미 이미지를 가져왔기 때문에 새로 다운로드 하지 않았습니다.)

docker pull grafana/grafana

Grafana를 배포합니다.

docker run -d --name=grafana -p 3000:3000 grafana/grafana

Grafana로 접속합니다.

Grafana의 초기 아이디와 비밀번호는 admin admin 입니다.

admin 계정으로 로그인합니다.

Connection을 선택하여 Grafana에서 Prometheus의 Metrics를 받을 수 있도록 구성합니다.

Prometheus를 선택하여 Data source를 추가합니다.

Connection의 Prometheus server URL에 배포했었던 Prometheus의 URL을 넣습니다.

Save & test 버튼을 눌러 테스트 및 저장합니다.

저장한 후 Dashboards로 이동하여 대시보드를 생성합니다.

Import를 선택하여 NGINX Exporter의 Dashboard를 가져옵니다.

NGINX Exporter의 Dashboard Id를 붙여넣습니다.

data source를 이전에 구성했었던 prometheus를 선택합니다.

Import 버튼을 눌러 Dashboard를 추가합니다.

NGINX OSS의 Metric을 시각화하여 확인할 수 있습니다.

위의 대시보드에서 확인할 수 있는 메트릭은 아래와 같습니다.

  • NGINX Status – NGINX의 현재 상태
  • Processed connection – NGINX 서버가 처리한 총 연결 수
  • Active Connections – NGINX 서버에 활성 연결 수
  • Total requests – NGINX 서버에 현재 요청되고 있는 전체 수

시간을 조정하여 이전에 있었던 Metric도 확인할 수 있습니다.

NGINX ngx_http_stub_status_module + NGINX Prometheus Exporter + Prometheus + Grafana를 사용하여 NGINX OSS 모니터링을 사용할 수 있습니다.

5. 결론

NGINX 모니터링은 안정적인 웹 서비스 운영의 핵심입니다. ngx_http_stub_status_module과 Prometheus Exporter를 활용해 실시간 메트릭 데이터를 수집하고, 이를 Prometheus와 Grafana로 시각화함으로써 NGINX의 성능 및 상태를 종합적으로 분석할 수 있습니다.

모니터링을 활용하여 NGINX의 현재 상태를 파악하고, 발생 가능한 문제를 사전에 예방하며, 안정적이고 효율적인 서버 운영을 도모할 수 있습니다.

NGINX OSS는 적은 Metrics들을 확인할 수 있지만 NGINX Plus에서는 Prometheus.njs 모듈을 통해 많은 Metric들과 NGINX 내장 대시보드를 사용할 수 있습니다.

NGINX Plus를 직접 사용해 보시려면 30일 무료 평가판을 신청하거나 NGINX STORE에 연락하여 논의하십시오.

NGINX STORE를 통한 솔루션 도입 및 기술지원 무료 상담 신청

* indicates required