NodeJS和NginX的SocketIO连接错误

时间:2015-04-21 02:48:23

标签: node.js ssl express nginx socket.io

当我尝试使用我的使用nginx的nodejs应用程序时,我收到以下错误:

    2015/04/20 22:34:08 [error] 29607#0: *1 connect() failed (111: Connection 
    refused) while connecting to upstream, client: myipaddress, server: 
    mydomain, request: "GET /socket.io/?EIO=3&transport=polling&
    t=1429583647703-9 HTTP/1.1", upstream: "http://127.0.0.1:3000    /socket.io
    /?EIO=3&transport=polling&t=1429583647703-9", host: "mydomain", 
    referrer: "http://mydomain"

我的nginx sites-available / default文件如下:

  server {
       listen 80;
       server_name mydomain;
       return 301 https://$server_name$request_uri;
    }

server {
    listen 443 ssl;
    server_name mydomain.com;
    ssl on;
    ssl_certificate /path/to/certificate;
    ssl_certificate_key /path/to/certificate-key;

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

}
   ssl_prefer_server_ciphers On;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers  ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3    DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5;

}

我正在使用express,它的http服务器配置如下:

app.set('port', process.env.PORT || 3000);
    var server = app.listen(app.get('port'), function() {
    debug('Express server listening on port ' + server.address().port);
});

var io = require("socket.io").listen(server);

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

事实证明我收到此错误是因为我的socket.io代码未正确验证。正确的方法可以在文档here中看到 用这样的东西:

io.use(function(socket, next) {
    var handshakeData = socket.request;
    // make sure the handshake data looks good as before
    // if error do this:
    // next(new Error('not authorized');
        // else just call next
        next();
});