如何为Meteor/SockJS和WebSocket设置apache代理?

Jey*_*ork 7 apache proxy websocket meteor sockjs

我有一个apache代理用于流星应用程序,apache和meteor在两台独立的机器上.我需要这样,因为apache必须服务于很多真实的网站,由于资源有限,在这台机器上安装meteor应用程序并不是一个好主意.

但是,如果我尝试通过代理从外部连接,则WebSocket握手失败,响应代码为400"只能升级到websocket".当我从局域网内直接连接到流星机器时,一切正常.当WebSocket失败时,SockJS/Meteor会重新回到XHR,但不幸的是,这会在应用程序中出现一些错误.所以我真的需要WebSocket才能在大多数情况下工作.

我用这里提到的补丁修补了我的apache安装:https://stackoverflow.com/a/16998664 看起来它运行良好但没有改变......

我的apache代理指令目前如下:

ProxyRequests Off
ProxyPreserveHost On
ModPagespeed Off
<proxy>
Order deny,allow
Allow from all
</proxy>
ProxyPass / http://10.0.2.6:3000/
ProxyPassReverse / http://10.0.2.6:3000/
Run Code Online (Sandbox Code Playgroud)

我甚至知道是什么引发了这个问题.apache代理与标题混杂在一起.离开我的机器的数据包的原始请求标题如下所示:

GET /sockjs/430/minw4r_o/websocket HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: example.com
Origin: http://example.com
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: myKey
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits, x-webkit-deflate-frame
User-Agent: My Agent
Run Code Online (Sandbox Code Playgroud)

数据包从apache代理转发,如下所示:

GET /sockjs/430/minw4r_o/websocket HTTP/1.1
Host: example.com
Origin: http://example.com
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: myKey
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits, x-webkit-deflate-frame
User-Agent: My Agent
X-Forwarded-For: 24.xxx.xxx.xxx
X-Forwarded-Host: example.com
X-Forwarded-Server: example.com
Connection: Keep-Alive
Run Code Online (Sandbox Code Playgroud)

因此"升级"被删除并且"连接"被更改,因此websocket握手失败.现在我可以尝试使用RequestHeader指令始终将"Upgrade"设置为"websocket".然而,这感觉不对,我想它会带来其他问题,所以我想知道这个问题是否有真正的解决方案?或者这是来自/sf/answers/1189906511/的补丁应该解决的问题,并且在我应用它时会出现问题?

从我读到的切换到nginx可以使这个设置更容易.我会考虑这个但是在可能的情况下我想用apache做这个,因为nginx会让其他东西变得更复杂并且花费我很多时间.

Fat*_*lan 18

我们将它用于Apache和Apache背后的SockJS应用程序.Apache正在自动执行WebSocket代理,但您必须将该方案重写为ws,否则它将回退到XHR.但只有连接是WebSocket握手.添加以下内容将解决您的问题:)(注意:localhost:3000相应地更改为您自己的后端网址.

RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^websocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade [NC]
RewriteRule .* ws://localhost:3000%{REQUEST_URI} [P]
Run Code Online (Sandbox Code Playgroud)

  • 我在哪里添加这个片段? (2认同)

der*_*wie 15

这个答案是基于法提赫的回答.对于发送"升级"以外的连接请求标头的浏览器,例如"keep-alive,Upgrade",他的解决方案失败了.使用Firefox 42就是这种情况.

要解决Firefox的问题,请更改apache RewriteCond,如下所示:

RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule .* ws://localhost:3000%{REQUEST_URI} [P]
Run Code Online (Sandbox Code Playgroud)

(^升级$变为升级$)

我想把这作为对法提赫答案的评论,但我缺乏必要的声誉.


pfe*_*rel 9

在阅读了几个答案之后,在Meteor论坛上发帖,并在这里进行了大量试验,这对我来说是完整的辣酱玉米饼馅.其他答案有点不完整,或者至少对我不起作用.

我必须做:

sudo a2enmod proxy_wstunnel 
Run Code Online (Sandbox Code Playgroud)

还必须添加一个ProxyPass和ProxyPassReverse,并从另一个SO答案更改^ Upgrade $ to Upgrade $.

<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>
Run Code Online (Sandbox Code Playgroud)

然后重启Apache.

我检查了控制台,现在没有错误,没有xhr请求.所以我认为它的工作正常