Heroku 地理定位>>始终返回“西雅图,华盛顿州”
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在 Heroku 上使用基于主机名的 SSL,目前无法获取请求的原始 IP。请参阅此帖子:
http://groups.google.com/group/heroku/browse_thread /thread/8cd2cba55f9aeb19
在该线程上,有人提到 http://jsonip.com/,它的作用是什么你会想到:
我的计划是向 jsonip 发出 Ajax 请求,然后将 IP 传回服务器并使用 geoip 或 geokit 对其进行地理定位。我因此尝试使用 geoip :(
看起来 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:
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:
(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.)
我不知道 heroku 是如何工作的,但你可能在负载均衡器后面。您需要将
TRUSTED_PROXIES
设置为将 request.remote_ip 设置为HTTP_X_FORWARDED_FOR
地址。您可以通过向控制器之一添加一个操作来检查这是否是您的问题:
如果您有一个
HTTP_X_FORWARDED_FOR
,那么您需要告诉 Rails 有关可信代理的信息。完成此操作后,您的request.remote_ip
和HTTP_X_FORWARDED_FOR
ip 将相同。在您的
product.rb
中,添加以下行,其中 allowed_ips 正则表达式包含您的负载均衡器 IP。将abc
替换为您从 heroku 获取的负载均衡器 IP。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 theHTTP_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:
If you've got an
HTTP_X_FORWARDED_FOR
, then you need to tell Rails about trusted proxies. Once you do that, yourrequest.remote_ip
and yourHTTP_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 thea.b.c.
with the load balancer IPs you get from heroku.有趣的。作为一名 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?