Rails ActionController:request.remote_ip 和 request.remote_addr 之间的区别

发布于 2024-10-01 00:14:18 字数 301 浏览 9 评论 0原文

在 ActionController 源中,本地请求定义如下:

def local_request? #:doc:
    request.remote_addr == LOCALHOST && request.remote_ip == LOCALHOST
end

在我的应用程序中,如果请求来自特定 IP 范围,我希望使用不同的逻辑。 request.remote_addrrequest.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 技术交流群。

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

发布评论

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

评论(2

月下客 2024-10-08 00:14:18

我是 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 multiple X-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.

无妨# 2024-10-08 00:14:18

似乎是这样的情况:remote_addr按原样返回REMOTE_ADDR环境变量的值,而remote_ip将根据存在情况调整该值以及 HTTP_X_FORWARDED_FORHTTP_CLIENT_IP 变量,例如当您的客户端通过代理转发时可能会遇到的情况。

local_request? 的双重检查只是一种确定用户来自本地计算机的方法,而不是简单地通过本地代理从其他地方转发的方法。

It seems to be the case that remote_addr returns the value of the REMOTE_ADDR environment variable as-is, while remote_ip will adjust this based on the presence of HTTP_X_FORWARDED_FOR and HTTP_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.

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