Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the real IP address of client with Rails and Nginx?

My server doesn't have a public IP address, so I don't know how to get the real client's IP address.

This is my nginx's configuration:

location / {
    proxy_pass http://domain1;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP     $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
}

In my Rails app's controller both request.ip and request.remote_ip return my server's gateway address.

How can i get the real IP of client?

How to get X-Forwarded-For value from Rails request?

like image 222
ethan Avatar asked May 26 '11 14:05

ethan


People also ask

How do I find my IP address in Ruby on Rails?

remote_ip checks all IPs present in the HTTP header, looking for fields generally used by firewalls, load balancers, or proxies, such as HTTP_X_FORWARDED_FOR, and make a guess to return what seems to be the correct visitor's IP address. From now, if you connect to your server, you will see your real IP address.


2 Answers

Rails was supposed to be doing it automatically for us, but it seems to be broken with current 3.x

I'm using this:

def ip() request.env['HTTP_X_FORWARDED_FOR'] || request.remote_ip end
like image 131
Kevin Avatar answered Oct 12 '22 01:10

Kevin


You should get the header value X-forwarded-for

http://en.wikipedia.org/wiki/X-Forwarded-For

like image 20
Rizwan Sharif Avatar answered Oct 12 '22 00:10

Rizwan Sharif