简体   繁体   English

在Rails中获取实际的远程IP?

[英]Get Actual Remote IP in Rails?

I'm developing rails(2.3.8) application. 我正在开发rails(2.3.8)应用程序。 I need to store actual client's remote IP address. 我需要存储实际客户端的远程IP地址。

request.remote_ip returns only 127.0.0.1 request.remote_ip仅返回127.0.0.1

but I need to store actual remote IP such as 93.43.56.77 . 但我需要存储实际的远程IP,如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 . 如果您的请求来自您的开发机器并且开发机器是您的服务器所在的位置,那么您可能会得到127.0.0.1 But if the request is coming from another machine, this will be the IP the remote machine. 但是如果请求来自另一台机器,这将是远程机器的IP。 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). 但是,在各种条件下,这可能不是机器的真正IP(代理机器,使用tor或其他工具隐藏它等)。

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. '93 .43.56.77',适用于通过无线或有线连接到网络的网络适配器。
  • '127.0.0.1' which is a virtual adapter which is used when connecting to itself. '127.0.0.1'是连接到自身时使用的虚拟适配器。

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. 除非连接在该连接上运行,否则您不会让应用程序显示'93 .43.56.77',这意味着它需要位于不同的计算机上。

If you are so concerned about it, you can easily push it up to Heroku where it should work as you expect. 如果你如此关注它,你可以轻松地将它推到Heroku,它应该按照你的预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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