user984621
user984621

Reputation: 48453

Rails - how to obtain visitors' IP address?

I need to store visitors' IP address to our database and here's the way I am trying to do that:

@ip = request.remote_ip
@ip = request.env['REMOTE_ADDR']

But in both cases, the @ip variable stored the value 127.0.0.1, even when I deploy the app to Amazon EC2 instance.

When I check http://www.whatismyip.com/, it shows my IP as 109.175.XXX.X.

Thus, why does the ruby variable always display the 127.0.0.1 address? How do I get the real IP?


EDIT: Here's the output of following:

request.env['HTTP_X_FORWARDED_FOR'] => 
request.remote_ip => 127.0.0.1
request.env['REMOTE_ADDR'] => 127.0.0.1
request.ip => 127.0.0.1

I thought that the problem is just on my side, but I sent links to 3 of my friends and all of them see the same IP, just 127.0.0.1.

I am solving this issue the whole day and still no success.

Thank you

Upvotes: 20

Views: 18565

Answers (3)

ray
ray

Reputation: 5552

x-forwarded-for. x-forwarded-for (XFF) is a standard proxy header which indicates the IP addresses that a request has flowed through on its way from the client to the server.

In my nginx server configuration I had,

proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

I was getting client remote IP as below,

request.env['HTTP_X_FORWARDED_FOR'] || request.remote_ip

Note: It can be wrong IP address if client is sitting behind proxy.

Upvotes: 6

Old Pro
Old Pro

Reputation: 25547

Note that if you are using AWS with any of their supplied Load Balancers, you may have to configure those load balancers appropriately in order to get them to forward the client IP.

Normally Stack Overflow does not like answers that are just a link, but in this case, since the question is very old and we do not the details to answer the specific question, and because the kind of load balancers AWS offers and the way to configure them keeps changing, I will just post a link to the AWS Knowledge Base instructions for forwarding the client IP address through a load balancer.

You can have similar issues if you are using a Content Delivery Network (CDN) to deliver data, in which case you need to contact your CDN provider for instructions.

@Jakob S correctly explains how to configure nginx to pass the X-Forwarded-For header, which is the current de facto standard, but the X- prefix means it is explicitly non-standard under the relevant HTTP spec. Eventually you may want to support the Forwarded header, which is specified in RFC 7239 but as of now (January 2019) it has not been widely adopted and is not worth switching to due to the lack of widespread support.

Upvotes: 0

Jakob S
Jakob S

Reputation: 20125

When you visit a site locally you're coming from the local IP address, ie 127.0.0.1.

What you're doing is the correct way to the visitors IP address, and the result you're seeing is as expected.

You want to use

@ip = request.remote_ip

because that takes into account most cases of reverse proxies and other situations you might encounter where request.env['REMOTE_ADDR'] might be nil or the address of the local proxy.

If you indeed do have a reverse proxy in front of your application server (and you probably do), you need to make sure it sets the proper headers when forwarding the requests. As a minimum the X-Forwarded-For header should be set.

Sample nginx configuration

If you're using nginx as a reverse proxy in front of your Rails application (ie using proxy_pass), you need to configure it to add the proper headers to the request it sends. In the case of X-Forwarded-For that is done using:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

You might want to also configure the following to have nginx forward the requested hostname and protocols:

# enable this if you forward HTTPS traffic to Rails,
# this helps Rack set the proper URL scheme for doing redirects:
proxy_set_header X-Forwarded-Proto $scheme;

# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;

Upvotes: 24

Related Questions