繁体   English   中英

在节点服务器上安装SSL证书

[英]Installing SSL Certificate On Node Server

我创建了一个自签名证书,并将其安装在apache以及node.js(端口3000)上。 在localhost上, https://localhosthttps://localhost:3000运行良好。

因此,我购买了GoDaddy Standard SSL证书并将其安装在服务器上( http://gatherify.com )。 现在https://gatherify.com运行良好,但节点上的ssl无效。 当我访问https://gatherify.com:3000我得到“连接被中断”。

我执行curl:

root@host [~]# curl -v -s -k https://gatherify.com:3000
* About to connect() to gatherify.com port 3000 (#0)
*   Trying 108.160.156.123... connected
* Connected to gatherify.com (108.160.156.123) port 3000 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* warning: ignoring value of ssl.verifyhost
* NSS error -5938
* Closing connection #0
* SSL connect error

有什么建议来解决这个问题

更新 * 服务器端: *

var io = require('socket.io'), 
    connect = require('connect'), 
    fs = require('fs'),

var privateKey = fs.readFileSync('cert/server.key').toString();
var certificate = fs.readFileSync('cert/server.crt').toString();

var options = { 
    key: privateKey,
    cert: certificate
};

var app = connect(options).use(connect.static('../htdocs/node/'));
app.listen(3000);
var server = io.listen(app);

server.sockets.on('connection', function(socket) { 
console.log("Connected");
});

客户端:

<html> <head>

<script type = "text/javascript" src = "https://gatherify.com:3000/socket.io/socket.io.js"></script>

<script type = "text/javascript">

var socket = io.connect('https://gatherify.com:3000', {secure:true}); 

</script>

</head><body></body></html>

如果要使用(后面)HTTPS在端口3000上运行node.js应用程序,则需要在端口443上设置代理服务以将HTTPS请求代理到端口3000。

你没有提到你现在在端口443上运行的服务器(是Apache吗?)但你可能想要

  1. 将该服务移动到新端口(例如4000),然后在端口443上运行处理HTTPS的节点http代理
  2. 然后为您在端口3000上运行的node.js应用程序设置子域(例如blah.gatherify.com)。
  3. 然后,使用节点http代理 ,您将代理所有对“gatherify.com”发送到端口4000的请求,以及对“blah.gatherify.com”发送到端口3000的所有请求。

如果设置正确,用户可以访问“ https://gatherify.com ”或“ https://blah.gatherify.com ”(不使用:端口号),并且所有内容都使用SSL保护。 ;)

安装证书客户端(在Node.js中)

如果您需要node.js客户端能够识别您自己分配或廉价购买的SSL证书,您可以使用ssl-root-cas ,它可以在npm上使用

'use strict';

var https = require('https')
  , cas
  ;

// This will add the well-known CAs
// to `https.globalAgent.options.ca`
require('ssl-root-cas').inject();

cas = https.globalAgent.options.ca;

cas.push(fs.readFileSync(path.join(__dirname, 'ssl', '01-cheap-ssl-intermediary-a.pem')));
cas.push(fs.readFileSync(path.join(__dirname, 'ssl', '02-cheap-ssl-intermediary-b.pem')));
cas.push(fs.readFileSync(path.join(__dirname, 'ssl', '03-cheap-ssl-site.pem')));

这将使您的证书可用于核心https模块以及依赖它的模块,例如requestsocket.io-client而不会删除正常的ssl证书(由于某些奇怪的原因,这是默认行为)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM