Heroku 地理定位>>始终返回“西雅图,华盛顿州”

发布于 2024-10-28 17:45:46 字数 201 浏览 2 评论 0原文

在 Rails 3 中,geo_ip 和 geo_location gems 在我的本地计算机上返回准确的结果,但一旦上传到 Heroku,就会持续返回“西雅图,华盛顿州”(我位于宾夕法尼亚州)。

我做了一些挖掘,发现我正在使用的 Heroku 共享数据库位于西雅图。谁能指出我如何处理这种情况的正确方向?同样,在本地运行时,地理定位按预期工作。

谢谢!!

In Rails 3, both the geo_ip and the geo_location gems return accurate results on my local machine, but once uploaded to Heroku, persistently returns "Seattle, WA" (I'm located in Pennsylvania).

I did some digging, and found that the Heroku shared database I'm using is located in Seattle. Can anyone point me in the right direction for how to handle this situation? Again, while running locally the geolocation is working as intended.

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花期渐远 2024-11-04 17:45:46

如果您在 Heroku 上使用基于主机名的 SSL,目前无法获取请求的原始 IP。请参阅此帖子:

http://groups.google.com/group/heroku/browse_thread /thread/8cd2cba55f9aeb19

在该线程上,有人提到 http://jsonip.com/,它的作用是什么你会想到:

ultramarine:~ jdc$ curl http://jsonip.com/
{"ip":"131.247.152.2"}

我的计划是向 jsonip 发出 Ajax 请求,然后将 IP 传回服务器并使用 geoip 或 geokit 对其进行地理定位。我因此尝试使用 geoip :(

ruby-1.9.2-p136 :004 > c = GeoIP.new('GeoIP.dat').country('131.247.152.2')
 => ["131.247.152.2", "131.247.152.2", 225, "US", "USA", "United States", "NA"]

看起来 geokit 会更容易处理,因为它不需要我管理 IP 映射的 .dat 文件。我希望最终使用它。)

If you are using hostname-based SSL on Heroku, there is currently no way to get the request's original IP. See this thread:

http://groups.google.com/group/heroku/browse_thread/thread/8cd2cba55f9aeb19

On that thread, someone mentioned http://jsonip.com/, which does what you'd expect:

ultramarine:~ jdc$ curl http://jsonip.com/
{"ip":"131.247.152.2"}

My plan is to make an Ajax request to jsonip, then pass the IP back to the server and geolocate it using geoip or geokit. I tried it with geoip thusly:

ruby-1.9.2-p136 :004 > c = GeoIP.new('GeoIP.dat').country('131.247.152.2')
 => ["131.247.152.2", "131.247.152.2", 225, "US", "USA", "United States", "NA"]

(It seems like geokit will be easier to deal with because it doesn't require me to manage a .dat file of IP mappings. I expect to end up using that.)

终止放荡 2024-11-04 17:45:46

我不知道 heroku 是如何工作的,但你可能在负载均衡器后面。您需要将 TRUSTED_PROXIES 设置为将 request.remote_ip 设置为 HTTP_X_FORWARDED_FOR 地址。

您可以通过向控制器之一添加一个操作来检查这是否是您的问题:

def remote_ip
  render :text => "REMOTE_ADDR: %s<br/>remote_ip: %s<br/>HTTP_X_FORWARDED_FOR: %s" % 
                [ request.env['REMOTE_ADDR'],
                  request.remote_ip,
                  request.env['HTTP_X_FORWARDED_FOR'] ]
end

如果您有一个 HTTP_X_FORWARDED_FOR,那么您需要告诉 Rails 有关可信代理的信息。完成此操作后,您的 request.remote_ipHTTP_X_FORWARDED_FOR ip 将相同。

在您的 product.rb 中,添加以下行,其中 allowed_ips 正则表达式包含您的负载均衡器 IP。将 abc 替换为您从 heroku 获取的负载均衡器 IP。

# Setup Trusted Proxies
allowed_ips = /^a\.b\.c\.|^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i
ActionController::Request.const_set("TRUSTED_PROXIES", allowed_ips)

I don't know how heroku works, but you might be behind a load balancer. You'll need to set your TRUSTED_PROXIES to get request.remote_ip to be the HTTP_X_FORWARDED_FOR address.

You can check to see if this is your problem by adding an action to one of your controllers like this:

def remote_ip
  render :text => "REMOTE_ADDR: %s<br/>remote_ip: %s<br/>HTTP_X_FORWARDED_FOR: %s" % 
                [ request.env['REMOTE_ADDR'],
                  request.remote_ip,
                  request.env['HTTP_X_FORWARDED_FOR'] ]
end

If you've got an HTTP_X_FORWARDED_FOR, then you need to tell Rails about trusted proxies. Once you do that, your request.remote_ip and your HTTP_X_FORWARDED_FOR ips will be the same.

In your production.rb, add these lines, where the allowed_ips regex includes your load balancer IPs. Replace the a.b.c. with the load balancer IPs you get from heroku.

# Setup Trusted Proxies
allowed_ips = /^a\.b\.c\.|^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i
ActionController::Request.const_set("TRUSTED_PROXIES", allowed_ips)
辞旧 2024-11-04 17:45:46

有趣的。作为一名 Rails 菜鸟,我唯一能问的是它从哪里获取 IP?有没有办法确保它是从用户那里获取的? (完全的rails菜鸟,所以你可能已经在这样做了,我只是不知道)

我还发现了一个看起来制作精良的插件,称为GeoKit。链接:http://geokit.rubyforge.org/ -- 也许效果会更好?

Interesting. Being a Rails noob, the only thing I can ask is where is it getting the IP from? Is there a way to make sure it is getting it from the user? (Complete rails noob, so you may already be doing this, and I just don't know)

I also found a plugin that seems to be well made called GeoKit. Link: http://geokit.rubyforge.org/ -- Maybe it will work better?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文