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

后端 未结 5 1902
猫巷女王i
猫巷女王i 2020-12-05 12:31

I have an apache proxy for a meteor app and apache and meteor are on two separate machines. I need it that way as apache has to serve a lot of real websites and it wouldn\'t

相关标签:
5条回答
  • 2020-12-05 12:43

    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

    0 讨论(0)
  • 2020-12-05 12:54

    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]
    
    0 讨论(0)
  • 2020-12-05 13:04

    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.

    0 讨论(0)
  • 2020-12-05 13:10

    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]
    
    0 讨论(0)
  • 2020-12-05 13:10

    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
    
    0 讨论(0)
提交回复
热议问题