Ruby on rails 在某些情况下,远程ip返回的ip与whatismyip.com不同

Ruby on rails 在某些情况下,远程ip返回的ip与whatismyip.com不同,ruby-on-rails,ip-address,Ruby On Rails,Ip Address,我正在尝试为特定控制器设置一些IP白名单,我依赖于请求返回的值。远程\u IP def country_whitelist request_country = Custom::BackofficeLibrary.get_country_by_ip(request.remote_ip) head 404 unless ['PE'].include? request_country end 我的问题是,对于某些设备/网络,request.remote\u IP返回的IP地址是错误的 例如,

我正在尝试为特定控制器设置一些IP白名单,我依赖于
请求返回的值。远程\u IP

def country_whitelist
  request_country = Custom::BackofficeLibrary.get_country_by_ip(request.remote_ip)
  head 404 unless ['PE'].include? request_country
end
我的问题是,对于某些设备/网络,
request.remote\u IP
返回的IP地址是错误的

例如,使用我手机的移动网络,当我在秘鲁时,我正在为美国获得一个IP。但是,如果我在手机上浏览到或,我会得到正确的IP地址

因此,在某些情况下(十分之一),当免费互联网服务正确时,远程ip无法获得正确的ip

我的设置是AWS EC2->Nginx->Puma->Rails 5.0.0.1

我试过AWS,前面有没有负载平衡器,都没有区别

有什么想法吗

编辑:

在相关情况下添加Ngnix部分配置:

server {
  listen 80;
  server_name localhost;

  root /home/deploy/backoffice/current/public;

  try_files $uri/index.html $uri @app;

  location / {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    proxy_pass http://app;
  }