node.jsapachesocket.io

Running Socket.io over Apache Reverse Proxy


I am trying to run NodeJS behind Apache and so far I am stuck with Socket.io issue.

I have no issue accessing the application directly, but whenever I accessed through my domain, I get this error thrown from socket.io:

Firefox can’t establish a connection to the server at wss://example.com/socket.io/?EIO=3&transport=websocket&sid=X-hLU73t7ojk2zoRAAAB.

My Apache configuration is as follows:

     <VirtualHost _default_:443>
            ServerName example.com

            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined

            SSLEngine on

            SSLCertificateFile      /etc/ssl/certs/apache-selfsigned.crt
            SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key


            ProxyRequests off
            ProxyVia on

            RewriteEngine On
            RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
            RewriteCond %{QUERY_STRING} transport=websocket    [NC]
            RewriteRule /(.*)           ws://localhost:8080/$1 [P,L]

            ProxyPass        /socket.io http://localhost:8080/socket.io
            ProxyPassReverse /socket.io http://localhost:8080/socket.io

            <Location />
                ProxyPass http://127.0.0.1:8080/
                ProxyPassReverse http://127.0.0.1:8080/
            </Location>
            #ProxyPass / http://localhost:8080/
            #ProxyPassReverse / http://localhost:8080/

            # BrowserMatch "MSIE [2-6]" \
            #               nokeepalive ssl-unclean-shutdown \
            #               downgrade-1.0 force-response-1.0

            BrowserMatch "MSIE [2-6]" \
                           nokeepalive ssl-unclean-shutdown \
                           downgrade-1.0 force-response-1.0

    </VirtualHost>

I have also tried changing the RewriteRule /(.*) to wss://localhost:8080/$1 [P,L] but still thrown the same error. Can't seem to find any other answer to solve this.

I believe I am using socket.io 2.0, and on the client side it is connected as such:

var socket = io();

This is what bugs me,

enter image description here

It seems that some of the connection is going through but one is not.


Solution

  • Look into setting up a connection upgrade in Apache. That's what I needed to config nginx. Also, those http requests could be socketio serving the client file.

    Also look into this: http://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html

    Edit:

    The issue was solved with passing the ws/wss proxy as stated in mod_proxy_wstunnel. The apache virtual host config should have this:

             ProxyPass /socket.io/ ws://localhost:8080/socket.io
             ProxyPassReverse /socket.io/ ws://localhost:8080/socket.io
    

    Instead of this:

            ProxyPass /socket.io http://localhost:8080/socket.io
            ProxyPassReverse /socket.io http://localhost:8080/socket.io