LoginSignup
1
0

More than 1 year has passed since last update.

docker+nginx+railsでアプリケーション側でクライアントのIPアドレスが取得できずに困った話

Posted at

dockerで起動しているrailsアプリケーションで、クライアントのIPアドレスを取得しようとしたら
docker内のIPアドレスしか取得できずに困った。

p request.remote_ip  //これがクライアントのIPではない

ただ、nginxのaccess_logには、末尾にクライアントのIPアドレスが出力されていた。

xxx.xxx.xxx.xxx - - [30/Jun/2021:13:19:53 +0000] "HEAD /robots.txt HTTP/1.1" 301 0 "-" "-" "クライアントのIPアドレス, xxx.xxx.xxx.xxx"

そこでnginxの設定ファイルに以下を追記

default.conf
+    proxy_set_header X-Forwarded-For $http_x_forwarded_for;

以下でクライアントのIPをrailsアプリケーション内で取得できた。

p request.env['HTTP_X_FORWARDED_FOR']
1
0
0

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
1
0