如何在 Azure Functions node.js 中获取客户端 IP 地址?

How to get client IP address in Azure Functions node.js?

我在 node.js 上编写了一个 Azure 函数。我如何检索调用该函数的客户端的 IP 地址?

到目前为止我发现了什么:

  1. 同一个问题的 ,但使用的是 C#。
  2. 可以从headers读取:

    module.exports = function (context, req) {
        var ip = req.headers['x-forwarded-for']    
    }
    

这样获取ip靠谱吗,因为在调用函数的途中很容易改?

是的,它是可靠的,因为 Azure Web 服务器将覆盖 x-forwarded-for,因为它知道它是从负载均衡器转发的。

如果您正在使用 express,请考虑设置 app.set('trust proxy');

有关详细信息,请查看 express manual page on 'Express behing proxies'