套接字无法通过https工作

时间:2017-09-04 06:35:35

标签: node.js http https socket.io

无法使用套接字在服务器和客户端之间建立连接。获得以下错误。

GET https://**.**.**.**:9000/socket.io/?EIO=3&transport=polling&t=LvBYNwb net::ERR_CONNECTION_CLOSED
Request.create @ socket.io.js:1456
Request @ socket.io.js:1369
XHR.request @ socket.io.js:1297
XHR.doPoll @ socket.io.js:1327
Polling.poll @ socket.io.js:1740
Polling.doOpen @ socket.io.js:1684
Transport.open @ socket.io.js:827
Socket.open @ socket.io.js:248
Socket @ socket.io.js:129
Socket @ socket.io.js:55
Manager.open.Manager.connect @ socket.io.js:4549
(anonymous) @ socket.io.js:4859
setTimeout (async)
Manager.reconnect @ socket.io.js:4849
(anonymous) @ socket.io.js:4863
(anonymous) @ socket.io.js:4570
Emitter.emit @ socket.io.js:2556
Socket.onError @ socket.io.js:688
(anonymous) @ socket.io.js:279
Emitter.emit @ socket.io.js:2556
Transport.onError @ socket.io.js:814
(anonymous) @ socket.io.js:1333
Emitter.emit @ socket.io.js:2556
Request.onError @ socket.io.js:1502
(anonymous) @ socket.io.js:1449
setTimeout (async)
xhr.onreadystatechange @ socket.io.js:1448
XMLHttpRequest.send (async)
Request.create @ socket.io.js:1456
Request @ socket.io.js:1369
XHR.request @ socket.io.js:1297
XHR.doPoll @ socket.io.js:1327
Polling.poll @ socket.io.js:1740
Polling.doOpen @ socket.io.js:1684
Transport.open @ socket.io.js:827
Socket.open @ socket.io.js:248
Socket @ socket.io.js:129
Socket @ socket.io.js:55
Manager.open.Manager.connect @ socket.io.js:4549
(anonymous) @ socket.io.js:4859

服务器(node.js)和客户端都通过HTTPS。如果客户端是通过HTTP,套接字工作正常,但一旦更改为HTTPS结果就是错误。 无法将套接字配置为使用HTTPS。

服务器端:

import express from 'express';
import http from 'http';
var app = express();
var server = require('http').Server(app);
var socketio = require('socket.io').listen(server, { log: true });

客户端:

var socket = io('localhost:9000');

你走了:

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_pass http://localhost:9000;
proxy_set_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';
proxy_set_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Methods' 'GET,DELETE, POST,PUT, OPTIONS';
add_header 'Content-Type' 'application/json';

Nginx HTTPS配置:

listen 443 ssl default_server;
if ($ssl_protocol = "") {
rewrite ^   https://$server_name$request_uri? permanent;
}
ssl_certificate /etc/example.com/ssl-bundle.crt;
ssl_certificate_key /etc/example.com/example.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

这是我现在在example.com上的错误:

  

XMLHttpRequest无法加载   https://example.com/socket.io/?EIO=3&transport=polling&t=LvC2GP8。该   响应中“Access-Control-Allow-Credentials”标头的值   是'true,true',在请求的凭据时必须为'true'   模式是'包含'。因此,“https://staging.zikher.com”来源   不允许访问。由...发起的请求的凭据模式   XMLHttpRequest由withCredentials属性控制。

1 个答案:

答案 0 :(得分:1)

假设NGINX在端口443上运行,您的客户端连接到错误的端口(9000,这是Express服务器正在侦听的,但客户端应连接到NGINX代理)。

在客户端中尝试:

var socket = io();

或者,通过HTTPS连接到特定服务器(和HTTPS端口,443):

var socket = io('https://server.example.com')