Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: How to Get the Client IP Address

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.

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.

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.

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

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.

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

"request.remote_ip" is a similar issue.

like image 773
Peter Lee Avatar asked Dec 05 '25 13:12

Peter Lee


1 Answers

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.

like image 127
Peter Lee Avatar answered Dec 07 '25 04:12

Peter Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!