ethan
ethan

Reputation: 612

Get the real IP address of client with Rails and Nginx?

My server doesn't have a public IP address, so I don't know how to get the real client's IP address.

This is my nginx's configuration:

location / {
    proxy_pass http://domain1;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP     $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
}

In my Rails app's controller both request.ip and request.remote_ip return my server's gateway address.

How can i get the real IP of client?

How to get X-Forwarded-For value from Rails request?

Upvotes: 8

Views: 15942

Answers (2)

Kevin
Kevin

Reputation: 4307

Rails was supposed to be doing it automatically for us, but it seems to be broken with current 3.x

I'm using this:

def ip() request.env['HTTP_X_FORWARDED_FOR'] || request.remote_ip end

Upvotes: 21

Rizwan Sharif
Rizwan Sharif

Reputation: 1119

You should get the header value X-forwarded-for

http://en.wikipedia.org/wiki/X-Forwarded-For

Upvotes: 6

Related Questions