Websocket is not working with WSS in Apache

103
December 30, 2018, at 8:30 PM

Let me explain you the whole scenario in detail:

We have two servers: - one with shared hosting - other is VPS

Our requirement is to setup the websocket in the project. but to do so, the shared hosting doesn't allow to open the custom port. so we decided to buy another "VPS" server.

check below image:

As you can see in above image, our webportal uses VPS server for websocket connection.

Shared hosting is HTTPS. And VPS server has also SSL. so it is also HTTPS.

so if I connect web portal with "ws" as below:

var wsocket = new WebSocket("ws://DOMAIN_NAME_OF_VPS:PORT")

then it gives error as below: "Mixed content : The page at 'https://example.com' was loaded over Https, but attepmted to connect to the insecure websocket endpoint 'ws://DOMAIN_NAME_OF_VPS:PORT'. This request has been blocked; this endpoint must be available over WSS."

so it is obvious that we cannot use WS in HTTPS.

so we simply replace WS with WSS as below (ofcousre we install SSL certificate):

var wsocket = new WebSocket("wss://DOMAIN_NAME_OF_VPS:PORT")

Now I am getting this error : websocket connection to 'wss://DOMAIN_NAME_OF_VPS:PORT' failed: WebSocket opening handshake timed out.

About websocket server (vps server):

I am using Ratchet for websocket integration in PHP. http://socketo.me/

I am using Apache engine for PHP.

Ratchet server code :

<?php
require __DIR__ . "/../vendor/autoload.php";
use Chat\Chat;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
$server = IoServer::factory(new HttpServer(new WsServer(new Chat)), 19994);
$server->run();
?>

000-default file is :

  • I already tried some ProxyPass which is commented in code. Please help if I have done wrong code.

    # ServerAdmin vps-server-domain ServerName vps-server-domain

    DocumentRoot /var/www/ 
    SSLEngine on 
    SSLCertificateKeyFile /etc/ssl/private/vps-server-domain_private.key 
    SSLCertificateFile  /etc/ssl/certs/vps-server-domain.crt 
    SSLCertificateChainFile /etc/ssl/certs/vps-server-domain.ca-bundle
    # SSLProxyEngine On
    # ProxyRequests Off 
    # ProxyPass "/wss/"  "ws://vps-server-domain:19994/"
    # ProxyPass "/wss2/"  "ws://vps-server-domain:19994/"
    # ProxyPass /wss2/  "ws://vps-server-domain:19994/"
    # ProxyPass /ws/ wss://vps-server-domain:19994/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    # RewriteEngine on
    # RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    # RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
    # RewriteRule .* wss://vps-server-domain:19994%{REQUEST_URI} [P]
    

If I use http in the web portal, and use "ws" for websocket, then everything works fine.

so I do not understand why "wss" is not working with "https", while I have integrated SSL in VPS.

Is there any other way to tell websocket to use SSL files?

Am I missing something?

Your help will be very important for me.

Thank you very much, Happy new year in advance.

READ ALSO
Sum of MYSQL Query

Sum of MYSQL Query

I have a MySQL DB

66
Is it a good idea to register style only in certain WP pages?

Is it a good idea to register style only in certain WP pages?

I have a page speed issue, the theme I bought is really crappy but i cannot change it nowI use WP_rocket, server have HTTP2 but still it is to many resources to load

49
PHP curl taking so long to execute and in the end it does not do anything on de nodejs server

PHP curl taking so long to execute and in the end it does not do anything on de nodejs server

I'm setting up a new notification server with nodejs and socketio

47
Having trouble adding validation errors to ACF form

Having trouble adding validation errors to ACF form

I'm using ACF with WordPress and I have a form that I am trying to add a validation error to, but it doesn't appear to be working (I think I know why)

99