通常nginxでアクセスログを吐くにはaccess_log
ディレクティブを利用するのだけど、streamモジュールによるTCPロードバランサーだとこのディレクティブが使えない。公式ドキュメントにもあるようにaccess_log
ディレクティブを利用できるのはhttp
, server
, location
, if in location
, limit_except
コンテキストに限られる。(nginxのTCPロードバランサーの設定はstream
コンテキストに記述する。)
#
# access_log is available in the contexts below
#
# http, server, location, if in location, limit_except
#
access_log /var/log/nginx/access.log;
というわけでaccess_log
は利用できないのでかわりにerror_log
を使う。ログレベルはinfo
以上じゃないと出力されない。
stream {
error_log /var/log/nginx/stream.log info;
}
出力はこんな感じ。
2016/02/29 11:38:16 [info] 76796#0: *4 client 127.0.0.1:63501 connected to 0.0.0.0:9999
2016/02/29 11:38:16 [info] 76796#0: *4 proxy 127.0.0.1:63502 connected to 127.0.0.1:9001
2016/02/29 11:38:16 [info] 76796#0: *4 client disconnected, bytes from/to client:78/171, bytes from/to upstream:171/78