How to connect socket.io through a reverse proxy

Thijs Koerselman picture Thijs Koerselman · Apr 7, 2014 · Viewed 14.6k times · Source

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

?

Answer

yed picture yed · Apr 7, 2014

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