Can not get remote ip in Rails 3

1k views Asked by At

I am trying to get client ip in Rails 3. Application is installed in cloud hosting, with SSL, and nginx server.

I wrote some code to get client ip.

request.remote_ip
request.env['HTTP_X_FORWARDED_FOR']

But it returns wrong address, like '10.159.21.86' Is there any issue related Nginx server, or SSL installation?

3

There are 3 answers

0
Evan Machnic On

If you're using SSL with HAProxy (the default configuration for multi-instance environments) then it will not be able to pull the remote IP due to the hand-off from HAProxy to Nginx. We have a solution that uses stunnel to get around this but since all SSL decryption is done on the App Master instance, if you have more than about five instances then performance will suffer.

The other option is to use Elastic Load Balancer instead of HAProxy. The documentation for that is at https://support.cloud.engineyard.com/entries/21715452-Use-Elastic-Load-Balancing-with-Engine-Yard-Cloud.

Evan

0
Gaurav Sharma On
def remote_ip  
  @remote_ip ||= (@env["action_dispatch.remote_ip"] || ip).to_s
end

request.remote_ip gets the ip address of the client outside of local proxies but If your request is coming from your development machine and the development machine is where your server is, probably you will get 127.0.0.1 or wrong ip But if the request is coming from another machine, this will be the IP the remote machine. However, under various conditions this may not be the real IP of the machine (machine behind proxy, using tor or other tool to hide it e.t.c.). so you can also try:-

request.env['REMOTE_ADDR']

You should visit this post written by rails contributor describing Repeated headers and Ruby web servers

0
Simone Carletti On

I believe the issue you have is the same described in the following Engine Yard support request: HAProxy, SSL Requests & Request IP Addresses.

Apparently there is a workaround, but you are supposed to contact them directly to know what it is.

The docs team is working on formal documentation, for the short term, please open a ticket and a support engineer can help out.