首页 > 解决方案 > Apache2 反向代理和 websocks

问题描述

我正在尝试通过以下方式允许我的反向代理中的 websocks:

<Location "/monitor">
        ProxyPreserveHost On
        RequestHeader set X-Forwarded-Proto https
        RequestHeader set X-Forwarded-Port 443

        RewriteEngine On
        RewriteCond %{REQUEST_URI}  ^/monitor/notify       [NC]
        RewriteCond %{QUERY_STRING} transport=websocket    [NC]
        RewriteRule ^/monitor/notify/(.*) wss://localhost:8001/monitor/notify/$1         [P,L]

        ProxyPass http://localhost:8001/monitor
        ProxyPassReverse http://localhost:8001/monitor
</Location>

但我收到了这个错误:

VM690:149 WebSocket connection to 'wss://MY.HOST/monitor/notify/516/q7pqnqdz/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400

我做错了什么?

标签: apachewebsocketreverse-proxy

解决方案


解决了这个问题:

RewriteEngine On
RewriteCond %{HTTP:Connection} Upgrade [NC]
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteRule /monitor/(.*) ws://127.0.0.1:8001/monitor/$1  [P,L]

推荐阅读