使用模数从Meteor访问客户端的IP地址(不是负载均衡器)

时间:2016-01-01 08:21:37

标签: meteor ip-address load-balancing modulus.io

我在modulus.io上托管了https Meteor网络应用程序。遵循建议here我有一个服务器方法:

Meteor.methods({
    printIP: function() {
        return this.connection.clientAddress;
    }
});

我在实时网站上的浏览器控制台中调用此方法:

Meteor.call('printIP', function(err, ip) { console.log(ip); })

但这总是会返回Modulus的load balancer的IP地址,54.236.216.66。

如何访问客户端的IP地址而不是负载均衡器?

谢谢!

1 个答案:

答案 0 :(得分:4)

通过一些实验,我找到了一个解决方案:

Meteor.methods({
   printIP: function() {
      if (this.connection.httpHeaders && this.connection.httpHeaders['x-forwarded-for']) {
         return this.connection.httpHeaders['x-forwarded-for'];
      } else {
         return this.connection.clientAddress;
      }
   }
});