在本地Rails开发环境中获取真实的IP地址 [英] Get real IP address in local Rails development environment
问题描述
我有Rails 2.3.8,Ruby 1.8.7,Mongrel Web Server和MySQL数据库.
I have Rails 2.3.8, Ruby 1.8.7, Mongrel Web Server and MySQL database.
我处于开发模式,我需要找到真实IP地址
当我使用request.remote_ip
时,我获得的IP为127.0.0.1
When I use request.remote_ip
I am getting the IP as 127.0.0.1
我知道我要获得127.0.0.1
是因为我正在本地计算机上进行开发..但是即使我在本地计算机上,也有办法获取真实的ip地址吗?
I know I am getting 127.0.0.1
because I am developing on the local machine.. but is there a way to get the real ip-address even if I am on the local machine?
我正在控制器中使用下面提到的这些,而我得到的只是127.0.0.1
,并且所有这些都在视图中.
I am using these mentioned below in my controller and all I get is 127.0.0.1
with all of them in the view.
request.remote_ip
request.env["HTTP_X_FORWARDED_FOR"]
request.remote_addr
request.env['REMOTE_ADDR']
推荐答案
据我所知,尚无获取本地计算机远程地址的标准方法.如果您需要使用远程地址来(进行地理编码)(测试),建议您将127.0.0.1添加到与IP地址与位置匹配的数据库表中.
As far as I can see there is no standard method for getting the remote address of your local machine. If you need the remote address for (testing) your geocoding, I suggest adding 127.0.0.1 to your database table that matches IP addresses with locations.
作为最后的选择,您还可以对您的远程地址进行硬编码.例如.像这样:
As a last resort you could also hard code your remote address. E.g. like this:
class ApplicationController < ActionController::Base
def remote_ip
if request.remote_ip == '127.0.0.1'
# Hard coded remote address
'123.45.67.89'
else
request.remote_ip
end
end
end
class MyController < ApplicationController
def index
@client_ip = remote_ip()
end
end
这篇关于在本地Rails开发环境中获取真实的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!