NodeJs Error during WebSocket handshake

I am able to establish a websocket connection, from a clienJS to NodeJs. But fails to connect websocket, when the request pass through apache httpd.

Using Httpd2.4.7, I am getting the below error. Please let me know what needs to be corrected.

WebSocket connection to 'ws://172.27.38.86/socket.io/1/websocket/_uW8Sv7lgQfrZncTSzKu' failed: Error during WebSocket handshake: Unexpected response code: 502

Thanks & Regards

Jawahar

Answers


After 2.4.5, apache included a module called proxy_wstunnel on their trunk, not available yet on current ubuntu production versions of apache (2.4.7). It was somehow painful, but following roughly the steps listed on this blog-post I managed to install the module and use it successfully.

dpkg -s apache2 //this gives you the version of apache in my case "2.4.7-1ubuntu4.1"
//then you checkout that version of apache
svn co http://svn.apache.org/viewvc/httpd/httpd/tags/2.4.7/
//you get into the directory just checked out
cd 2.4.7
//in there you checkout the Apache Portable Runtime Project and utils
svn co http://svn.apache.org/repos/asf/apr/apr/branches/1.4.x srclib/apr
svn co http://svn.apache.org/repos/asf/apr/apr-util/branches/1.3.x srclib/apr-util
//you compile with the corresponding modules flags
./buildconf
./configure --enable-proxy=shared --enable-proxy_wstunnel=shared
make

//You copy the modules (mod_proxy and mod_proxy_wstunnel) to your apache working copy
//It could be advisable to backup the old mods first
sudo cp modules/proxy/.libs/mod_proxy{_wstunnel,}.so /usr/lib/apache2/modules/
sudo chmod 644 /usr/lib/apache2/modules/mod_proxy{_wstunnel,}.so
sudo echo -e "# Depends: proxy\nLoadModule proxy_wstunnel_module /usr/lib/apache2/modules/mod_proxy_wstunnel.so" | sudo tee -a /etc/apache2/mods-available/proxy_wstunnel.load

//you then enable your module and restart your apache... so now the module is ready to use
sudo a2enmod proxy_wstunnel
sudo service apache2 restart

and after all that, researching a little bit I found this page I configured my apache vhost file accordingly like this

<VirtualHost *:80>
    ServerAdmin yourmail@mail.com
    ServerName yoursubdomain.yourdomain.info
    Redirect permanent / https://yoursubdomain.yourdomain.info
</VirtualHost>

<VirtualHost *:443>
    ServerAdmin yourmail@mail.com
    ServerName yoursubdomain.yourdomain.info

    ProxyRequests off

        <Proxy *>
            Order deny,allow
            Allow from all
        </Proxy>

    ProxyPreserveHost On
//these next two lines are to enable the wstunnel
    ProxyPass /socket.io/1/websocket ws://localhost:9091/socket.io/1/websocket
    ProxyPassReverse /socket.io/1/websocket ws://localhost:9091/socket.io/1/websocket
//this line is to retrieve the socket.io.js to use
    ProxyPass /socket.io/ http://localhost:9091/socket.io/

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/yourcert.crt
    SSLCertificateKeyFile /etc/ssl/private/yourcert.key
    SSLCertificateChainFile /etc/ssl/certs/your_bundle.crt

    //your logs
    CustomLog /var/log/apache2/yoursubdomain.yourdomain.info.log combined
    ErrorLog /var/log/apache2/yoursubdomain.yourdomain.info.error.log
</VirtualHost>

And TaDaaa... I have a working node websocket responding on port 9091 through an apache proxy that gets everything trough the 443 standard ssl port.

I suppose ubuntu will soon include this module on their production version, but meanwhile this is the way to go


Apache SUCKS at handling websockets through proxies. I recommend either getting rid of the Apache layer, or modifying your socket.io settings to use XHR polling.


I had to setup ws tunnel in CentOs, where apache 2.2.15 was installed by default.

I have tried patching proxy_wstunnel module with apache 2.2.15. But no help. Finally I have decided to remove apache 2.2.15 and install apache 2.4 by following the official documentation from http://httpd.apache.org/docs/2.4/install.html

After installation (I have installed in the default location /usr/local/apache2/), I did the following to get tunneling work

  1. uncomment the following lines from /usr/local/apache2/conf/httpd.conf

    LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

  2. Add a virtual host for tunneling websocket requests


<VirtualHost *:80>
    ServerName subdomain.mydomain.com
    ProxyPass / ws://localhost:8081/
    ProxyPassReverse / ws://localhost:8081/
</VirtualHost>

from my frontend the websocket is connected via ws://subdomain.mydomain.com:80

  1. restart apache using sudo /usr/local/apache2/bin/apachectl -k restart

Need Your Help

Using a System.Diagnostics.Process to Launch a Third Party Application

c# process singleton

All, I am developing an application that needs to launch another application at run-time. To launch the third-party app I am using System.Diagnostics.Process and to ensure I never launch the third-...

Can't add css in Magento

php html css magento xhtml

Have a custom template for my home page placed in mytheme/layout/page/customhome.phtml