LoginSignup
17

More than 5 years have passed since last update.

nginxのTCPロードバランサーでロギング

Posted at

通常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

参考

access_log in stream context

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17