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
?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With