How to connect socket.io through a reverse proxy

18,352

Solution 1

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'});

Solution 2

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
Share:
18,352
Thijs Koerselman
Author by

Thijs Koerselman

Updated on June 06, 2022

Comments

  • Thijs Koerselman
    Thijs Koerselman almost 2 years

    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

    ?

  • Thijs Koerselman
    Thijs Koerselman about 10 years
    OK now it tries to connect to what seems to be the correct url. The handshake is made, but the request (socket.io/1?t=123983759) status is now "cancelled". If I use the same url in the browser it simply returns something like "xrf021YGWWEF5NUHoyTD:60:60:websocket" without problems. I don't know what's going on.
  • Thijs Koerselman
    Thijs Koerselman about 10 years
    With "handshake is made" I mean nodejs prints "info - handshake authorized 05T-Uzt9CmK4SrN8sPQJ"
  • Thijs Koerselman
    Thijs Koerselman almost 10 years
    I got it to work, but can't remember why I ran into the cancelled issue. I think it had to do with my settings for the reverse proxy and not the socket.io settings. For socket.io version 1.0 the resource option has been replaced by 'path'. Found this to be helpful blog.seafuj.com/migrating-to-socketio-1-0
  • Egidi
    Egidi over 9 years
    this is not wirking for me... still trying to get example.com/socket.io....
  • edwardsmarkf
    edwardsmarkf over 4 years
    has resource been changed to "path"? socket.io/docs/client-api -- your excellent suggestion lead me to this solution. note that i was able to eliminate the "localhost:8888" part. thank you very much for sharing.