【发布时间】:2020-06-29 17:11:18
【问题描述】:
我正在尝试在 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 时,它可以完美运行!我不想这样做,因为我不想仅仅基于代理到 ws URI。我想像你应该的那样检查标题!
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/
【问题讨论】:
标签: apache mod-rewrite websocket mod-proxy mod-proxy-wstunnel