本文介绍了如何设立流星/ SockJS和WebSocket的一个Apache代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个流星应用程序,Apache和流星的Apache代理是在两个单独的机器上。我需要这种方式为Apache在服务很多真正的网站,它不会是安装在本机上的应用程序流星一个好主意,因为其有限的资源。

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 be a good idea to install the meteor app on this machine due to its limited resources.

但是WebSocket的握手失败,响应code 400只能升级到WebSocket的如果我尝试通过代理从外部连接。当我从局域网内直接连接到机器流星一切正常。
当WebSocket的失败SockJS /流星回落到XHR可惜这带来了在相关的应用程序的一些错误。所以,我真的很需要的WebSocket在大多数情况下工作。

However the WebSocket handshake fails with response code 400 "Can upgrade only to websocket" if I try to connect from the outside via the proxy. Everything works fine when I connect from within the LAN directly to the meteor machine.When WebSocket fails SockJS/Meteor falls back to XHR but unfortunately this brings up some bugs in the app in question. So I really need WebSocket to work in most of the cases.

我修改我的Apache的安装与这里提到的补丁:
这看起来似乎很顺利,但什么都没有改变...

I patched my apache installation with the patch mentioned here: http://stackoverflow.com/a/16998664That looked like it went well but nothing changed...

我的Apache的代理指令,目前如下:

My apache proxy directives currently are as follows:

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/

和我知道什么引发的问题。 Apache的代理与头部周围食堂。问题离开我的机器的数据包的原始请求头看起来是这样的:

And I even know what's triggering the problem. The apache proxy messes around with the header. The original request header of the packet in question leaving my machine looks like this:

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

虽然数据包被从Apache代理转发如下:

While the packet gets forwarded from the apache proxy like this:

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

所以,升级被删除,连接改变,所以WebSocket的握手失败。现在,我可以尝试总是设置升级为的WebSocket与RequestHeader指令。然而,这感觉不对,我想这会带来了其他问题,所以我想知道是否有一个真正的解决这个问题?或者是这个东西从补丁应该解决并出现了一些问题在我结束申请呢?

So "Upgrade" gets removed and "Connection" altered and so the websocket handshake fails. Now I could try to always set "Upgrade" to "websocket" with a RequestHeader directive. However this doesn't feel right and I guess it would bring up other problems and so I was wondering if there is a real solution to this problem? Or is this something the patch from http://stackoverflow.com/a/16998664 should address and something went wrong on my end applying it?

这是我看过切换到nginx的能做出这样的设置更简单的东西。我会考虑这一点,但可能的情况下,我想这样做在Apache的nginx会使其他事情更加复杂,花了我很多时间。

From what I have read switching to nginx could make this setup easier. I will consider this but when possible I'd like to do this with apache as nginx would make other things more complicated and cost me a lot of time.

推荐答案

我们使用Apache和Apache的背后一个SockJS应用程序。 Apache是​​自动做的WebSocket代理,但你必须重写方案来,否则它WS向回退XHR。但是,只有当连接是WebSocket的握手。添加下面将解决您的问题:)(注意:修改本地主机:3000 据此向自己的后端网址

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]

这篇关于如何设立流星/ SockJS和WebSocket的一个Apache代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:02
查看更多