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
You can use RemoteAddr to get the remote client's IP address and port (the format is "IP:port"), which is the address of the original requestor or the last proxy (for example a load balancer which lives in front of your server). This is all you have for sure. This is because internally http. Header.
Find your IP address using the command prompt (CMD)Open the Start menu and type cmd to open the Command Prompt. Type ipconfig into the Command Prompt and press Enter. The tool will return a set of data that includes your IP address.
The simplest way to determine the IP address of a website is to use our DNS Lookup Tool. Simply go to the DNS Lookup Tool, type the website URL into the text entry, and select Lookup. You'll notice the search yielded a list of IPv4 addresses that differ from the IPs shown using the other methods.
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.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With