Friday, November 12, 2021

[SOLVED] How to use RewriteRule and ProxyPass to get conditional proxying?

Issue

What I want to do is to proxy every websocket initialization to "ws://localhost:8081" and rest of the traffic directly to http://localhost:80801

I got it working using [P] flag however it is discouraged to do this that way as it does not use connection pooling.

The idea is to rewrite request to /websocket if header is set, and then if url is /websocket proxypass to ws endpoint.

However, this does not work for WS (works fine for 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/

What am I doing wrong?


Solution

Like 10 seconds later and I got an answer.

I had to PASS TROUGHT the request, so settings like

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

does the job.



Answered By - Antoniossss