Question

I'm trying to connect to a socket.io server from behind an apache reverse proxy. I have apache running on port 8888. The nodejs server is running on the same machine on port 9096. For testing the proxy is configured on my local machine like this:

ProxyPass /some/path http://localhost:9096
ProxyPassReverse /some/path http://localhost:9096

In the client code I do something like this:

var socketUrl = 'http://localhost:8888/some/path/namespace';
var socket = io.connect(socketUrl);

This results in the following behavior.

First my client requests the socket.io.js script at:

http://localhost:8888/some/path/socket.io/socket.io.js
-> 200 ok

Then the socket tries to connect at:

localhost:8888/socket.io/1?123983759
-> 404 not found

I have found the "resource" configuration for socket.io, but this only seems to set to where the socket.io.js script is fetched from, but not the url it's trying to connect to. It always seems to connect to the root of the client origin.

How could I make it connect to localhost:8888/some/path/socket.io/1?123983759

?

Was it helpful?

Solution

In your client code you have to set the base path with the resource option, like so:

var socket = io.connect('http://localhost:8888', {resource: '/some/path/socket.io'});

OTHER TIPS

Had this issue myself, The example by YED is still pointing out to a solution which connects to the nodeJs directly and not via the Reverse Proxy.

Normally you want index.html to connect over the Apache Reverse Proxy and not directly. An example is provided at Socket.io via Apache Reverse Proxy

basically, you have to enable proxy_wstunnel as well and add the following to the your virtual host configuration

RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
RewriteCond %{QUERY_STRING} transport=websocket    [NC]
RewriteRule /(.*)           ws://dev-ip-machine:8001/$1 [P,L]

ProxyPass /chat http://dev-ip-machine:8001
ProxyPassReverse /chat http://dev-ip-machine:8001
ProxyPass        /socket.io  http://dev-ip-machine:8001/socket.io
ProxyPassReverse /socket.io  http://dev-ip-machine:8001/socket.io
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top