Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect socket.io through a reverse proxy

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

?

like image 859
Thijs Koerselman Avatar asked Apr 07 '14 17:04

Thijs Koerselman


1 Answers

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
like image 106
Al-Punk Avatar answered Nov 05 '22 11:11

Al-Punk