3

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?

engel0088
  • 203
  • 3
  • 12
  • is this ip same as ip of the machine where you application is deployed? – Sachin Singh Dec 20 '13 at 09:25
  • probably your traffic passes through nginx/varnish/stunnel/etc and the source addresses is changed. Check with your cloud provider to see how you can get the user's real ip address – Cristian Bica Dec 20 '13 at 09:56
  • I remember getting similar issue. You can try installing nginx real_ip module. Check if that helps. – Anirudhan J Dec 20 '13 at 12:26

3 Answers3

0
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

Gaurav Sharma
  • 477
  • 9
  • 24
0

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.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
0

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

Evan Machnic
  • 637
  • 1
  • 6
  • 8