ApacheWebSocket可以使用ProxyPass,但不能重写规则

ApacheWebSocket可以使用ProxyPass,但不能重写规则,apache,mod-rewrite,websocket,mod-proxy,mod-proxy-wstunnel,Apache,Mod Rewrite,Websocket,Mod Proxy,Mod Proxy Wstunnel,我正在尝试在AWS ElasticBeanstalk Tomcat 8.5 web应用程序上启用WebSocket。我有一个定制的apache配置(缩写): 在web应用程序中,当我尝试将客户端websocket连接到服务器端点时,出现以下错误: js:372 websocket与'wss://example.com/ws/sales'失败:WebSocket握手期间出错:意外响应代码:404 我在Chrome的开发者工具中验证了发送到上述端点的请求中的连接和升级头是正确的。(显然,我是在连接我

我正在尝试在AWS ElasticBeanstalk Tomcat 8.5 web应用程序上启用WebSocket。我有一个定制的apache配置(缩写):

在web应用程序中,当我尝试将客户端websocket连接到服务器端点时,出现以下错误:

js:372 websocket与'wss://example.com/ws/sales'失败:WebSocket握手期间出错:意外响应代码:404

我在Chrome的开发者工具中验证了发送到上述端点的请求中的连接和升级头是正确的。(显然,我是在连接我的网站,而不是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/

我仅仅通过反复试验就找到了解决方法,即从
重写规则中删除
L
(last)标志。我仍然不明白的是,为什么会这样。我想不管怎么说,把这个答案贴出来还是不错的

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/
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/