Ruby on Rails:如何获取客户端IP地址

时间:2021-08-27 23:32:15

I have searched a lot, and I could not find a solution. I tried:

我找了很多,却找不到解决的办法。我试着:

Rails.logger.error request.remote_ip
Rails.logger.error request.env["REMOTE_ADDR"]

But the result is something weird: 10.130.150.254 or 10.130.150.251.

但结果有些奇怪:10.130.150.254或10.130.150.251。

I used my cell phone's LTE/4G networking to test, and it always gave me 10.130.247.251, which is a Class A reserved private network IP address.

我用手机的LTE/4G网络进行测试,它总是给我10.130.247.251,这是一个a类预留的私有网络IP地址。

I also tried:

我也试过:

Rails.logger.error request.env["HTTP_X_FORWARDED_FOR"]
Rails.logger.error request.env["HTTP_X_REAL_IP"]
Rails.logger.error request.env["HTTP_X_CLUSTER_CLIENT_IP"]

But those gave me empty strings.

但它们给了我空字符串。

When I use my cell phone and point to touch.whatsmyip.org, it gives me: 172.56.9.89, which is a public IP address.

当我用手机指向touch.whatsmyip.org时,它会告诉我:172.56.9.89,这是一个公共IP地址。

We use HTTPS SSL termination and a load balancer, it probably looks like "Get Remote Host Client IP Behind Load Balancers (ELB)".

我们使用HTTPS SSL终端和负载均衡器,它可能看起来像“在负载均衡器(ELB)之后获得远程主机客户端IP”。

How do I get the client IP address? If the client is behind a proxy, that's fine, then the proxy IP address is also good for me.

如何获得客户端IP地址?如果客户端位于代理的后面,那没问题,那么代理IP地址也很适合我。

I'm using Apache and Passenger to serve the application.

我正在使用Apache和Passenger来服务这个应用程序。

"request.remote_ip" is a similar issue.

”的请求。remote_ip”是一个类似的问题。

2 个解决方案

#1


5  

It turns out that our IT had not configured to pass it at the Load Balancer. Once he finished configuring, it works. Both request.env["HTTP_X_FORWARDED_FOR"] and request.remote_ip works.

事实证明,我们的It没有配置在负载均衡器上传递它。一旦他完成了配置,它就可以工作了。这两个请求。env(“HTTP_X_FORWARDED_FOR”)和请求。remote_ip作品。

#2


0  

Hope this will be helpful

希望这能有所帮助

env['action_dispatch.remote_ip'].calculate_ip

env .calculate_ip action_dispatch.remote_ip”

#1


5  

It turns out that our IT had not configured to pass it at the Load Balancer. Once he finished configuring, it works. Both request.env["HTTP_X_FORWARDED_FOR"] and request.remote_ip works.

事实证明,我们的It没有配置在负载均衡器上传递它。一旦他完成了配置,它就可以工作了。这两个请求。env(“HTTP_X_FORWARDED_FOR”)和请求。remote_ip作品。

#2


0  

Hope this will be helpful

希望这能有所帮助

env['action_dispatch.remote_ip'].calculate_ip

env .calculate_ip action_dispatch.remote_ip”