What's the difference between request.remote_ip and request.ip in Rails?

Minqi Pan picture Minqi Pan · Jun 12, 2012 · Viewed 47.4k times · Source

As the title goes, you can get the client's ip with both methods. I wonder if there is any differences. Thank you.

in the source code there goes

"/usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/action _dispatch/http/request.rb" 257L, 8741C

def ip
  @ip ||= super
end

# Originating IP address, usually set by the RemoteIp middleware.
def remote_ip
  @remote_ip ||= (@env["action_dispatch.remote_ip"] || ip).to_s
end

but I really don't know the implications.

Answer

Clowerweb picture Clowerweb · Jun 12, 2012

request.ip returns the client ip even if that client is a proxy.

request.remote_ip is smarter and gets the actual client ip. This can only be done if the all the proxies along the way set the X-Forwarded-For header.