Help us understand the problem. What is going on with this article?

nginxのアクセスログ解析にalpとngxtop

More than 1 year has passed since last update.

ISUCON用にnginxのアクセスログ解析して各ページごとの応答速度やサイズ、ステータスコード等いくつかの観点から集計ができる簡単に使えそうなツールとしてalpとngxtopがよさそうなのでインストール方法と簡単な使い方をまとめた。

環境: CentOS7

alp

https://github.com/tkuchiki/alp

alp導入(CentOS7)

  • alpコマンドインストール
# curl -L -O https://github.com/tkuchiki/alp/releases/download/v0.3.1/alp_linux_amd64.zip
# yum install -y unzip
# unzip alp_linux_amd64.zip
# mv alp /usr/bin/alp
  • nginx log formatの指定
http{

    ~~~

    ##  /etc/nginx/nginx.confのlog_formatとaccess_logの行を以下のように変更
    ##  

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #
    #access_log  /var/log/nginx/access.log  main;

    log_format ltsv "time:$time_local"
                "\thost:$remote_addr"
                "\tforwardedfor:$http_x_forwarded_for"
                "\treq:$request"
                "\tstatus:$status"
                "\tmethod:$request_method"
                "\turi:$request_uri"
                "\tsize:$body_bytes_sent"
                "\treferer:$http_referer"
                "\tua:$http_user_agent"
                "\treqtime:$request_time"
                "\tcache:$upstream_http_x_cache"
                "\truntime:$upstream_http_x_runtime"
                "\tapptime:$upstream_response_time"
                "\tvhost:$host";

    access_log  /var/log/nginx/access.log  ltsv;

一度ログを空にしてから設定変更反映

# echo -n "" > /var/log/nginx/access.log
# systemctl restart nginx

使い方

## 出力はpathごとの統計情報
## COUNT: アクセス回数
## MIN、MAX、SUM、AVG: 
# alp -f /var/log/nginx/access.log

+-------+-------+-------+-------+-------+-------+-------+-------+--------+-----------+-----------+-----------+--
---------+--------+-----+
| COUNT |  MIN  |  MAX  |  SUM  |  AVG  |  P1   |  P50  |  P99  | STDDEV | MIN(BODY) | MAX(BODY) | SUM(BODY) | A
VG(BODY) | METHOD | URI |
+-------+-------+-------+-------+-------+-------+-------+-------+--------+-----------+-----------+-----------+--
---------+--------+-----+
|     1 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 |  0.000 |   612.000 |   612.000 |   612.000 |  
 612.000 | GET    | /   |
+-------+-------+-------+-------+-------+-------+-------+-------+--------+-----------+-----------+-----------+--
---------+--------+-----+



## アクセス回数順にソート(-rで降順)
#alp -f /var/log/nginx/access.log --cnt -r

ngxtop

https://github.com/lebinh/ngxtop

インストール

# yum install -y epel-release
# yum install -y python-pip
# pip install ngxtop

ngxtopはnginx.confのlog_formatを見てよしなにログをパースしてくれるみたいなので
alpのlog_formatをそのまま使っている。

使い方

## topコマンドのように以下のコマンド入力後のnginxのアクセスを解析して
# 以下のようにpathごとに内容を表示する
# nginxtop

running for 380 seconds, 56 records processed: 0.15 req/sec

Summary:
|   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|---------+------------------+-------+-------+-------+-------|
|      56 |          393.321 |     8 |    18 |    30 |     0 |

Detailed:
| request_path   |   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|----------------+---------+------------------+-------+-------+-------+-------|
| /              |      26 |          188.308 |     8 |    18 |     0 |     0 |
| /test          |      22 |          571.000 |     0 |     0 |    22 |     0 |
| /favicon.ico   |       8 |          571.000 |     0 |     0 |     8 |     0 |


## ログファイルを解析して出力する場合
# ngxtop --no-follow

## 出力されるpathの上限を変更する(デフォルト 10)

# ngxtop -n 100 --no-follow
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした