gpt4 book ai didi

node.js - 在 Node 前面使用nginx服务器作为代理时如何获取客户端真实IP

转载 作者:搜寻专家 更新时间:2023-11-01 00:37:08 29 4
gpt4 key购买 nike

我需要在 nodeJs 中获取客户端的真实 IP 地址,当使用 nginx 代理服务器时,我总是获取本地主机 (127.0.0.1)。谁能帮我解决这个问题?这是我的代码

app.set('trust proxy', 'loopback');
app.use(function(req, res, next) {
app.ipInfo = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
next();
});

最佳答案

在 nginx 中,如果你想将远程用户的 IP 地址传递给你的后端 Web 服务器,你必须将 X-Forwarded-For header 设置为该远程 ip,如下所示:

proxy_set_header X-Forwarded-For $remote_addr;

关于node.js - 在 Node 前面使用nginx服务器作为代理时如何获取客户端真实IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47572601/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com