简体   繁体   English

在使用socket.io进行生产的客户端上使用哪个IP地址?

[英]What IP address to use on client-side using socket.io for production?

The code that I am using right now is : 我现在使用的代码是:

    <!--Socket.io -->
    <script src='http://172.16.17.185:3000/socket.io/socket.io.js'></script>

Where, 172.16.17.185 is the IP address on our LAN on which my server runs and port 3000 is used for socket.io communication. 其中172.16.17.185是运行服务器的局域网中的IP地址,端口3000用于socket.io通信。

The examples on the socket.io website use localhost in place of the server IP address. socket.io网站上的示例使用本地主机代替服务器IP地址。 I started with this. 我从这个开始。 Using localhost, my page loaded fine on the server, but no other PC on the LAN was able to create the socket object (as it was trying to look for socket.io on its localhost, which wasnt there). 使用本地主机,我的页面在服务器上的加载情况很好,但是局域网上没有其他PC能够创建套接字对象(因为它试图在本地的localhost上寻找socket.io)。

So, I changed localhost to the IP address of the server, and everything worked fine. 因此,我将localhost更改为服务器的IP地址,并且一切正常。

Until, I tried accessing it from outside. 直到我尝试从外部访问它。 Since this IP address is valid only on the LAN, it doesnt work like it should on the internet. 由于此IP地址仅在LAN上有效,因此无法像在Internet上那样工作。

Is there a workaround for this problem ? 有解决此问题的方法吗? or do I need to find the public IP of the server and use it ? 还是我需要找到服务器的公共IP并使用它? Does this create any security vulnerabilities for the server as I dont have authorization ? 我没有授权,这会为服务器创建任何安全漏洞吗?

So far: http://www.ipsacademy.org/weather 到目前为止: http//www.ipsacademy.org/weather

Server code in web-server.js web-server.js中的服务器代码

If you want to access the socket.io server from outside you LAN, you must use public IP (and possibly avoid the use of port 3000). 如果要从LAN外部访问socket.io服务器,则必须使用公共IP(并可能避免使用端口3000)。

In a production environment you must use a fronted proxy (like nginx) to translate requests to your socket.io server to the private LAN address. 在生产环境中,您必须使用前端代理(例如nginx)将对socket.io服务器的请求转换为专用LAN地址。

Following example in the nginx blog you can use something like this: nginx博客中的以下示例中,您可以使用类似以下的内容:

location /socket.io/ {
    proxy_pass http://172.16.17.185:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";    
}

This will not cause any security problem different from any publicly accessible service on Internet. 这不会引起任何与Internet上的公共可访问服务不同的安全问题。

Looking at your code you are using separate http instances for serving page and socket.io. 查看您的代码,您正在使用单独的http实例来提供page和socket.io。 If you change that to use the same http server you can use relative URL for socket.io connection. 如果将其更改为使用相同的http服务器,则可以使用相对URL进行socket.io连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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