nginx、php7、および http_realip_module がインストールされています。
2 つの Web サイトにサービスを提供する 1 つのサーバーがあります。
サイト 1 nginx 構成:
server {
...
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_read_timeout 300;
proxy_set_header REMOTE_ADDR $http_x_real_ip;
proxy_set_header X-Forwarded-For $http_x_real_ip;
}
}
これにより、$_SERVER['REMOTE_ADDR"] をダンプするときにクライアントの IP アドレスが入力されます。
サイト 1 は、単純な API のように curl を使用してサイト 2 に接続します。
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_URL, $serverUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"REMOTE_ADDR: ".$_SERVER['REMOTE_ADDR'],
"HTTP_X_FORWARDED_FOR: ".$_SERVER['REMOTE_ADDR'],
));
$result = curl_exec($ch);
サイト 2 nginx 構成:
server {
...
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_read_timeout 300;
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
}
}
サイト 2 のスクリプトが呼び出されることがわかりますが、PHP で $_SERVER['REMOTE_ADDR'] 変数を確認すると、クライアントの IP アドレスではなく、サーバーの IP アドレスが表示されます。ここでnginxのセットアップは正しいですか?
どうすればこれを正しく動作させることができますか?