WebSockets and Apache proxy : how to configure mod_proxy_wstunnel? WebSockets and Apache proxy : how to configure mod_proxy_wstunnel? apache apache

WebSockets and Apache proxy : how to configure mod_proxy_wstunnel?


I finally managed to do it, thanks to this topic.

TODO:

1) Have Apache 2.4 installed (doesn't work with 2.2), and do:

a2enmod proxya2enmod proxy_httpa2enmod proxy_wstunnel

2) Have nodejs running on port 3001

3) Do this in the Apache config

<VirtualHost *:80>  ServerName www.domain2.com  RewriteEngine On  RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]  RewriteCond %{QUERY_STRING} transport=websocket    [NC]  RewriteRule /(.*)           ws://localhost:3001/$1 [P,L]  ProxyPass / http://localhost:3001/  ProxyPassReverse / http://localhost:3001/</VirtualHost>

Note: if you have more than one service on the same server that uses websockets, you might want to do this to separate them.


Instead of filtering by URL, you can also filter by HTTP header. This configuration will work for any web applications that use websockets, also if they are not using socket.io:

<VirtualHost *:80>  ServerName www.domain2.com  RewriteEngine On  RewriteCond %{HTTP:Upgrade} =websocket [NC]  RewriteRule /(.*)           ws://localhost:3001/$1 [P,L]  RewriteCond %{HTTP:Upgrade} !=websocket [NC]  RewriteRule /(.*)           http://localhost:3001/$1 [P,L]  ProxyPassReverse / http://localhost:3001/</VirtualHost>


May be will be useful.Just all queries send via ws to node

<VirtualHost *:80>  ServerName www.domain2.com  <Location "/">    ProxyPass "ws://localhost:3001/"  </Location></VirtualHost>