How to set up an apache proxy for Meteor/SockJS and WebSocket?

自作多情 提交于 2019-11-28 20:55:03
Fatih Arslan

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]

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.

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

I wish I were able to provide you a direct reply with apache instructions but since you have mentioned nginx and that the fact is, it is hard to configure, I'd like to weigh in with an alternative that actually uses nginx but shields you from all the complexities.

The tutorial at https://github.com/phusion/passenger/wiki/Phusion-Passenger:-Meteor-tutorial walks through the steps to set up Phusion Passenger with or without nginx (it internally uses nginx anyway) for multi-instance production Meteor deployments that can scale up to utilize all cores in your server.

It is as easy as:

$ cd meteor-app-directory
$ mkdir public tmp
$ passenger start

Fatih-Arslan's answer with Derwiwie's amendment worked like charm. One thing I had to use was putting wss instead of ws, because my service works only in https.

RewriteEngine on  
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]  
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]  
RewriteRule .* wss://localhost:3000%{REQUEST_URI} [P]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!