使用Rails和Nginx获取客户端的真实IP地址?

问题描述

| 我的服务器没有公用IP地址,所以我不知道如何获取真实客户端的IP地址。 这是我的Nginx的配置:
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;
}
在我的Rails应用程序的控制器中,ѭ1和
request.remote_ip
都返回我服务器的网关地址。 如何获得客户的真实IP? 如何从Rails请求中获取X-Forwarded-For值?     

解决方法

您应该获得标头值X-forwarded-for http://en.wikipedia.org/wiki/X-Forwarded-For     ,Rails原本应该为我们自动执行此操作,但是似乎在当前的3.x版本中已被破坏 我正在使用这个:
def ip() request.env[\'HTTP_X_FORWARDED_FOR\'] || request.remote_ip end