nginx アクセスログ

$request_time $upstream_response_time

$request_time
request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client
http://nginx.org/en/docs/http/ngx_http_log_module.html

$upstream_response_time
keeps time spent on receiving the response from the upstream server; the time is kept in seconds with millisecond resolution. Times of several responses are separated by commas and colons like addresses in the $upstream_addr variable.
http://nginx.org/en/docs/http/ngx_http_upstream_module.html

Webサーバがnginxの場合、処理時間として$request_time、$upstream_response_timeをログに記録できます。$request_timeはnginxがリクエストを受け取りはじめてからレスポンスをクライアントに返しきるまでの時間(小数点を含む秒数⁠)⁠、$upstream_response_timeはnginxがリバースプロキシとして動作する場合に、プロキシ先からのレスポンスを得るまでの時間が記録されています。
第17回 Webアプリケーションのパフォーマンス改善(1) | gihyo.jp 2013

if=$loggable

【Nginx】ログの出力可否 - 地方エンジニアの学習日記 2021

複数条件にする場合は入れ子にする

map $status $loggable_status {
    ~^[4]   1;
    default 0;
}
map $http_user_agent $loggable_user_agent {
    ~*AppleWebKit   0;
    default $loggable_status; # Use map var above as default
}

access_log /var/log/nginx/error.log combined if=$loggable_user_agent;

https://stackoverflow.com/questions/44520875/nginx-logging-on-multiple-conditions

  • 特定拡張子の20x/30x応答はログに記録しない
  • ヘルスチェックは別ログに記録
map $uri $loggable_uri {
    ~*\.(css|gif|ico|jpeg|jpg|js|png|svg|webp|woff|woff2) 0;
    default 1;
}

map $status $loggable_status {
    ~^[23] $loggable_uri;
    default 1;
}

map $http_user_agent $loggable {
    ~Amazon-Route53-Health-Check-Service 0;
    ~ELB-HealthChecker 0;
    default $loggable_status;
} 

map $http_user_agent $healthcheck {
    ~Amazon-Route53-Health-Check-Service 1;
    ~ELB-HealthChecker 1;
    default 0;
}

server {
    listen  80;
    server_name  localhost;
    access_log   /var/log/nginx/access.log  main  if=$loggable;
    access_log   /var/log/nginx/healthcheck.log  main  if=$healthcheck;

!envができないのかなぁ、というのが疑問。
nginxでaccess_logの出力先を条件によって分ける設定。if=conditionを使う。 - Qiita 2016

access_log ディレクティブ

Sets the path, format, and configuration for a buffered log write. Several logs can be specified on the same configuration level. Logging to syslog can be configured by specifying the “syslog:” prefix in the first parameter. The special value off cancels all access_log directives on the current level. If the format is not specified then the predefined “combined” format is used.

https://nginx.org/en/docs/http/ngx_http_log_module.html

access_log ディレクティブは override しない

Nginxでアクセスログが重複する問題の解消方法 2021

https://serverfault.com/questions/729128/overriding-nginx-access-log-directive-duplicate-log-entries 2015

access_log ディレクティブなし

http {
    server {
    }
}

Default: access_log logs/access.log combined;

nginx -V
... --http-log-path=/var/log/nginx/access.log

/var/log/nginx/access.log
に出力

server

http {
    server {
        access_log  /var/log/nginx/host-access.log  main;
    }
}

/var/log/nginx/host-access.log
に出力

Several logs can be specified on the same configuration level.

http {
    access_log  /var/log/nginx/access.log  main;
    server {
        access_log  /var/log/nginx/host-access.log  main;
    }
}
/var/log/nginx/access.log
/var/log/nginx/host.access.log
の両方に出力

The special value off cancels all access_log directives on the current level.

http {
    access_log  /var/log/nginx/access.log  main;
    server {
        access_log  /var/log/nginx/host-access.log  main;
        location /foo/ {
           access_log off;
        }
    }
}

/var/log/nginx/access.log
/var/log/nginx/host.access.log
どちらにも /foo/ へのアクセスは出力されない