Socket.io with apache proxy

im trying to configure proxyng with apache to be able to use socket.io in my nodejs application. But I get this error on the client side:

WebSocket connection to 'wss://example.com/tools/socket.io/?EIO=3&transport=websocket&sid=eOGwJSC14TTWhHRMAAAR' failed: Error during WebSocket handshake: Unexpected response code: 400 

Here is my apache configuration:

  <VirtualHost *:443> ServerName example.com ServerAlias example.com #SSLRequireSSL SSLProtocol all -SSLv2 -SSLv3 SSLCompression off SSLHonorCipherOrder on SSLEngine on SSLCertificateFile /etc/ssl/main.pem SSLCertificateKeyFile /etc/ssl/dec_ssl.key SSLCertificateChainFile /etc/ssl/sub.class1.server.ca.pem SSLCACertificateFile /etc/ssl/main.pem SSLProxyEngine On ProxyPreserveHost On ProxyRequests off </VirtualHost> <Location /tools/> ProxyPass http://localhost:8181/ ProxyPassReverse http://localhost:8181/ </Location> 

And here is the client side code:

 socket = io.connect("https://example.com",{path:'/tools/socket.io'}); socket.on("connect", function () { console.log("socketio Connected to server!"); }); 

What else do I need to add to apache configuration to get rid of this error?

EDIT: My Apache Version: Server Version: Apache / 2.4.6 (CentOS)

+7
source share
1 answer

I have successfully used this with Apache 2.4.16, older versions may not work correctly.

 <VirtualHost *:443> ServerName example.com ServerAlias example.com #SSLRequireSSL SSLProtocol all -SSLv2 -SSLv3 SSLCompression off SSLHonorCipherOrder on SSLEngine on SSLCertificateFile /etc/ssl/main.pem SSLCertificateKeyFile /etc/ssl/dec_ssl.key SSLCertificateChainFile /etc/ssl/sub.class1.server.ca.pem SSLCACertificateFile /etc/ssl/main.pem SSLProxyEngine On ProxyPreserveHost On ProxyRequests off </VirtualHost> <Location /tools/> RewriteEngine On RewriteCond %{REQUEST_URI} ^/tools/socket.io [NC] RewriteCond %{QUERY_STRING} transport=websocket [NC] RewriteRule "^/tools/socket.io" "ws://localhost:8181/socket.io/" [P,L] ProxyPass http://localhost:8181/ ProxyPassReverse http://localhost:8181/ </Location> 

ProxyPass will not process websocket updates. So what you want to do is redirect these requests to ws: //

I think, in general, you should not use Location to handle this, but this config should work for you.

+4
source share

All Articles