[re-post] Socket.IO + NGINX not working/connecting with CloudFlare

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_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) => {

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.

A post was merged into an existing topic: Socket.io + nginx + Cloudflare problems