Rails - 如何获取访问者的 IP 地址? [英] Rails - how to obtain visitors' IP address?

查看:63
本文介绍了Rails - 如何获取访问者的 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将访问者的 IP 地址存储到我们的数据库中,这是我尝试这样做的方式:

I need to store visitors' IP address to our database and here's the way I am trying to do that:

@ip = request.remote_ip
@ip = request.env['REMOTE_ADDR']

但在这两种情况下,@ip 变量都存储了值 127.0.0.1,即使我将应用程序部署到 Amazon EC2 实例也是如此.

But in both cases, the @ip variable stored the value 127.0.0.1, even when I deploy the app to Amazon EC2 instance.

当我检查 http://www.whatismyip.com/ 时,它显示我的 IP 为 109.175.XXX.X.

When I check http://www.whatismyip.com/, it shows my IP as 109.175.XXX.X.

那么,为什么 ruby​​ 变量总是显示 127.0.0.1 地址?如何获取真实IP?

Thus, why does the ruby variable always display the 127.0.0.1 address? How do I get the real IP?

这是以下输出:

request.env['HTTP_X_FORWARDED_FOR'] => 
request.remote_ip => 127.0.0.1
request.env['REMOTE_ADDR'] => 127.0.0.1
request.ip => 127.0.0.1

我以为问题出在我这边,但我向我的 3 个朋友发送了链接,他们都看到了相同的 IP,只是 127.0.0.1.

I thought that the problem is just on my side, but I sent links to 3 of my friends and all of them see the same IP, just 127.0.0.1.

我一整天都在解决这个问题,但仍然没有成功.

I am solving this issue the whole day and still no success.

谢谢

推荐答案

当您在本地访问站点时,您来自本地 IP 地址,即 127.0.0.1.

When you visit a site locally you're coming from the local IP address, ie 127.0.0.1.

您所做的是访问访问者 IP 地址的正确方式,您看到的结果与预期一致.

What you're doing is the correct way to the visitors IP address, and the result you're seeing is as expected.

你想使用

@ip = request.remote_ip

因为这考虑到了大多数反向代理情况以及您可能遇到的其他情况,其中 request.env['REMOTE_ADDR'] 可能为零或本地代理的地址.

because that takes into account most cases of reverse proxies and other situations you might encounter where request.env['REMOTE_ADDR'] might be nil or the address of the local proxy.

如果你的应用服务器前确实有一个反向代理(你可能有),你需要确保它在转发请求时设置了正确的标头.至少 X-Forwarded-For 标头应该设置.

If you indeed do have a reverse proxy in front of your application server (and you probably do), you need to make sure it sets the proper headers when forwarding the requests. As a minimum the X-Forwarded-For header should be set.

如果您在 Rails 应用程序前使用 nginx 作为反向代理(即使用 proxy_pass),则需要对其进行配置,以将适当的标头添加到它发送的请求中.对于 X-Forwarded-For 使用:

If you're using nginx as a reverse proxy in front of your Rails application (ie using proxy_pass), you need to configure it to add the proper headers to the request it sends. In the case of X-Forwarded-For that is done using:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

您可能还需要配置以下内容以让 nginx 转发请求的主机名和协议:

You might want to also configure the following to have nginx forward the requested hostname and protocols:

# enable this if you forward HTTPS traffic to Rails,
# this helps Rack set the proper URL scheme for doing redirects:
proxy_set_header X-Forwarded-Proto $scheme;

# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;

这篇关于Rails - 如何获取访问者的 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆