如何配置 Apache 以正确转发 websockets?

How can I configure Apache to correctly forward websockets?

我已经在 SO 和相关网站上查看了 this question 和其他各种解决方案,但是到目前为止 none 所建议的解决方案已经奏效。

我在 localhost:8080 运行 tty.js 并验证了直接访问该端口时 websockets 工作正常。现在,我正在尝试通过 Apache 反向代理连接到 tty.js。该应用程序可以在不使用 Websockets 的情况下工作,但是 我想了解为什么 Websockets 不工作。

这是我的 Apache 配置,用于在 localhost 上使用来自 source 的全新 Apache 构建进行测试。

Listen 9000
# Load proxy modules
LoadModule proxy_module "modules/mod_proxy.so"
LoadModule proxy_http_module "modules/mod_proxy_http.so"
LoadModule proxy_wstunnel_module "modules/mod_proxy_wstunnel.so"

<VirtualHost *:9000>
ServerName localhost

ProxyPass /tty/socket.io/1/ ws://localhost:8080/socket.io/1/
ProxyPassReverse /tty/socket.io/1/ ws://localhost:8080/socket.io/1/
ProxyPass /tty/ http://localhost:8080/
ProxyPassReverse /tty/ http://localhost:8080/

</VirtualHost>

当我连接到 http://localhost:9000/tty/ 时,出现 500 错误。

在服务器端日志中,我收到以下错误。

[Mon Jan 02 19:30:32.342551 2017] [proxy:warn] [pid 28226:tid 140098955872000] [client 127.0.0.1:38372] AH01144: No protocol handler was valid for the URL /tty/socket.io/1/. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule., referer: http://localhost:9000/tty/

Apache 使 Websockets 正常工作的正确配置是什么?

在调试器中 运行 Apache 几个小时后,我发现 Apache 忠实地通过 mod_proxy 传递与给定路径匹配的所有请求,包括以下请求。

http://localhost:9000/tty/socket.io/1/

原来这个请求在Upgrade: Websocketheader的实际请求之前就出去了,而且是正常的AJAX请求.

当此请求传递给 mod_proxy_wstunnel 时,模块注意到 Websocket header 丢失并拒绝处理它,导致 Apache 在日志消息中声明 No protocol handler was valid和 return 状态代码 500。

解决方法是扩展 ProxyPath 指令的路径,使其仅涵盖实际的 Websocket 请求,而不是杂散的 Ajax 请求。

ProxyPass /tty/socket.io/1/websocket/ ws://localhost:8080/socket.io/1/websocket/
ProxyPassReverse /tty/socket.io/1/websocket/ ws://localhost:8080/socket.io/1/websocket/