How to get client IP and Server IP using Rails

Senthil Kumar Bhaskaran picture Senthil Kumar Bhaskaran · Aug 4, 2009 · Viewed 49.2k times · Source

Can anyone please help how to get client IP and also server IP using Ruby on Rails?

Answer

karim79 picture karim79 · Aug 4, 2009

From your controller:

request.remote_ip

If you are using apache in front of a mongrel, then remote_ip will return the source address of the request, which in this case will be local host because the Apache web server is making the request, so instead put this in your controller:

@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]

To get the server IP see:

Getting the Hostname or IP in Ruby on Rails