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

时间:2019-07-05 14:58:44

标签: apache httpd.conf

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

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

想法是如果设置了标头,然后将请求重写为/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/

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

就像10秒钟后,我得到了答案。

我必须通过请求,所以设置

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

完成工作。