0

im very new to nodejs so bare with me

so i have a nodejs server which i use to inject live data to my php/laravel website via socket.io i run my nodejs server in port 3002 and apache is using port 80

so my node server is running on

http://78.47.222.225:3002

so i noticed clients who use vpn or proxy cant connect to my nodeserver address and websocket connection fails for them

after asking in this question it was suggested its bcuz vpn is fire walling unusual ports

nodejs server doesn't respond when clients use vpn or proxy

so i maped a address to 3002 port , so basically

http://example.com/server ---proxying---> http://78.47.222.225:3002

so now when i go to http://example.com/server i get

Hello World!

which means its working but in my homepage http://example.com when i try to connect to the server via socket.io it fails ... homepage source

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

not sure why is this happening but in my console i see it trying to connect to this address

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

but shouldn't it try the example.com/server/socket.io ? considering i gave http://example.com/server to io.connect ?

it works fine when i try connecting via ip:port ... try this address to connect via ip:port instead of proxy address

http://example.com?port=1

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

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

basically

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

tries to connect to

ws://example.com/socket.io

which will fail even tho server works with this address example.com/server ... i think it should connect to ws://example.com/server/socket.io ?

but

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

will connect to

ws://78.47.222.225:3002/socket.io

and it works !

proxy setting

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

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

</VirtualHost>
hretic
  • 999
  • 9
  • 36
  • 78
  • please share your apache proxy settings – Chandan Oct 20 '20 at 16:53
  • @Chandan i've added the setting – hretic Oct 20 '20 at 18:52
  • Can you please add your whole config ? Also how/why do you proxy that/this: `http://bia2roll.com/server` to a public ip: `http://78.47.222.225:3002`. Regardingless, take a look here on the "path" option: https://socket.io/docs/server-api/ – Marc Oct 20 '20 at 19:11
  • @hretic try this https://stackoverflow.com/questions/27526281/websockets-and-apache-proxy-how-to-configure-mod-proxy-wstunnel – Chandan Oct 21 '20 at 07:56
  • @Marc whats wrong with proxy-ing address to public ip ? if your asking why im doing it as i explained in the question is to avoid getting blocked by vpn users – hretic Oct 21 '20 at 08:24
  • @Chandan pleas post it as answer so i can accept it – hretic Oct 21 '20 at 17:32
  • @hretic I have posted it as answer - thanks – Chandan Oct 22 '20 at 05:19

2 Answers2

1

Your proxy setting indicates a VHost on port 443, which is default port for SSL.

But your requests with bia2roll.com are unsecure (http:// or ws://).

Secure your server by adding SSL (Let's encrypt) and call it with https or wss or Unsecure your server by binding on the port 80

Romain V...
  • 423
  • 2
  • 10
0

There seems to problem with your proxy settings:

You can try this: WebSockets and Apache proxy : how to configure mod_proxy_wstunnel?

Chandan
  • 11,465
  • 1
  • 6
  • 25