如何使用 RewriteRule 和 ProxyPass 获得条件代理?

How to use RewriteRule and ProxyPass to get conditional proxying?

我想做的是将每个 websocket 初始化代理到 "ws://localhost:8081" 并将其余流量直接代理到 http://localhost:80801

我使用 [P] 标志让它工作,但是不鼓励这样做,因为它不使用连接池。

如果设置了 header,想法是将请求重写到 /websocket,然后如果 url 是 /websocket proxypass 到 ws 端点。

但是,这对 WS 不起作用(对 http 工作正常)

#rewrite for websockets

RewriteEngine On
LogLevel alert rewrite:trace8
RewriteCond "%{HTTP:Upgrade}" "websocket"
# RewriteRule "" "ws://localhost:8081" [P] works via proxy like that 
RewriteRule "" "/websocket" [L]

ProxyPass /websocket "ws://localhost:8081"
ProxyPassReverse /websocket "ws://localhost:8081"

ProxyPass / http://localhost:8081/

我做错了什么?

大约 10 秒后,我得到了答复。

我不得不通过请求,所以像

这样的设置
RewriteCond "%{HTTP:Upgrade}" "websocket"
# RewriteRule "" "ws://localhost:8081" [P] works via proxy like that 
RewriteRule "" "/websocket" [L,PT]

完成任务。