使用nginx + Unicorn + rails的IP地址错误

时间:2012-05-22 21:31:26

标签: ruby-on-rails-3 nginx unicorn

我用

检查控制器中的ip-address
request.env['REMOTE_ADDR']

这在我的测试环境中运行良好。 但是在使用nginx + unicorn的生产服务器上,我总是得到127.0.0.1

这是我对网站的nginx配置:

  upstream unicorn {
  server unix:/tmp/unicorn.urlshorter.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  # server_name example.com;
  root /home/deployer/apps/urlshorter/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

5 个答案:

答案 0 :(得分:17)

我也遇到了麻烦;我发现了这个问题,但另一个答案对我没有帮助。

我查看了Rails 3.2.8的Rack :: Request#ip的实现,看看它是如何决定说的;让它使用通过环境传递的地址而不过滤掉我本地网络中的地址(它试图过滤出中间代理,但这不是我想要的),我还必须从我的nginx代理配置块中设置HTTP_CLIENT_IP你已经得到了什么(X-Forwarded-For也必须在那里工作!):<​​/ p>

proxy_set_header CLIENT_IP $remote_addr;

答案 1 :(得分:6)

如果你使用request.remote_addr,你将获得你的Nginx代理。

要获取用户的真实IP地址,您可以使用request.remote_ip

根据Rails的源代码,它会检查各种http标头,以便为您提供最相关的标题:in Rails 3.2Rails 4.0.0.beta1

答案 2 :(得分:4)

对于ELB - nginx - rails,您想要遵循本指南:

http://engineering.blopboard.com/resolving-real-client-ip-with-amazon-elb-nginx-and-php-fpm

请参阅:

server {
   listen 443 ssl spdy proxy_protocol;

   set_real_ip_from 10.0.0.0/8;
   real_ip_header proxy_protocol;

  location /xxx {
    proxy_http_version 1.1;
    proxy_pass <api-endpoint>;
    proxy_set_header      Host              $http_host;
    proxy_set_header      X-Forwarded-By    $server_addr:$server_port;
    proxy_set_header      X-Forwarded-For   $remote_addr;
    proxy_set_header      X-Forwarded-Proto $scheme;
    proxy_set_header      X-Real-IP         $remote_addr;
    proxy_set_header      CLIENT_IP         $remote_addr;
    proxy_pass_request_headers on;
  }
  ...

答案 3 :(得分:3)

答案在你的配置文件中:)以下应该做你想做的事:

real_ip = request.headers["X-Real-IP"]

更多信息:http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-headers

更新正确的答案出现在另一个问题中:

https://stackoverflow.com/a/4465588

或在此主题中:

https://stackoverflow.com/a/15883610

扰流器:

使用request.remote_ip

答案 4 :(得分:3)

proxy_set_header CLIENT_IP $remote_addr;对我不起作用。这是做了..

我在查看actiondispatch代码remote_ip.rb源后找到的解决方案。现在我在我的设计/监督过程以及我正在查看请求的任何其他例程中获得适当的IP .remote_ip

我的配置...... Ruby 2.2.1 - Rails 4.2.1 - NGINX v1.8.0 - Unicorn v4.9.0 - Devise v3.4.1

nginx.conf

HTTP_CLIENT_IP与CLIENT_IP

location @unicorn {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header HTTP_CLIENT_IP $remote_addr;  <-----
  proxy_redirect off;
  proxy_pass http://unicorn;
}

源actionpack-4.2.1 / lib / action_dispatch / middleware / remote_ip.rb

第114行:

client_ips    = ips_from('HTTP_CLIENT_IP').reverse

第126行:

"HTTP_CLIENT_IP=#{@env['HTTP_CLIENT_IP'].inspect} " +