LoginSignup
18
14

More than 5 years have passed since last update.

ロードバランサーごしにRemoteAddressを取得する

Posted at

サーバ構成を

     [世の中]
      |
  [ロードバランサー]
      |
 [アプリ][アプリ][アプリ]
      |
     [DB][DB]

みたいにしてる場合、アプリケーションサーバで普通に

php

$ip = $_SERVER['REMOTE_ADDR'];

とすると、ロードバランサーのIPが入っちゃって、ユーザのIPが分かんない。

php

if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ipArray = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    $ip = $ipArray[0];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}

↑こうすればいいらしい。

元ネタ
https://teratail.com/questions/131

18
14
1

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
18
14