0

我正在尝试在 AWS ElasticBeanstalk Tomcat 8.5 Web 应用程序上启用 websocket。我有一个自定义 apache 配置(缩写):

RewriteEngine On
RewriteOptions Inherit

ProxyRequests Off
ProxyPreserveHost on

# send websocket requests to tomcat with the websocket protocol
RewriteCond ${HTTP:Connection}  "Upgrade" [NC]
RewriteCond ${HTTP:Upgrade}     "websocket" [NC]
RewriteRule /(.*) "ws://localhost:8080/$1" [P,L]

# send all other requests to tomcat
ProxyPass           /   http://localhost:8080/  retry=0
ProxyPassReverse    /   http://localhost:8080/

在 Web 应用程序中,当我尝试将客户端 websocket 连接到服务器端点时,我收到此错误:

websocket.js:372 WebSocket 连接到“wss://example.com/ws/sales”失败:WebSocket 握手期间出错:意外响应代码:404

我在 Chrome 的开发人员工具中验证了发送到上述端点的请求中的 Connection 和 Upgrade 标头是正确的。(显然我真的连接到我的网站,而不是 example.com)

当我将 apache 配置更改为使用 ProxyPass 而不是 RewriteRule 时,它​​可以完美运行!我不想这样做,因为我不想仅仅基于 URI 代理到 ws。我想像你应该的那样检查标题!

ProxyRequests Off
ProxyPreserveHost on

# send websocket requests to tomcat with the websocket protocol
ProxyPass           /ws/    ws://localhost:8080/ws/ retry=0
ProxyPassReverse    /ws/    ws://localhost:8080/ws/

# send all other requests to tomcat
ProxyPass           /   http://localhost:8080/  retry=0
ProxyPassReverse    /   http://localhost:8080/
4

1 回答 1

0

我通过反复试验偶然发现了解决方案,即LRewriteRule. 我仍然不明白为什么要修复它。我认为无论如何发布这个答案会很好。

RewriteEngine On
RewriteOptions Inherit

ProxyRequests Off
ProxyPreserveHost on

# send websocket requests to tomcat with the websocket protocol
RewriteCond %{HTTP:Upgrade}     "websocket" [NC]
RewriteCond %{HTTP:Connection}  "Upgrade" [NC]
RewriteRule .* "ws://localhost:8080%{REQUEST_URI}" [P]

# send all other requests to tomcat
ProxyPass           /   http://localhost:8080/  retry=0
ProxyPassReverse    /   http://localhost:8080/
于 2020-03-20T12:52:33.513 回答