【问题标题】:Apache websocket works with ProxyPass but not RewriteRuleApache websocket 适用于 ProxyPass 但不适用于 RewriteRule
【发布时间】: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


    【解决方案1】:

    我只是通过反复试验偶然发现了解决方案,即从RewriteRule 中删除L(最后一个)标志。我仍然不明白为什么要修复它。我认为无论如何发布这个答案会很好。

    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/
    

    【讨论】:

      猜你喜欢
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 2011-07-04
      • 2011-09-09
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      相关资源
      最近更新 更多