웹사이트 검색

Ubuntu 20.04에서 ngxtop을 사용하여 Nginx 로그 파일 모니터링


이 페이지에서

  1. 전제 조건
  2. ngxtop 설치
  3. ngxtop 사용 방법
  4. 결론

ngxtop은 Nginx 웹 서버를 위한 무료 오픈 소스의 유연한 실시간 모니터링 도구입니다. Nginx 액세스 로그를 구문 분석하고 요청 수, 요청된 URI, 상태 코드별 요청 수 등에 대한 정보를 인쇄할 수 있습니다. Nginx 웹 서버로 들어오는 요청을 모니터링하는 간단하고 사용하기 쉬운 도구입니다.

이 기사에서는 Ubuntu 20.04에서 ngxtop 모니터링 도구를 설치하고 사용하는 방법을 보여줍니다.

전제 조건

  • Ubuntu 20.04를 실행하는 서버.\n
  • 서버에 루트 암호가 구성되어 있습니다.\n

ngxtop 설치

ngxtop은 Python 기반 도구이므로 시스템에 Python 및 PIP 패키지를 설치해야 합니다. 다음 명령을 사용하여 Nginx와 함께 설치할 수 있습니다.

apt-get install nginx python3 python3-pip -y

설치가 완료되면 아래와 같이 PIP를 사용하여 ngxtop 패키지를 설치할 수 있습니다.

pip3 install ngxtop

ngxtop이 설치되면 다음 명령을 사용하여 ngxtop의 버전을 확인할 수 있습니다.

ngxtop --version

다음 출력이 표시되어야 합니다.

xstat 0.1

ngxtop 사용 방법

이 섹션에서는 ngxtop을 사용하여 Nginx 웹 서버를 모니터링하는 방법을 보여줍니다.

인수 없이 ngxtop 명령을 실행하면 요청 수 요약, 요청 URI, 상태 코드별 요청 수가 표시됩니다.

ngxtop

다음 화면이 표시됩니다.

-l 옵션을 사용하여 분석하려는 액세스 로그를 지정할 수 있습니다.

ngxtop -l /var/log/nginx/access.log

다음 화면이 표시됩니다.

Nginx 서버에 액세스하는 상위 IP를 나열하려면 다음 명령을 실행하십시오.

ngxtop --group-by remote_addr -l /var/log/nginx/access.log

다음 화면이 표시됩니다.

다음 명령을 사용하여 전송된 총 바이트 수가 가장 높은 10개의 요청을 인쇄할 수 있습니다.

ngxtop --order-by 'avg(bytes_sent) * count' -l /var/log/nginx/access.log

다음 화면이 표시됩니다.

ngxtop을 사용하면 원격 서버에서 Apache 로그 파일을 구문 분석할 수도 있습니다. 다음 명령으로 수행할 수 있습니다.

ssh  tail -f /var/log/apache2/access.log | ngxtop -f common

ngxtop에서 사용 가능한 모든 옵션 목록을 얻으려면 다음 명령을 실행하십시오.

ngxtop --help

다음 출력이 표시되어야 합니다.

ngxtop - ad-hoc query for nginx access log.

Usage:
    ngxtop [options]
    ngxtop [options] (print|top|avg|sum)  ...
    ngxtop info
    ngxtop [options] query  ...

Options:
    -l , --access-log   access log file to parse.
    -f , --log-format   log format as specify in log_format directive. [default: combined]
    --no-follow  ngxtop default behavior is to ignore current lines in log
                     and only watch for new lines as they are written to the access log.
                     Use this flag to tell ngxtop to process the current content of the access log instead.
    -t , --interval   report interval when running in follow mode [default: 2.0]

    -g , --group-by   group by variable [default: request_path]
    -w , --having   having clause [default: 1]
    -o , --order-by   order of output for default query [default: count]
    -n , --limit   limit the number of records included in report for top command [default: 10]
    -a  ..., --a  ...  add exp (must be aggregation exp: sum, avg, min, max, etc.) into output

    -v, --verbose  more verbose output
    -d, --debug  print every line and parsed record
    -h, --help  print this help message.
    --version  print version information.

    Advanced / experimental options:
    -c , --config   allow ngxtop to parse nginx config file for log format and location.
    -i , --filter   filter in, records satisfied given expression are processed.
    -p , --pre-filter  in-filter expression to check in pre-parsing phase.

Examples:
    All examples read nginx config file for access log location and format.
    If you want to specify the access log file and / or log format, use the -f and -a options.

    "top" like view of nginx requests
    $ ngxtop

    Top 10 requested path with status 404:
    $ ngxtop top request_path --filter 'status == 404'

    Top 10 requests with highest total bytes sent
    $ ngxtop --order-by 'avg(bytes_sent) * count'

    Top 10 remote address, e.g., who's hitting you the most
    $ ngxtop --group-by remote_addr

    Print requests with 4xx or 5xx status, together with status and http referer
    $ ngxtop -i 'status >= 400' print request status http_referer

    Average body bytes sent of 200 responses of requested path begin with 'foo':
    $ ngxtop avg bytes_sent --filter 'status == 200 and request_path.startswith("foo")'

    Analyze apache access log from remote machine using 'common' log format
    $ ssh remote tail -f /var/log/apache2/access.log | ngxtop -f common

결론

위 가이드에서는 Ubuntu 20.04에서 ngxtop을 설치하고 사용하는 방법을 배웠습니다. 이제 명령줄 인터페이스에서 Nginx 로그를 쉽게 모니터링할 수 있기를 바랍니다.