简体   繁体   中英

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

I have an Azure function written on node.js. How could I retrieved an IP address of a client that called the function?

What I've found so far:

  1. An answer to the same question, but using C#.
  2. It is possible to read it from headers:

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

Is it reliable to get the ip this way, since it can be easily changed on the way to the function?

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

If you're using express , consider setting app.set('trust proxy');

For more information, check the express manual page on 'Express behing proxies'

I came across this question while looking for how to determine the client IP address. Did a quick test and it seems this is no longer correct. Azure happily forwards anything I put in the x-forwarded-for header to the node server. It does appear to append the correct IPs on the end however.

From my research it seems that "client-ip" is not forwarded and does contain the correct IP, but I've not been able to confirm this yet.

Screenshot below is from Postman where I've provided x-forwarded-for and client-ip headers and the returned body is a simple dump of all headers from a nodeJs express application.

app.get("/headers", (req, res) => {
  res.send(req.headers)
})

在此处输入图片说明

x-forwarded-for does contain the client ip, but also the one I specified. client-ip however does not. I've not been able to find any documentation on this yet. I'll try to update this answer with a sourced link if I can find one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM