Search code examples
node.jscloudflarenode-https

Cloudflare 522 error - javascript client connecting to node server


I'm trying to connect to a nodejs https server from a apache web server hosted javascript client and I'm getting an error message : 522 - Failed to load response data: No data found for resource with given indentifier. The apache web server runs on the same domain/server as the node server and the servers are proxied by Cloudflare:

Both services run in the same server/machine. This is how I start nodejs server:

// Certificates are the same used by apache web server in Virtual Host 
// and were got from Cloud Flare Panel > SSL/TLS > Origin Server
var options = {
  key: fs.readFileSync('/etc/cloudflare/example.com.key'),
  cert: fs.readFileSync('/etc/cloudflare/example.com.pem'),
};

var socket = require('socket.io');
var http = require('https');

// Port 2053 was listed as a https port supported by Cloud Flare in
// https://developers.cloudflare.com/fundamentals/get-started/reference/network-ports/
var argv = require('optimist')
    .usage('Usage: --port [num]')
    .default({port: 2053})
    .argv;

var server = http.createServer(options, function(req, res) {
});

server.listen(argv.port);

var io = socket.listen(server);

This is how I connect to nodejs server from the javascript client:

let socket = io.connect("https://www.example.com:2053", {secure: true});

Any tips?

Edit 1 It works if I create the node server as http (instead of https).


Solution

  • I was able to connect to node server by doing the follow:

    1. Set "key" and "cert" options when instancing https node server: these files can be generated in Cloud Flare Panel > Select your domain > SSL/TLS > Origin Server. There was no need for "ca", "requestCert" or "rejectUnauthorized" parameters.
    2. Use one of the ports listed in https://developers.cloudflare.com/fundamentals/get-started/reference/network-ports/ in the node server. Cloud flare automatically redirect these ports to the same port in your origin server.
    3. Allow inbound connections on the selected port (step 2) in your origin server firewall.
    4. Set SSL setting to FULL in Cloud Flare Panel > Select your domain > SSL/TLS > Overview