如何获取Meteor服务器中的用户IP地址?

sar*_*ata 26 meteor

我想在服务器端的meteor应用程序中获取用户IP地址,这样我就可以用一堆东西记录IP地址(例如:订阅邮件列表的非注册用户,或者只是做任何事情重要的).

我知道当涉及反向代理时,服务器"看到"的IP地址可能与实际源地址不同.在这种情况下,X-Forwarded-For应解析标头以获取用户的真实公共IP地址.请注意,解析X-Forwarded-For不应该是自动的(有关潜在安全问题的讨论,请参阅http://www.openinfo.co.uk/apache/index.html).

外部参考:这个问题出现在2012年8月流星谈话邮件列表中(没有提供解决方案).

Flo*_*bre 30

1 - 如果没有http请求,在函数中你应该能够获得clientIP:

clientIP = this.connection.clientAddress;
//EX: you declare a submitForm function with Meteor.methods and 
//you call it from the client with Meteor.call().
//In submitForm function you will have access to the client address as above
Run Code Online (Sandbox Code Playgroud)

2 - 使用http请求并使用iron-router及其Router.map函数:

在目标路线的动作功能中使用:

clientIp = this.request.connection.remoteAddress;
Run Code Online (Sandbox Code Playgroud)

3 - 使用Meteor.onConnection功能:

Meteor.onConnection(function(conn) {
    console.log(conn.clientAddress);
});
Run Code Online (Sandbox Code Playgroud)

  • 请记住,如果您在代理服务器后面,这些将不起作用.在这种情况下,您可以尝试类似`this.connection.httpHeaders ['x-real-ip']`或您的代理添加到数据包的任何标头.这适用于`nginx`后面的我 (5认同)
  • 我在nginx代理后面运行.我必须在我的nginx配置中设置`proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;`在`location/{`以及`process.env.HTTP_FORWARDED_COUNT = 2;`作为Meteor环境值,而不是在流星文档.我也对这个'Meteor.onConnection(函数(conn){var forwardedFor = conn.httpHeaders ['x-forwarded-for'].split(","); console.log(forwardedFor [0]); });` (4认同)