ablog

不器用で落着きのない技術者のメモ

nginx でレスポンスタイムをログに記録する

nginx のログにリクエスト処理時間を記録したい場合、LogFormat で $request_time を指定する。

ログ出力

**.0.3.*** - - [29/Nov/2020:14:15:07 +0000] "GET / HTTP/1.1" 200 3520 0.000 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36" "-"
**.0.3.*** - - [29/Nov/2020:14:15:07 +0000] "GET /nginx-logo.png HTTP/1.1" 200 368 0.000★ "http://ec2-**-***-***-***.ap-northeast-1.compute.amazonaws.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36" "-"
**.0.3.*** - - [29/Nov/2020:14:15:33 +0000] "GET / HTTP/1.1" 304 0 0.000★ "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36" "-"

インストール・設定

  • nginxインストール
$ sudo amazon-linux-extras install nginx1
  • /etc/nginx/nginx.conf
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent $request_time "$http_referer" ' # ★ $request_time を追加
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
  • 起動
$  sudo systemctl start nginx.service
  • ステータス確認
$  sudo systemctl status nginx.service
* nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-11-29 14:08:52 UTC; 11s ago
  Process: 22550 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 22547 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 22543 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 22551 (nginx)
   CGroup: /system.slice/nginx.service
           |-22551 nginx: master process /usr/sbin/nginx
           |-22552 nginx: worker process
           |-22553 nginx: worker process
           |-22554 nginx: worker process
           |-22555 nginx: worker process
           |-22556 nginx: worker process
           |-22557 nginx: worker process
           |-22558 nginx: worker process
           |-22559 nginx: worker process
           |-22560 nginx: worker process
           |-22561 nginx: worker process
           |-22562 nginx: worker process
           |-22563 nginx: worker process
           |-22564 nginx: worker process
           |-22565 nginx: worker process
           |-22566 nginx: worker process
           `-22567 nginx: worker process

Nov 29 14:08:52 ip-***-**-3-178.ap-northeast-1.compute.internal systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 29 14:08:52 ip-***-**-3-178.ap-northeast-1.compute.internal nginx[22547]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 29 14:08:52 ip-***-**-3-178.ap-northeast-1.compute.internal nginx[22547]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 29 14:08:52 ip-***-**-3-178.ap-northeast-1.compute.internal systemd[1]: Started The nginx HTTP and reverse proxy server.