【问题标题】:Socket not working over https套接字无法通过 https 工作
【发布时间】:2018-02-12 08:43:12
【问题描述】:

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

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”标头的值 是“真,真”,当请求的凭据时必须是“真” 模式是“包括”。因此,原点 'https://staging.zikher.com' 是 不允许访问。发起请求的凭证模式 XMLHttpRequest 由 withCredentials 属性控制。

【问题讨论】:

  • 你的服务器端没有使用 HTTPS。
  • 我已经为 HTTPS 设置了 nginx。我确实在服务器端设置了 HTTPS,但由于 nginx 无法使其工作,需要一些指导。
  • 您能否将 NGINX 配置的相关部分(代理部分)添加到您的问题中?
  • 您可以编辑您的问题并将其添加到那里吗?您现在将其作为评论发布。
  • 您的服务器代码中有两个不同的地方将Access-Control-Allow-Credentials 响应标头的值设置为true。您需要删除其中一个。

标签: node.js http https socket.io


【解决方案1】:

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

在客户端试试这个:

var socket = io();

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

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

【讨论】:

  • 客户端和服务器端运行在不同的服务器上。所以我为服务器端定义了一个带有端口的IP。
  • @Shanky 好的,所以使用var socket = io('https://your.server/')(没有端口号)
  • XMLHttpRequest cannot load https://example.com/socket.io/?EIO=3&transport=polling&t=LvC2GP8. The value of the 'Access-Control-Allow-Credentials' header in the response is 'true, true' which must be 'true' when the request's credentials mode is 'include'. Origin 'https://example1.com' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
猜你喜欢
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
  • 2020-08-14
  • 2018-07-13
相关资源
最近更新 更多