Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
14
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@rev84

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

サーバ構成を

     [世の中]
      |
  [ロードバランサー]
      |
 [アプリ][アプリ][アプリ]
      |
     [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

14
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
14
Help us understand the problem. What is going on with this article?