【问题标题】:Access client's IP address (not the load balancer's) from Meteor, with Modulus使用 Modulus 从 Meteor 访问客户端的 IP 地址(不是负载均衡器的)
【发布时间】:2016-04-05 21:49:30
【问题描述】:

我有一个托管在 modulus.io 上的 https Meteor webapp。按照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 地址而不是负载均衡器的 IP 地址?

谢谢!

【问题讨论】:

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


    【解决方案1】:

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

    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;
          }
       }
    });
    

    【讨论】:

    • 请注意,X-Forwarded-For 标头可以是 IP 地址的逗号分隔列表(实际上是逗号+空格分隔的列表),其中第一个地址是原始客户端的地址。
    猜你喜欢
    • 2021-11-07
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多