Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How connect websocket to wss:// on https server [duplicate]

I couldn't connect my websocket using wss://.

I found that there is a way to use Apache to expose

  • wss://domain.com - frontend (Apache)
  • ws://domain.com:9090 - backend plaintext

My configuration:

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. Example: wss://websocket must point to ws://domain.com:9090.

It doesn't 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 918
Sam Rad Avatar asked Aug 08 '26 21:08

Sam Rad


2 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 172
Florian Metzger-Noel Avatar answered Aug 11 '26 10:08

Florian Metzger-Noel


i worked 24 hours for find this and searched a lot of forum but no one write about success. here is my server configuration :

CentOS release 6.7 , Apache 4.2.18

here is what i did finally : first i found that modules/mod_proxy_wstunnel.so must be enable in apache config file , but my apache didn't have that module and after a lot of search i found that module is Available in apache 2.4.5 and later.

https://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html

i downloaded https://archive.apache.org/dist/httpd/httpd-2.4.18.tar.gz extracted httpd-2.4.18\modules\proxy\mod_proxy_wstunnel.c and uploaded to my server root then from terminal could compile it again with these commonds :

chmod 755 mod_proxy_wstunnel.c #set permission
pxs -i -a -c mod_proxy_tunnel.c #compile module

pxs command did compile the module and wrote in apache config file to load it

LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

after that i added these lines to end of apache config file :

RewriteEngine on
ProxyRequests Off
ProxyPreserveHost on
ProxyPass /myws ws://mysite.com:8091
ProxyPassReverse /myws ws://mysite.com:8091

AND NOW : it works ! in client side js you can set ws url like this :

var protocol = 'ws://'; 
if (window.location.protocol === 'https:') {
            protocol = 'wss://';
   }

 var wsUri =protocol+ "mysite.com/myws";  

 var ws = new WebSocket(wsUri);

and it will forward request to ws://mysite.com:8091 doesnt matter the page loaded with https or http , it will direct all request ended with /myws to ws://mysite.com:8091

like image 24
Sam Rad Avatar answered Aug 11 '26 10:08

Sam Rad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!