Rails ActionController:request.remote_ip 和 request.remote_addr 之间的区别
在 ActionController 源中,本地请求定义如下:
def local_request? #:doc:
request.remote_addr == LOCALHOST && request.remote_ip == LOCALHOST
end
在我的应用程序中,如果请求来自特定 IP 范围,我希望使用不同的逻辑。 request.remote_addr
和 request.remote_ip
之间有什么区别,我应该使用哪一个?
In the ActionController source, local requests are defined as follows:
def local_request? #:doc:
request.remote_addr == LOCALHOST && request.remote_ip == LOCALHOST
end
In my application, I want to use different logic if requests are coming from a particular IP range. What is the difference between request.remote_addr
and request.remote_ip
, and which one should I use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是
remote_ip
当前实现的作者,它所做的其他事情包括检查 IP 欺骗攻击,以及正确处理多个X-Forwarded-For
标头。不过,有一个很大的警告:只有某些 Ruby Web 服务器支持多个标头,因此该值仍然可能是错误的。我写下了测试最流行的 Ruby 应用服务器的结果 在我的博客上,您可能需要检查重复的标头对您的应用程序是否重要。
I'm the author of the current implementation of
remote_ip
, and the other things that it does include checking for IP spoofing attacks, and correctly handling multipleX-Forwarded-For
headers. There's a big caveat, though: only some Ruby web servers support multiple headers, so the value still might be wrong.I wrote up the results from testing the most popular Ruby app servers on my blog, which you might want to check out if repeated headers matter for your application.
似乎是这样的情况:
remote_addr
按原样返回REMOTE_ADDR
环境变量的值,而remote_ip
将根据存在情况调整该值以及HTTP_X_FORWARDED_FOR
和HTTP_CLIENT_IP
变量,例如当您的客户端通过代理转发时可能会遇到的情况。对
local_request?
的双重检查只是一种确定用户来自本地计算机的方法,而不是简单地通过本地代理从其他地方转发的方法。It seems to be the case that
remote_addr
returns the value of theREMOTE_ADDR
environment variable as-is, whileremote_ip
will adjust this based on the presence ofHTTP_X_FORWARDED_FOR
andHTTP_CLIENT_IP
variables as well, such as you might have when your client is being forwarded through a proxy.That double check for
local_request?
is simply a way of ascertaining that the user came from a local machine, and wasn't simply forwarded from somewhere else through a local proxy.