Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proxy websocket wss:// to ws:// apache

i searched alot but i couldnt connect my websocket to wss:// , i found that there is a way to proxy wss://domain.com:9090 and apache apply the proxy on it and redirect request to where the normal ws://domain.com:9090 server is running

ProxyPass /websocket ws://domain.com:9090
ProxyPassReverse /websocket ws://domain.com:9090

this code in apache config will send request from any address ended with /websocket to ws://domain.com:9090 ex : ws://websocket will be ws://domain.com:9090

i want to do it for wss:// also ex wss://websocket must point to ws://domain.com:9090

it dosnt work and i get this error in browser console :

failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

is there any mistake here ? thanks you .

like image 981
Sam Rad Avatar asked Aug 08 '16 21:08

Sam Rad


1 Answers

You need to enable some Apache2 modules:

$ a2enmod proxy proxy_wstunnel proxy_http rewrite

Then you can use this configuration to solve your problem.

    ProxyRequests off
    ProxyVia on      
    RewriteEngine On

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

    ProxyPass               /websocket http://example.com:9090/websocket
    ProxyPassReverse        /websocket http://example.com:9090/websocket

Apache2 automatically upgrades the connection to websocket with ws://, you don't need to set the ws:// manually. I tried dozens of configurations and this is the only one that worked for me.

like image 78
Florian Metzger-Noel Avatar answered Sep 22 '22 07:09

Florian Metzger-Noel