Docker環境下でフロント部のリバースプロキシサーバーとコンテンツなどを配信するサーバーの構築した場合とする。設定は以下のように

Server A(nginx.conf)

events {
}

http {
    server {
        server_name localhost;
        location / {
            proxy_pass http://server-b;
            proxy_redirect off;
        }
    }
}

Server B(nginx.conf)

events {}

http {
    server {
        location / {
            root   /usr/share/nginx/html;
            index  index.html;
        }
    }
}

Dockerfileはほぼ共通でnginx.confを各サーバーに設定するだけなので省略。ではこれでアクセスしてみると

アクセスログ

192.168.1.10 - - [28/May/2025:15:27:24 +0000] "GET / HTTP/1.1" 200 2 "-" "curl/8.5.0"

※192.168.1.10はクライアント側のIP

Server Aはフロントのリバースプロキシサーバー側なので問題無いがServer B側では

172.16.0.3 - - [28/May/2025:15:27:24 +0000] "GET / HTTP/1.0" 200 2 "-" "curl/8.5.0"

※172.16.0.3はServer A側のIP

Server B側でも正しくクライアント側のIPのアクセスログが欲しいはずなので以下のような設定を行う

  1. Server AにX-Forwarded-Forの設定を行う

  2. setrealipfrom/realip_headerの設定を行う

Server A/nginx.conf

events {
}

http {
    server {
        server_name localhost;
        location / {
            proxy_pass http://server-b;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
        }
    }
}

Server B/nginx.conf

events {}

http {
    server {
        set_real_ip_from <Reverse ProxyサーバーのIP>;
        real_ip_header X-Forwarded-For;
        location / {
            root   /usr/share/nginx/html;
            index  index.html;
        }
    }
}

設定してから(Server B)アクセスログを見てみると

192.168.1.10 - - [28/May/2025:16:10:19 +0000] "GET / HTTP/1.0" 200 2 "-" "curl/8.5.0

てな感じになる。nginx側設定に関してはhttps://christina04.hatenablog.com/entry/2016/10/25/190000がすごく参考になるので(ry