Socket.io + node.js + express + ssl + Nginx - the client will not connect - 400 (failed request)

I have an express application on one server running nginx. I tested socket.io and it works fine on our dev server. Adding ssl to the mix led to the following console log on the client: the client console registers this As you can see, it produces 400 (failed request) and then successfully loads the same request, but my console logs have not started since the connection failed. .. would thank for the tip. Thank you This is the code:

This is my nginx configuration:

upstream example.com {
  server 127.0.0.1:3000;
}

server {
        listen 443 ssl;    
        server_name example.com;

        ssl_certificate       /path/to/nginx/ssl/example.crt;
        ssl_certificate_key   /path/to/nginx/ssl/example.key;

        location / {    
            proxy_pass  http://example.com;
            proxy_redirect off;

            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;

            proxy_set_header Host $host ;
            proxy_set_header X-Real-IP $remote_addr ;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
            proxy_set_header X-Forwarded-Proto https;      
        }    
        location /public {
            root /var/www/example/;
        }
        location /favicon.ico {
            root /var/www/example/public/favicon.ico;
        }
}
server {
    listen 443 ssl;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
}
server {
    listen 80;
    server_name example.com;
    return 301 https://example.com$request_uri;
}
server {
    listen 80;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
}

this is my code related to socket.io on express server

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

http.listen(port, function(){
    console.log('MyDomain.com app listening at ', 'http://localhost:'+port);

    io.on('connection', function(socket){
        console.log('socket.io connected','socket.id: '+socket.id);

        socket.on('new-user', function(data) {
            var user_id = data.user_id;
            console.log('new-user, user_id: ', user_id);
        });
    });
});

this is my client side that is trying to connect:

var socket = io.connect(null, { secure: true, port: 443, rememberTransport: false, 'reopen delay': 1000 });

//I have also tried these variations...
//-----------------------------------------
//var socket = io();
//var socket = io.connect('https://localhost', {secure: true});
//var socket = io.connect('https://example.com',{secure: true});
//var socket = io.connect('https://example.com');
//-----------------------------------------

socket.on('connect', function () { 
    console.log('on "connect"');
    socket.emit('new-user', {user_id:some_user_id});
});

in my html template i use this to load socket.io

<script src="/socket.io/socket.io.js"></script>

01: , URL- , - ... , ...?

+4

Source: https://habr.com/ru/post/1618323/


All Articles