【问题标题】:Apache2 WebSockets reverse proxy on same URL同一 URL 上的 Apache2 WebSockets 反向代理
【发布时间】:2015-06-29 19:18:25
【问题描述】:

如何配置 Apache2 代理 WebSocket 连接(例如 BrowserSync),如果它是在同一个 URL 上进行的,唯一的区别是标题“升级:websocket”和 URL 架构 ws://?

例如:

HTTP request:
GET http://example.com/browser-sync/socket.io/?... HTTP/1.1
...

WebSocket request:
GET ws://example.com/browser-sync/socket.io/?... HTTP/1.1
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
...

我找到的所有示例,仅重定向某些路径,例如“...”或“ProxyPass /ws/ws://example.com/”

我当前的配置:

ProxyRequests off
<Location />
    ProxyPass http://127.0.0.1:3000/
    ProxyPassReverse /
</Location>

mod_proxy、mod_proxy_http 和 mod_proxy_wstunnel 已启用。

【问题讨论】:

    标签: apache websocket reverse-proxy


    【解决方案1】:

    回答自己。

    使用 RewriteEngine、this post 给出的提示和 WebSocket 握手规范:

    RewriteEngine On
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule /(.*) ws://127.0.0.1:3000/$1 [P,L]
    
    ProxyRequests off
    <Location />
        ProxyPass http://127.0.0.1:3000/
        ProxyPassReverse /
    </Location>
    

    【讨论】:

    • 非常感谢!
    • 太好了,一直在找这个,一定会试试这个。
    • 帮了我很多忙 - 没想到关闭 ProxyRequests 可以解决问题,但最终还是这样。谢谢!
    【解决方案2】:

    非常感谢metalim!我在这里发布完整配置以供参考:

    <VirtualHost *>
        ServerName example.org
        ServerAlias *.example.org
        ServerAdmin sysadmin@example.org
        ProxyRequests Off
        ProxyPreserveHost On
    
        RewriteEngine On
        RewriteCond %{HTTP:Connection} Upgrade [NC]
        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteRule /(.*) ws://192.168.1.1/$1  [P,L]
    
        ProxyPass / http://192.168.1.1/ retry=1
        ProxyPassReverse / http://192.168.1.1/
    
        ErrorLog /var/log/apache2/example.org.error.log
        CustomLog /var/log/apache2/example.org.access.log combined
    </VirtualHost>
    

    【讨论】:

      猜你喜欢
      • 2011-08-09
      • 2013-02-18
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多