在 socket.io 中使用代理时未获取远程地址 [英] Not getting remote address while using proxy in socket.io

查看:49
本文介绍了在 socket.io 中使用代理时未获取远程地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 socket.io 代码中,

In my socket.io code,

socket.sockets.on('connection', function(client){ 
    var ip = client.handshake.address.address;
    ..
}

ip 总是返回 127.0.0.1,这是因为服务器位于代理后面.如何正确获取远程地址?

ip always returns 127.0.0.1 and this is because the server sits behind a proxy. How do I get remote address properly ?

我正在使用 http-proxy

I am using http-proxy

推荐答案

是的,这对我有用.

client.handshake.headers['x-forwarded-for'] || client.handshake.address.address;

我正确获取远程 IP 地址而不是 127.0.0.1

I am properly getting the remote IP address and not 127.0.0.1

这篇关于在 socket.io 中使用代理时未获取远程地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆