NginxのログをFluentdで送信する方法とカスタムログの追加

目次

概要

Fluentdを使うことで、外部にログを送信したり、ログをまとめたりできます。

ここでは、WebサーバーであるNginxのログをFluentdに入力するための方法を紹介します。

環境

  • Ubuntu 20.04
  • td-agent 1.11.2

そもそもこの記事を書いた理由

実はFluentdにはNginxのログを入力とする設定はあるのですが、Nginxのログの設定によってはうまくいきません。

具体的には、log_formatでより詳細なログを出力するmainの設定を使用するときです。

mainという設定は、UbuntuのNginxでは、デフォルトでは使用されていないので、設定を変更していない場合はそのままの設定で動きます。

しかし、ログは詳細な方が良いと言うことの方が多いはずです。

そこで、この記事ではmainの設定を使用した場合のFluentdの設定を紹介します。

Nginxの設定

Nginxでログの整形にmainの設定を使う場合は、Nginxでlog_formatのコメントインを行い、access_logにmainを追加します。

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;

このような設定ができていたら、Fluentdの設定を行います。

Fluentdの設定

Fluetndの設定として、/etc/td-agent/td-agentd.confに次のように記述します。

<source>
  @type tail
  path /var/log/nginx/access.log
  tag nginx.access
  pos_file /var/log/td-agent/nginx_access.log.pos
  format /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<timestamp>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?<forwarder>[^\"]*)")?/
  #format nginx
</source>

<source>
  type tail
  path /var/log/nginx/error.log
  tag nginx.error
  pos_file /var/log/td-agent/nginx_error.log.pos
  format /^(?<time>\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[(?<log_level>\w+)\] (?<pid>\d+).(?<tid>\d+): (?<message>.*)$/
</source>

Nginxのログは、アクセスログとエラーログのふたつがあるため、sourceもふたつになっています。

この設定の特徴として、fomratに正規表現を適用して、書き込まれたログをパースしています。この設定は、様々なサイトを見て決めました。

これで、設定は終了です。

ログにアクセスしたホストを追加

私の場合は、NginxのWebサーバーの機能であるバーチャルホストによって様々なサイトのアクセスを受け付けています。

そのため、アクセスしたホストをログに記述したいと思っていました。

そこで、次のように、アクセスログの設定を変更しました。

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                 '$status $body_bytes_sent "$http_referer" '
                 '"$http_user_agent" "$host" "$http_x_forwarded_for"';

$hostがアクセスしたホストを表します。

また、ログのフォーマットに合わせてFluentdの設定も変更します。

<source>
  @type tail
  path /var/log/nginx/access.log
  tag nginx.access
  pos_file /var/log/td-agent/nginx_access.log.pos
  format /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<timestamp>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?<host>[^\"]*)" "(?<forwarder>[^\"]*)")?/
  #format nginx
</source>

変更としては、hostという名前で新しくパースするというようにしています.

これで、ログの変更に合わせてFluentdの設定を変更できました。

さいごに

ログのパースのために正規表現を使用しているわけですが、結構面倒だと思いました。