I have been trying for weeks to get a websocket working on my SSL secure Apache 2.4.29 server running on Ubuntu 18.04.
Here is the code I am using to set up a secure wss:// protocol websocket:
// Minimal amount of secure websocket server
var fs = require('fs');
// read ssl certificate
var privateKey = fs.readFileSync('/path/to/privkey.pem', 'utf8');
var certificate = fs.readFileSync('/path/to/fullchain.pem', 'utf8');
var credentials = {
key: privateKey,
cert: certificate
};
var https = require('https');
//pass in your credentials to create an https server
var httpsServer = https.createServer(credentials);
httpsServer.listen(8080);
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({
server: httpsServer
});
httpsServer.on('upgrade', wss.handleUpgrade);
wss.on('connection', function connection(ws) {
wss.on('message', function incoming(message) {
console.log('received: %s', message);
wss.send('reply from server : ' + message)
});
wss.send('something');
});
Here is some of my ssl <VirtualHost *:443> where I am trying to do a ProxyPass:
> LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
> LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
> LoadModule proxy_ajp_module /usr/lib/apache2/modules/mod_proxy_ajp.so
> LoadModule proxy_connect_module /usr/lib/apache2/modules/mod_proxy_connect.so
> LoadModule proxy_express_module /usr/lib/apache2/modules/mod_proxy_express.so
> LoadModule proxy_fcgi_module /usr/lib/apache2/modules/mod_proxy_fcgi.so
> LoadModule proxy_ftp_module /usr/lib/apache2/modules/mod_proxy_ftp.so
> LoadModule proxy_html_module /usr/lib/apache2/modules/mod_proxy_html.so
> LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
> LoadModule proxy_scgi_module /usr/lib/apache2/modules/mod_proxy_scgi.so
> LoadModule proxy_wstunnel_module /usr/lib/apache2/modules/mod_proxy_wstunnel.so
> LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
>
> LoadModule log_forensic_module /usr/lib/apache2/modules/mod_log_forensic.so
> <IfModule log_forensic_module>
> ForensicLog /var/log/apache2/forensic_log
> </IfModule>
>
> SSLEngine on
> SSLCertificateFile /etc/letsencrypt/live/taskhandle.io/fullchain.pem
> SSLCertificateKeyFile /etc/letsencrypt/live/taskhandle.io/privkey.pem
>
> AllowEncodedSlashes NoDecode
>
> #SSL Connect
> SSLProxyVerify none
> SSLProxyCheckPeerCN off
> SSLProxyCheckPeerName off
> SSLProxyCheckPeerExpire off
>
> SSLProxyEngine on
> RewriteEngine on
> ProxyRequests off
> ProxyPreserveHost On
> RewriteCond %{HTTP:Upgrade} websocket [NC]
> RewriteCond %{HTTP:Connection} upgrade [NC]
> RewriteRule /(.*) "wss:/localhost:8080/$1" [P,L]
>
> # the WebSocket Proxying
> ProxyPass "/app/ws" "ws://localhost:8090/app/ws"
> # the common app proxying
> ProxyPass "/app" "http://localhost:8090/app"
> ProxyPassReverse "/app" "http://localhost:8088/app"
>
> ProxyPass "/wss2" "wss://localhost:8080/"
> ProxyPassReverse "/wss2" "wss://localhost:8080/"
>
> ProxyPass "/wss" "ws://localhost:8080/"
> ProxyPassReverse "/wss" "ws://localhost:8080/"
>
> ServerName taskhandle.io
> SSLCertificateFile /etc/letsencrypt/live/taskhandle.io/fullchain.pem
> SSLCertificateKeyFile /etc/letsencrypt/live/taskhandle.io/privkey.pem
> Include /etc/letsencrypt/options-ssl-apache.conf
> </VirtualHost>
> </IfModule>
But when I run the websocket using 'nodejs index.js' from the command line, then try to access the websocket from a client browser using
var conn = new WebSocket('wss://mywebsite.io:8080');
conn.onopen = function(e) {
console.log("Connection established!");
};
I get a 500 response code on the client side, and in the apache error log it says:
> "[Sun Jun 21 21:10:20.487029 2020] [proxy:warn] [pid 30076] [client
> 208.87.239.180:34995] AH01144: No protocol handler was valid for the URL /wss2 (scheme 'wss'). If you are using a DSO version of mod_proxy,
> make sure the proxy submodules are included in the configuration using
> LoadModule."
which is strange because I am pretty sure I have all the submodules enabled and loaded. I wonder if it might be an issue with the headers not being proxied. Here are the headers being received in my access log when I try to establish the websocket connection:
**GET /wss2 HTTP/1.1|Host:taskhandle.io|Pragma:no-cache|Cache-Control:no-cache|User-Agent:Mozilla/5.0
(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36|Origin:https%3a//stackoverflow.com|Sec-WebSocket-Version:13|Accept-Encoding:gzip, deflate|Accept-Language:en-US,en;q=0.9|Cookie:_ga=GA1.2.1978077118.1589209571; _gid=GA1.2.1472919475.1592682267|Sec-WebSocket-Key:WyjiHAZ3HPj0lcvvVGzq9Q==|Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits|Via:1.1 hosted.websense 02o|X-Forwarded-For:68.12.180.212|Client-IP:68.12.180.212 -30076:5eefccbc:1**
If you notice there is not a Connection: Upgrade or Upgrade: websocket header present in the request header being received in my access log...
Please help I have been struggling for weeks to get this websocket working. I have also tried setting up the websocket using Ratchet in PHP but still having the same issue whether I use node or ratchet.