How to set up an apache proxy for Meteor/SockJS and WebSocket?
This answer is based on Fatih's answer. His solution fails for browsers that send a connection request header other than "Upgrade", such as "keep-alive, Upgrade". This was the case for me with Firefox 42.
To tackle the issue for Firefox also, change the apache RewriteCond as follows:
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule .* ws://localhost:3000%{REQUEST_URI} [P]
(^Upgrade$ becomes Upgrade$)
I wanted to put this as a comment to Fatih's answer, however I lack the necessary reputation.
We use this for Apache and a SockJS app behind Apache. Apache is doing WebSocket proxy automatically, but you have to rewrite the scheme to ws otherwise it fallbacks to XHR. But only if the connection is a WebSocket handshake. Adding the following will fix your problem :) (note: change the localhost:3000
accordingly to your own backend url.
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^websocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade [NC]
RewriteRule .* ws://localhost:3000%{REQUEST_URI} [P]
After reading several answers, posting on the Meteor forum, and a lot of trials here is the whole enchilada that worked for me. The other answers were somewhat incomplete, or at least didn't work for me.
I had to do:
sudo a2enmod proxy_wstunnel
Also had to add a ProxyPass and ProxyPassReverse and changed ^Upgrade$ to Upgrade$ from another SO answer.
<VirtualHost *:80>
ServerName some-domain.com
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule .* ws://localhost:3000%{REQUEST_URI} [P]
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
then restart Apache.
I checked on the console and there is no error now and no xhr requests. So I assume it's working correctly