我正在通过ssl连接到服务器上的端口…最近我开始无法连接到node.js + socket.io服务器时,无法在Chrome上加载资源:net :: ERR_INSECURE_RESPONSE错误。这是我的服务器设置上码:
var fs = require('fs'); var express = require('express'); var routes = require('./routes'); var https = require('https'); var path = require('path'); var socketio = require('socket.io'); var util = require('util'); var url = require('url'); var privateKey = fs.readFileSync('ssl/keys/xxxxxxxxxxxxxxxxxxxxxxxx.key', 'utf8'); var certificate = fs.readFileSync('ssl/certs/xxxxxxxxxxxxxx.crt', 'utf8'); var credentials = {key: privateKey, cert: certificate}; var sizeOf = require('image-size'); var DBWrapper = require('node-dbi').DBWrapper; var DBExpr = require('node-dbi').DBExpr; var dbConnectionConfig = { host: 'localhost', user: 'user', password: 'password', database: 'dbname' }; dbWrapper = new DBWrapper( "pg", dbConnectionConfig ); dbWrapper.connect(); var app = express(); app.set('port', process.env.PORT || 8080); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(express.logger('dev')); app.use(express.json()); app.use(express.urlencoded()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); app.get('/', routes.index); var server = https.createServer(credentials,app).listen(app.get('port'), function(){ console.log("Express server listening on port with https " + app.get('port')); }); var io = socketio.listen(server);
我究竟做错了什么?
编辑:这就是我在客户端连接的方式:
socket = io.connect("https://website.com:8080", {'reconnect': false});
使用官方而非自签名证书时,我遇到了类似的问题,而我才找到解决方案。顺便说一句,它只能在Android chrome上失败。将证书链添加到https.createServer方法的options对象:
var hskey = fs.readFileSync('/the_key.key'); var hscert = fs.readFileSync('/the_cert.pem') var ca = fs.readFileSync('/The_CA_bundle.pem') var credentials = { ca:ca, key: hskey, cert: hscert }; var server = https.createServer(credentials,app).listen(app.get('port'), function(){ console.log("Express server listening on port with https " + app.get('port')); });
只是将其放在此处,以防止其他人在遇到类似问题时将头撞到墙上。