How to get user's IP

Pawel Stawicki wrote:


I want to log users who enter my site and what they are doing.
Registration and login is not required, so what is left is IP.
(something like wikipedia, you can edit without logging in, and then
there is your IP in log). How to get this IP in Ruby on Rails?

And if you know some nice tutorial about logging in ruby I would also
appreciate :wink: I mean logging events to some file, not logging users to
service :slight_smile:

Pawel Stawicki
A google search for this 'rails ip address of client' returned this:
which gave this:

ip_addr = request.env['REMOTE_ADDR']

Sorry, can't answer the second question :slight_smile:
6/22/2007 | 7:53 PM.

Just want to mention also that if you are behind a load balancer, the REMOTE_ADDR may return the load balancer’s IP, not the actual client’s. In this case, you can typically use something like:

ip_addr = request.env


Oops … sorry about the space, should be:

ip_addr = request.env[‘X_FORWARDED_FOR’]


Nice, I wasn’t aware of that. Thanks!


Im using request.remote_ip now but the problem is if im behind a proxy i
always get ip as

But in the development.log i can see the correct ip like this
Processing AuthController#login (for at 2007-07-05 11:31:17)

Is there a way to get the ip from the log or some other workaround?

Spit out the contents of request.env (a hash) and see if what you want is in there. Usually there's an X-header that will have the clients ip...