【メモ】【勉強】nginxでロードバランス先を出力する

nginxでロードバランスを行った際、
デフォルトの設定ではアクセスログにロードバランス先が出力されない。

ロードバランス先の出力方法を調べてみると、以下の記事が参考になったのでメモとして残しておく。
yapud.hatenablog.com

修正内容は以下の通りになります。
ポイントは、nginx.confでは複数のログフォーマットをそれぞれ別名で定義できること。

下記の場合だと、nginx宛のアクセスログのフォーマットを「main 」、
ロードバランス先のアクセスログのフォーマットを「upstreamlog」という名称で定義しています。

※まだ検証途中なので、nginxとロードバランス先のサーバーは同じインスタンスで動かしています。

worker_processes 1;

events {
        worker_connections 1024;
}

http {
        include mime.types;
        default_type application/octet-stream;

        log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" '
        access_log /var/log/nginx/access.log main;
        
        # 追記したログフォーマット
        log_format upstreamlog '[$time_local] $remote_addr $host $upstream_addr $request';
        access_log /var/log/nginx/upstream.log upstreamlog;

        upstream backends {
                keepalive 32;
                server 127.0.0.1:8080;

        }

        server {
                listen 80;
                server_name www.example.com;

                location / {
                        proxy_http_version 1.1;
                        proxy_set_header Connection "";
                        proxy_set_header Host $http_host;

                        proxy_pass http://backends;
                }
        }
}

以上。