title: Ruby on Rails: How to Get the Client IP Address
body: <p>I have searched a lot, and I could not find a solution. I tried:</p>
  
  <pre><code>Rails.logger.error request.remote_ip
  Rails.logger.error request.env["REMOTE_ADDR"]
  </code></pre>
  
  <p>But the result is something weird: <code>10.130.150.254</code> or <code>10.130.150.251</code>.</p>
  
  <p>I used my cell phone's LTE/4G networking to test, and it always gave me <code>10.130.247.251</code>, which is a Class A reserved private network IP address.</p>
  
  <p>I also tried:</p>
  
  <pre><code>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"]
  </code></pre>
  
  <p>But those gave me empty strings.</p>
  
  <p>When I use my cell phone and point to touch.whatsmyip.org, it gives me: <code>172.56.9.89</code>, which is a public IP address.</p>
  
  <p>We use HTTPS SSL termination and a load balancer, it probably looks like "<a href="http://blog.svnlabs.com/get-remote-host-client-ip-behind-load-balancers-elb" rel="nofollow">Get Remote Host Client IP Behind Load Balancers (ELB)</a>".</p>
  
  <p>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.</p>
  
  <p>I'm using Apache and Passenger to serve the application.</p>
  
  <p>"<a href="https://groups.google.com/forum/#!topic/heroku/jNLLpV-a6xk" rel="nofollow">request.remote_ip</a>" is a similar issue.</p>