Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用Nginx和Express的Socket.io SSL连接_Node.js_Ssl_Express_Nginx_Socket.io - Fatal编程技术网

Node.js 使用Nginx和Express的Socket.io SSL连接

Node.js 使用Nginx和Express的Socket.io SSL连接,node.js,ssl,express,nginx,socket.io,Node.js,Ssl,Express,Nginx,Socket.io,我试图在运行socket.io和express和nginx的应用程序上使用SSL,但无法使其正常工作。我已经做了研究,但我发现的都不管用 我一直有这样的错误:ERR_CONNECTION_关闭,客户端没有http状态代码 GET https://subdomain.mywebsite.com:1339/socket.io/?EIO=3&transport=polling&t=LIgxHmz net::ERR_CONNECTION_CLOSED 以下是我的nginx配置: ser

我试图在运行socket.io和express和nginx的应用程序上使用SSL,但无法使其正常工作。我已经做了研究,但我发现的都不管用

我一直有这样的错误:ERR_CONNECTION_关闭,客户端没有http状态代码

GET https://subdomain.mywebsite.com:1339/socket.io/?EIO=3&transport=polling&t=LIgxHmz net::ERR_CONNECTION_CLOSED
以下是我的nginx配置:

server {
listen 443;
server_name subdomain.mywebsite.com;

root /usr/share/nginx/html;
index index.html index.htm;

ssl on;
# Use certificate and key provided by Let's Encrypt:
ssl_certificate /etc/letsencrypt/live/subdomain.mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/subdomain.mywebsite.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://localhost:1339/;
    proxy_redirect off;

    # Socket.IO Support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
以下是服务器端:

var express = require('express'),

var app = express();

var server = require('http').createServer(app);
var io = require('socket.io')(server);

server.listen(1339);

...
以下是客户端: subdomain.mywebsite.com

var socket = io.connect("https://subdomain.mywebsite.com:1339");
页面加载很好,服务器端没有错误,但没有连接到socket.io。 在我尝试切换到SSL之前,一切都完美无瑕


我做错了什么?

尝试此配置。请确保具有正确的SSL证书和密钥。若您在本地测试,那个么您可以很容易地使用该工具生成SSL证书以进行本地测试

server {
   listen 80;
   server_name <your server_name>;
   return 301 https://<your server_name>$request_uri;
}

server {
   listen 443 ssl;


   ssl_certificate <your certificate path>;  # better if you put them at /etc/nginx/ssl/
   ssl_certificate_key <your certificate_key path >;

   server_name <your server_name>;

   location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header X-Client-Verify SUCCESS;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_pass http://localhost:3000;
      proxy_redirect off;
      proxy_buffering off;
   }
}
服务器{
听80;
服务器名称;
返回301 https://$request\u uri;
}
服务器{
听443ssl;
ssl_certificate;#最好将它们放在/etc/nginx/ssl/
ssl_证书_密钥;
服务器名称;
地点/{
代理集头X-Real-IP$remote\u addr;
$remote\u addr的代理\u集\u头X转发;
代理设置头X-Client-Verify成功;
代理设置头主机$http\U主机;
proxy\u set\u头X-NginX-proxy true;
proxy_http_版本1.1;
代理设置头升级$http\U升级;
代理设置头连接“升级”;
代理通行证http://localhost:3000;
代理_重定向关闭;
代理缓冲关闭;
}
}

根据您使用的nginx版本,它可能与移动ssl标志一样简单。我试过了,但没有改变。Express正在用https为我的html页面提供服务。只有socket.io出现故障。您不应该使用
wss
协议从客户端连接吗?我也尝试过,但仍然是一样的。我过去也有相同的要求,我已经设法找到了有效的解决方案。请参阅我的问题和答案,了解我的nginx+express+客户端配置,它可能会帮助您: