Why is my Nginx-Socket.io-Express-Setup not working over HTTPS?

by MiXT4PE   Last Updated August 14, 2019 20:00 PM

Locally it is working perfectly but as soon as I put it behind the Nginx reverse-proxy I get this:

GET https://127.0.0.1:8443/socket.io/?EIO=3&transport=polling&t=MoHVP61 net::ERR_SSL_PROTOCOL_ERROR

Nginx configuration:

upstream io_nodes {
  ip_hash;
  server 127.0.0.1:8443;
}

map $sent_http_content_type $expires {
    default                    off;
    text/html                  epoch;
    text/css                   max;
    application/javascript     max;
    ~image/                    max;
}

server {
    listen       80;
    listen       [::]:80;
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name  xxxxxxxxxx;
    expires      $expires;

    large_client_header_buffers 8 32k;

    error_page   404              /404.html;
    error_page   500 502 503 504  /50x.html;

    location / {
        proxy_pass http://io_nodes;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
    }

    location /socket.io {
        proxy_pass http://io_nodes;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
    }

    location = /404.html {
        root   /usr/share/nginx/html;
    }   
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    ssl_certificate      /etc/letsencrypt/live/thewthr.app/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/thewthr.app/privkey.pem;
    include              /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam          /etc/letsencrypt/ssl-dhparams.pem;

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }
}

Socket.io Backend:

const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

server.listen(process.env.PORT, () => console.log(`Express running → PORT ${server.address().port}`));

io.on('connection', () => console.log(`Socket.io running → PORT ${server.address().port}`));

Socket.io Client:

let socket = io.connect(`127.0.0.1:8443`);


Related Questions


nginx node.js & socket.io 504 timeout

Updated November 28, 2016 08:00 AM

Deploying node.js application on Ubuntu VPS

Updated May 24, 2017 08:00 AM