Search code examples
node.jshttpssocket.iossl-certificatecloudflare

Nodejs SSL using CloudFlare not working over https


So the problem I'm having is that the client won't connect with the server.js when the server.js is using https.

if I go to "https://mydomainame.com" I get this error in the console of every other browser than brave browser index.js:83 GET https://serverip:8081/socket.io/?EIO=3&transport=polling&t=NK0oCD6 net::ERR_CERT_AUTHORITY_INVALID enter image description here

(The blacked out is the IP address of the server) the weird thing is that in the brave browser the domain changes to "http://mydomainame.com" and the client then is connected to server.js
enter image description here

I'm using free Cloudflare with Full end to end encryption enter image description here

server.js code:

var express = require('express'),
https = require('https');
var app = express();
var fs = require('fs');
var httpsOptions = { 
    key: fs.readFileSync('/var/www/ssl/sitename.com.key'),
    cert: fs.readFileSync('/var/www/ssl/sitename.com.pem')};
var server = https.createServer(httpsOptions,app);
var io = require('socket.io').listen(server);

const port = 8081;
server.listen(port);

And client.js connection code:

socket = io.connect('https://serverip:8081', {secure: true});

I am using the same Origin Certificates for the server and for the nodejs code. The server is using Apache2 with PHPMyAdmin and is configured to make the domain only work using https.

I read somewhere something Cloudflare not being able to use other ports than 443 and some other but I did not really understand it, And I can't get the server.js to work over port 443.

I'm thankful for any information or help I can get! :)


Solution

  • So I figured it out, big thanks to Eric Wong for pointing out the biggest problem that I was trying to connect to the server using its IP there for not going thru Cloudflare.

    Then in this article Identifying network ports compatible with Cloudflare's proxy you can see what ports Cloudflare allows connections on then, I changed my code to used the https port 8443.

    socket = io.connect('https://domainname.com:8443',{secure: true});
    

    then the only thing I had to do was to port forward the new port and everything worked fine!