socket.io返回通过nginx传输未知错误

时间:2019-05-08 18:07:37

标签: node.js nginx socket.io

我正在尝试通过Nginx连接到我的Nodejs服务器。

我的服务器在端口3000上运行,我的Nodejs客户端使用https://example.com:3000成功连接到服务器,而https://example.com/socket.io返回:

{"code":0,"message":"Transport unknown"}

我使用certbot进行SSL认证,并且它配置了我的nginx.conf

这是我的socket.io服务器的Nginx .conf文件:

upstream nodesocket {
        server  127.0.0.1:3000;
}

server {
        listen                  443 ssl;
        server_name             example.com www.example.com;

        access_log              /var/log/nginx/access-ssl.log;
        error_log               /var/log/nginx/error-ssl.log;

        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; 

        ssl_protocols           SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers             RC4:HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        keepalive_timeout       60;
        ssl_session_cache       shared:SSL:10m;
        ssl_session_timeout     10m;

        large_client_header_buffers 8 32k;


        location /socket.io/ {
                proxy_pass              https://nodesocket;
                proxy_redirect off;

                proxy_http_version      1.1;

                proxy_set_header        Upgrade                 $http_upgrade;
                proxy_set_header        Connection              "upgrade";

                proxy_set_header        Host                    $host;
                proxy_set_header        X-Real-IP               $remote_addr;
                proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
        }
}

这是我的套接字io服务器:

const express = require('express')
const https = require('https')
const socketIo = require('socket.io')
var fs = require('fs')

var options = {
    cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem'),
    key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
    ca: fs.readFileSync('/etc/letsencrypt/live/example.com/chain.pem')
}

var app = express();
var server = https.createServer(options, app);
var io = socketIo(server);


server.listen(3000, function(){
    console.log('listening on :3000');
  });

我检查了nodejs和nginx日志,但是没有相关的日志,也没有错误。

1 个答案:

答案 0 :(得分:0)

我遇到了完全相同的问题,这对我有用

我删除了proxy_http_version 1.1;

这是我的nginx.conf

location /socket.io/ {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_pass http://127.0.0.1:3000/socket.io/;
}