Socket.io + nginx + cloudflare problems

Hi, I was working on my project that uses socket.io, I hosted it on a VPS I don’t know how to call it but I use more than 1 port, so I’m using Nginx to manage this, if I connect to the VPS IP Address directly it works, but if using a domain it won’t work however if I didn’t turn on Cloudflare proxy it works but I don’t want to make my VPS IP Address revealed.

Here is the Nginx site config:

server {
    listen 80;

    server_name socket.domain.tld;

    location / {
        proxy_set_header X-Real-IP $http_cf_connecting_ip;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-User-Agent $http_user_agent;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:2112;
        proxy_redirect off;
    }
}

Yes, the port that socket.io using is 2112

Server Code:

const app = require('express')();
const Socket = require('socket.io');

const Server = app.listen(2112);
const io = Socket.listen(Server, { origins: '*' });

io.on('connection', (socket) => {
  console.log('Connected');
});

On the client (works):

const io = require('socket.io-client');

io.connect('http://vps.ip.address:2112');

On the client (not work):

const io = require('socket.io-client');

io.connect('http://socket.domain.tld');

Is there any way to fix this?

NOTE: Don’t reply with “just disable Cloudflare proxy” or “make the orange cloud to gray” because I don’t want to reveal my VPS IP Address

This is a repost from: this because it got detected spam I guess? but I need a solution ASAP

Hi, I was working on my project that uses socket.io, I hosted it on a VPS I don’t know how to call it but I use more than 1 port, so I’m using Nginx to do this, if I connect to the VPS IP Address directly it works, but if using a domain it won’t work however if I didn’t turn on Cloudflare proxy it works but I don’t want to make my VPS IP Address revealed.

Here is the Nginx site config:

server {
    listen 80;

    server_name cortex-api.hzmi.xyz;

    location / {
        proxy_set_header X-Real-IP $http_cf_connecting_ip;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-User-Agent $http_user_agent;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:2112;
        proxy_redirect off;
    }
}

Server Code:

const app = require('express')();
const Socket = require('socket.io');

const server = app.listen(2112); //I used the port 2112
const IO = Socket.listen(Server, { origins: '*' })

IO.on('connection', (socket) => {
     console.log("Connected');
});

Client code that works:

const client = require('socket.io-client');

client.connect('http://vps.ip.address:2112'); //Will work

Client code that not working:

const client = require('socket.io-client');
client.connect('http://socket.domain.tld'); //Will not work

NOTE: Please don’t reply with “Just turn off Cloudflare Proxy” or “Click the orange cloud so it turns gray” because I don’t want my VPS IP Address got revealed

Thank you.

2112 is not a port supported by Cloudflare. You can only use those listed at https://support.cloudflare.com/hc/en-us/articles/200169156-Which-ports-will-Cloudflare-work-with-

Will try, Thanks.

Instead of changing express to listen on port 2096, I made the NGINX listen on multiple ports (80 and 2096) like this:

server {
    listen 80;
    listen 2096;

    server_name socket.domain.tld;

    location / {
        proxy_set_header X-Real-IP $http_cf_connecting_ip;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-User-Agent $http_user_agent;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:2112;
        proxy_redirect off;
    }
}

But, I can’t still connect. When I open https://socket.domain.tld:2096, I got an SSL Handsake Error. What should I do now?

For starters I’d suggest to use the search as that topic has been covered a gazillion times.

I solved it by changing 2096 to 443, Thanks for the help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.