简体   繁体   中英

Get Actual Remote IP in Rails?

I'm developing rails(2.3.8) application. I need to store actual client's remote IP address.

request.remote_ip returns only 127.0.0.1

but I need to store actual remote IP such as 93.43.56.77 . Any ruby gems is available? or how do get that?

Try this:

request.env['REMOTE_ADDR']

If your request is coming from your development machine and the development machine is where your server is, probably you will get 127.0.0.1 . But if the request is coming from another machine, this will be the IP the remote machine. However, under various conditions this may not be the real IP of the machine (machine behind proxy, using tor or other tool to hide it etc).

Your computer essentially has two network interfaces.

  • '93.43.56.77' which is for your network adapter which connects to the network via wireless or wire.
  • '127.0.0.1' which is a virtual adapter which is used when connecting to itself.

You will not get the application to show '93.43.56.77' unless the connection is running over that connection, which means it will need to be on a different computer.

If you are so concerned about it, you can easily push it up to Heroku where it should work as you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM