1

My websocket setup works in http. But if I enable ssl(lets encrypt) and change the ws:// to wss://, browser throws this error.

WebSocket connection to 'wss://xx.yy.xx.yy:5001/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED

Javascript:

var ws = new WebSocket('wss://xx.yy.xx.yy:5001');

I then enabled these apache modules: proxy_http, proxy_wstunnel, proxy_balancer

And added these lines in /etc/apache2/sites-enabled/000-default-le-ssl.conf

<IfModule mod_proxy.c>
ProxyPass / ws://xx.yy.xx.yy:5001
ProxyPassReverse / ws://xx.yy.xx.yy:5001
</IfModule>

Got a 500 Server Error.

Though I had no idea how this proxy thing works, just given a blind try. Please correct my mistake, help me to fix this. Thanks.

Server details:

  • Apache 2.4.18 running in AWS EC2 ubuntu instance.
  • public ip : xx.yy.xx.yy
  • private ip : aa.bb.aa.bb

Websocket setup:

I use private ip for back-end(php) connection and public ip in front-end.

supercontra
  • 225
  • 1
  • 3
  • 10

1 Answers1

3

Well I fixed it myself. I changed my javascript into:

var ws = new WebSocket('wss://domain.com/ws/');

Enabled proxy modules by running the following command in terminal,

sudo a2enmod proxy proxy_balancer proxy_wstunnel proxy_http

Added these lines in my Apache virtualhost config file(/etc/apache2/sites-enabled/000-default-le-ssl.conf)

ProxyRequests Off
ProxyPass "/ws/"  "ws://domain.com:5001/"

Restarted apache service. And the websocket started working in https.

supercontra
  • 225
  • 1
  • 3
  • 10
  • Facing the same issue, Not able to set connection over wss(failed: WebSocket opening handshake timed out). Have you set the domain for backend and where you are configuring the proxy? is it in frontend virtualHost? – pbms Aug 27 '18 at 07:54
  • Can you share the full configuration of your apache2? – pbms Aug 27 '18 at 11:07
  • I added the proxypass in this file. /etc/apache2/sites-enabled/000-default-le-ssl.conf – supercontra Aug 28 '18 at 14:45
  • 1
    You need to enable the following modules and restart apache service. sudo a2enmod proxy proxy_balancer proxy_wstunnel proxy_http – supercontra Aug 28 '18 at 14:49