Ad

Socket.io Not Working With Nginx And Https

- 1 answer

I have a React app communicating with a very simple node backend, it works perfectly fine in local and was working perfectly fine over http. For obvious reason, the app is now running in https but the socket is not connecting anymore. I have been looking to so many threads but couldn't find a way to fix it. Here are the socket parts of my code and nginx conf.

Client side:

const socket = io('wss://sub.domain.io', { path: '/my-path',});

Server side:

const server = require('http').createServer();

const io = require('socket.io')(server, {
    path: '/my-path',
    serveClient: false
});

server.listen(8080);

nginx proxy

server {
      ....website serving part...

      location /my-path/ {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
 }

Error is: GET https://sub.domain.io/my-path/?EIO=3&transport=polling&t=MsCot3e net::ERR_CONNECTION_TIMED_OUT VM17:1 which looks like a timeout because it fails to connect.

Ad

Answer

So I found an answer after so many hours and it is pretty dumb and simple. My Nginx only handle the www website (DNS redirection is enough for the website), hence I needed www.sub.domain.io here:

const socket = io('wss://www.sub.domain.io', { path: '/my-path',});
Ad
source: stackoverflow.com
Ad