【问题标题】:Wrong IP-Address with nginx + Unicorn + railsnginx + Unicorn + rails 的 IP 地址错误
【发布时间】:2012-05-29 10:38:06
【问题描述】:

我用

检查控制器中的IP地址
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;
}

【问题讨论】:

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


    【解决方案1】:

    我也遇到过这个问题;我找到了这个问题,但其他答案对我没有帮助。

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

    proxy_set_header CLIENT_IP $remote_addr;
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案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】:

        对于 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;
          }
          ...
        

        【讨论】:

        • 谢谢!遇到应用程序接收“独角兽”作为 HTTP_HOST 的问题,因此在重定向期间 Rails 重定向到 http://unicorn/postshttp://MY_SERVER/posts,这解决了我的问题。
        • @todd Blopboard 链接无效
        【解决方案5】:

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

        我在查看 actiondispatch 代码 remote_ip.rb 源后找到的解决方案。现在我在我的设计/看守进程以及我正在查看 request.remote_ip 的任何其他例程中获得了正确的 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} " +
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-05
          • 2017-01-07
          相关资源
          最近更新 更多