gpt4 book ai didi

ruby-on-rails - 使用 Rails 和 Nginx 获取客户端的真实 IP 地址?

转载 作者:行者123 更新时间:2023-12-04 04:50:32 25 4
gpt4 key购买 nike

我的服务器没有公共(public)IP地址,所以我不知道如何获取真实客户端的IP地址。

这是我的 nginx 的配置:

location / {
proxy_pass http://domain1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

在我的 Rails 应用程序的 Controller 中, request.iprequest.remote_ip返回我的服务器的网关地址。

我怎样才能获得客户端的真实IP?

如何从 Rails 请求中获取 X-Forwarded-For 值?

最佳答案

Rails 应该会自动为我们做这件事,但它似乎被当前的 3.x 打破了

我正在使用这个:

def ip() request.env['HTTP_X_FORWARDED_FOR'] || request.remote_ip end

关于ruby-on-rails - 使用 Rails 和 Nginx 获取客户端的真实 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6139861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com