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

eth*_*han 8 networking web-services ruby-on-rails nginx ip-address

我的服务器没有公共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;
}
Run Code Online (Sandbox Code Playgroud)

在我的Rails应用程序的控制器中,request.iprequest.remote_ip返回我的服务器的网关地址.

我怎样才能获得客户的真实IP?

如何从Rails请求中获取X-Forwarded-For值?

Kev*_*vin 21

Rails应该是为我们自动完成的,但它似乎被当前的3.x打破了

我正在使用这个:

def ip() request.env['HTTP_X_FORWARDED_FOR'] || request.remote_ip end
Run Code Online (Sandbox Code Playgroud)


Riz*_*rif 6

您应该获得标头值X-forwarded-for

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

  • 您的网关可能正在做S​​ource Nat.nginx在哪里安装?在网关或与网关相同的机器或与apache相同的机器上? (2认同)
  • 我不明白.问题是否表明他试图获得X-Forwarded-For标题值,但答案表明他应该这样做.这个问题的尝试是错误的吗? (2认同)