web-socket icon indicating copy to clipboard operation
web-socket copied to clipboard

How to configure WSS working with HTTPS?

Open chantrea opened this issue 6 years ago • 9 comments

Hi here are my steps My Apache is 2.4.34 on Centos server in my route $socket->route('/myclass', new MyClass(), ['*']); <VirtualHost *:443> //All Certificates and domain is working fine here. SSLProxyEngine On ProxyPreserveHost On ProxyRequests Off ProxyPass /wss/ ws://dev.domain.com:2000/ retry=0 keepalive=On ProxyPassReverse /wss/ ws://dev.domain.com:2000/ retry=0 </VirtualHost>

In client JavaScript Code pace.min.js:2 WebSocket connection to 'wss://dev.domain.com/wss/myclass' failed: Error during WebSocket handshake: Unexpected response code: 503

And It is working find in localhost with ws://localhost:2000/myclass Without HTTPS

chantrea avatar Nov 22 '18 07:11 chantrea

Hi @chantrea

It duplicates #12 please note that you answered and gave instructions You must ensure proxying of requests through your main server (in this case, apache)

Example:

<VirtualHost *:443>
    ServerName website.com

    RewriteEngine On

    # When Upgrade:websocket header is present, redirect to ws
    # Using NC flag (case-insensitive) as some browsers will pass Websocket
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule ^/ws/(.*)    wss://localhost:8888/ws/$1 [P,L]

    # All other requests go to http
    ProxyPass "/" "http://localhost:8888/"

You can google information, for example on request: apache websocket proxy https

tabuna avatar Nov 22 '18 07:11 tabuna

I think I also did this way but still have no luck.

chantrea avatar Nov 22 '18 08:11 chantrea

Hi, this is my setting when used this library,

# This is for websocket requests
ProxyPass /ws/ ws://imessage.loc:8080/
ProxyPassReverse /ws/ ws://imessage.loc:8080/

RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://imessage.loc:8080%{REQUEST_URI} [P]

You can try that setup.. and don't forget to added :

ProxyPreserveHost On

mabuak avatar Nov 22 '18 08:11 mabuak

Thank you for your response. I already follow your instruction.

<VirtualHost *:443>
        SSLProxyEngine On
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyPass /wss/ ws://dev.mydomain.com:2000/
        ProxyPassReverse /wss/ ws://dev.mydomain.com:2000/
        RewriteEngine on
        RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
        RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
        RewriteRule .* ws://dev.mydomain.com:2000%{REQUEST_URI} [P]
</VirtualHost>

in my route $socket->route('/myclass', new MyClass(), ['*']); And here is the JavaScript Code

class Ws {
        get newClientPromise() {
	    return new Promise((resolve, reject) => {
		$.post("/api/getconfig", {_token: $("#laraveltoken").val(), configname: 'socket.host'}, function(host){
		let wsClient = new WebSocket(host+"/myclass");
 // URL wss://dev.mydomain.com/wss/myclass
		wsClient.onopen = () => {
	             console.log("connected");
	             resolve(wsClient);
               };
	 wsClient.onerror = error => reject(error);
		    });	
	    });
	  }
	  get clientPromise() {
	    if (!this.promise) {
	      this.promise = this.newClientPromise
	    }
	    return this.promise;
	  }
}

window.wsSingleton.clientPromise..then( wsClient =>{wsClient.send('UserID:{{Auth::User()->id}}');})
			         .catch( error => alert(error) );

$.post("/api/getconfig", {_token: $("#laraveltoken").val(), configname: 'socket.host'}, function(host){
  let socket = new WebSocket(host+"/myclass");
      socket.onmessage = function(event) {
       let data = event.data.split("UserID:");
      if(typeof data[1] !== 'undefined')
      {
         if(data[1] == "{{Auth::User()->id}}")
          {
             //do something
           }
         }
     };
});

Error is

pace.min.js:2 WebSocket connection to 'wss://dev.mydomain.com/wss/myclass' failed: Error during WebSocket handshake: Unexpected response code: 503

chantrea avatar Nov 22 '18 18:11 chantrea

ProxyRequests Off ProxyPass /wss/ ws://dev.mydomain.com:2000/ ProxyPassReverse /wss/ ws://dev.mydomain.com:2000/ RewriteEngine on RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC] RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC] RewriteRule .* ws://dev.mydomain.com:2000%{REQUEST_URI} [P] </VirtualHost>

Is you'r already installed the ssl certificated?

and installed this packages?

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart

mabuak avatar Nov 23 '18 03:11 mabuak

Yes certificates is 100% working because installed it more than a years and been using https all the time.

Sent from my iPhone

On Nov 22, 2018, at 8:32 PM, Ramadhan [email protected] wrote:

ProxyRequests Off ProxyPass /wss/ ws://dev.mydomain.com:2000/ ProxyPassReverse /wss/ ws://dev.mydomain.com:2000/ RewriteEngine on RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC] RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC] RewriteRule .* ws://dev.mydomain.com:2000%{REQUEST_URI} [P]

Is you'r already installed the ssl certificated?

and installed this packages?

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

chantrea avatar Nov 23 '18 03:11 chantrea

Anyway i am using AWS linux 2 not ubuntu. It is on the top of CentOS

chantrea avatar Nov 23 '18 07:11 chantrea

Come on guy I tried everythings but it is not working like you all said. Please guy me step by step.

chantrea avatar Nov 27 '18 02:11 chantrea

Not working on my side too. Does anybody resolved connection with wss?

ramixxx avatar Jun 13 '19 20:06 ramixxx