簡體   English   中英

將 ip:port 地址映射到沒有端口的域后,無法通過 socket.io 連接到節點服務器

[英]cant connect to node server via socket.io after mapping ip:port address to a domain withouth port

我對 nodejs 很陌生,所以對我很陌生

所以我有一個 nodejs 服務器,我用來通過 socket.io 將實時數據注入我的 php/laravel 網站,我在端口 3002 中運行我的 nodejs 服務器,而 apache 正在使用端口 80

所以我的節點服務器正在運行

http://78.47.222.225:3002

所以我注意到使用 vpn 或代理的客戶端無法連接到我的節點服務器地址並且 websocket 連接失敗

在提出這個問題后,有人建議它的 bcuz vpn 是防火牆不尋常的端口

當客戶端使用 vpn 或代理時,nodejs 服務器不響應

所以我將地址映射到 3002 端口,所以基本上

http://example.com/server ---代理---> http://78.47.222.225:3002

所以現在當我去http://example.com/server我得到

Hello World!

這意味着它可以正常工作,但是在我的主頁http://example.com當我嘗試通過 socket.io 連接到服務器時它失敗了...主頁源

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js" ></script>

</head>
<body>
<script>

    const  socket = io.connect("http://example.com/server" ,  {transports:['websocket']});

    socket.on('connect', function () {
        
        console.log('connected!!!') ; 
        
    });

</script>

</body>
</html>

不知道為什么會這樣,但在我的控制台中我看到它試圖連接到這個地址

WebSocket connection to 'ws://example.com/socket.io/?EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 301
r.doOpen @ index.js:83

但它不應該嘗試example.com/server/socket.io嗎? 考慮到我給了http://example.com/server io.connect ?

當我嘗試通過 ip:port 連接時它工作正常...嘗試通過 ip:port 而不是代理地址連接這個地址

http://example.com?port=1

const  socket = io.connect("http://78.47.222.225:3002" ,  {transports:['websocket']});

socket.on('connect', function () {
    
    console.log('connected!!!') ; 
    
});

基本上

io.connect("http://example.com/server")

嘗試連接到

ws://example.com/socket.io

即使服務器使用這個地址example.com/server也會失敗......我認為它應該連接到ws://example.com/server/socket.io

io.connect("http://78.47.222.225:3002")

將連接到

ws://78.47.222.225:3002/socket.io

它有效!

代理設置

<VirtualHost xxx.xxx.xxx.xxx:443 >
.....
.....

<Location /server>
            ProxyPass http://localhost:3002/
            ProxyPassReverse http://localhost:3002/
    </Location>

</VirtualHost>

您的代理設置指示端口 443 上的 VHost,這是 SSL 的默認端口。

但是您對bia2roll.com的請求是不安全的(http:// 或 ws://)。

通過添加 SSL(讓我們加密)並使用 https 或 wss 調用它來保護您的服務器,或者通過在端口 80 上綁定來取消保護您的服務器

您的代理設置似乎有問題:

你可以試試這個: WebSockets 和 Apache 代理:如何配置 mod_proxy_wstunnel?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM