I had a hard time connecting Socket.io via Apache Reverse Proxy, therefore here is a practical example of what works for me. The configuration was needed in the development of our web-chat interface for Zana A.I Companion.

Normally once you start Socket.io you have a port and an IP.
The example below is taken by the socket.io demo example. It assumes you have a running socket application. The server is started at port 8001 and there is an index.html that is launched for the client. The snippet below is the client, found at index.html

$(function () {
//What works through a direct call to the NodeJS
var socket = io.connect('http://dev-ip-machine:8001/');
//What you ideally want to have
//var socket = io.connect('http://yourpublicDomain.com/');
socket.emit('chat message', $('#m').val());
return false;
socket.on('chat message', function(msg){

  • ').text(msg));
  • Basically the client defines a socket server instance to connect too.
    In order to have the local socket server accessed behind an Apache Reverse Proxy you will need first to activate the following apache modules.

    sudo a2enmod proxy_http
    sudo a2enmod proxy_fcgi
    sudo a2enmod proxy_wstunnel

    Most examples I saw around where missing a reference to proxy_wstunnel which is the “Websockets support module for mod_proxy”

    Once you have the necessary modules, the next step is to configure the Apache Reverse Proxy in the correct way.

    In your VirtualHost configuration, below the ServerName and Server Aliases you should first of all define a forward rule for the socket.io.

    The second step is to create Proxy Rules, in my case I call my socket application under /chat path. Feel free to change the path as you like.
    The rule basically defines that if a user goes to yourpublicDomain.com/chat, he will be reaching the client index.html served by your node.js application

    ServerName yourpublicDomain.com
    RewriteEngine On
    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


    Change the index.html client to point to your public domain

    var socket = io.connect('http://yourpublicDomain.com/');
    and you are good to go.

    Posted by xpo6

    Software developer in the realm of AI, NLP and black magic.