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:
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.