Node怎么搭建https服务器

发布时间:2023-04-28 15:34:51 作者:iii
来源:亿速云 阅读:105

本篇内容介绍了“Node怎么搭建https服务器”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

第一步:创建文件目录如下,在index中引用外部的script.js文件,server.js是服务器文件。

Node怎么搭建https服务器

 第二步:创建自己的CA机构.

在根文件夹下打开命令行工具,直接依次使用下面的命令。

//为CA生成私钥
 openssl genrsa -out ca-key.pem -des 1024
//通过CA私钥生成CSR
 openssl req -new -key ca-key.pem -out ca-csr.pem
//通过CSR文件和私钥生成CA证书
 openssl x509 -req -in ca-csr.pem -signkey ca-key.pem -out ca-cert.pem

 注意下面的运行结果:enter pass phrase for ca-key.pem:设置自己的密码,要记住,接下来的操作中要多次验证。客户端和服务端的代码中要用,这里我设置的是sun13083691283.

Node怎么搭建https服务器

 需要设置项均可以直接enter键默认跳过。

 Node怎么搭建https服务器

第三步:创建服务器端证书 

//(1)为服务器生成私钥
 openssl genrsa -out server-key.pem 1024
//(2)利用服务器私钥文件服务器生成CSR
 openssl req -new -key server-key.pem -config openssl.cnf -out server-csr.pem

这一步可能会报错,Unable to load config info from /user/local/ssl/openssl.cnf,或者有关openssl.cnf的错误,在根目录下创建一

个openssl.cnf的文件,将下面的代码拷贝进去。

[req]  
    distinguished_name = req_distinguished_name  
    req_extensions = v3_req  
    [req_distinguished_name]  
    countryName = Country Name (2 letter code)  
    countryName_default = CN  
    stateOrProvinceName = State or Province Name (full name)  
    stateOrProvinceName_default = BeiJing  
    localityName = Locality Name (eg, city)  
    localityName_default = YaYunCun  
    organizationalUnitName  = Organizational Unit Name (eg, section)  
    organizationalUnitName_default  = Domain Control Validated  
    commonName = Internet Widgits Ltd  
    commonName_max  = 64  
    [ v3_req ]  
    # Extensions to add to a certificate request  
    basicConstraints = CA:FALSE  
    keyUsage = nonRepudiation, digitalSignature, keyEncipherment  
    subjectAltName = @alt_names  
    [alt_names]  
	#注意这个IP.1的设置,IP地址需要和你的服务器的监听地址一样
    IP.1 = 127.0.0.1
//(3)通过服务器私钥文件和CSR文件生成服务器证书
 openssl x509 -req -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -in server-csr.pem -out server-cert.pem -extensions v3_req -extfile openssl.cnf

 第四步:创建客户端证书

//(1)生成客户端私钥
 openssl genrsa -out client-key.pem
//(2)利用私钥生成CSR
 openssl req -new -key client-key.pem -out client-csr.pem
//(3)生成客户端证书
 openssl x509 -req -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -in client-csr.pem -out client-cert.pem

第五步:将证书打包

//(1)打包服务器端证书
 openssl pkcs12 -export -in server-cert.pem -inkey server-key.pem -certfile ca-cert.pem -out server.pfx
//(2)打包客户端证书
 openssl pkcs12 -export -in client-cert.pem -inkey client-key.pem -certfile ca-cert.pem -out client.pfx

打包结束后在文件夹下会看到如下文件,将其放到keys文件中。 

Node怎么搭建https服务器

第六步:编写服务端代码

注意:代码里面的passphrase要填写之前设置的密码,sun13083691283

var https = require('https');
var fs = require('fs');
var options = {
	pfx:fs.readFileSync('./keys/server.pfx','utf-8'),
	passphrase:'your password'
};
https.createServer(options,function(req,res){
	res.writeHead(200);
	res.end('hello world\n');
}).listen(3000,'127.0.0.1');

第七步:编写客户端代码

注意:代码里面的passphrase要填写之前设置的密码,sun13083691283

var https = require('https');
var fs = require('fs');
var options = {
	hostname:'127.0.0.1',
	port:3000,
	path:'/',
	method:'GET',
	pfx:fs.readFileSync('./keys/server.pfx','utf-8'),
	passphrase:'your password',
	agent:false
};
options.agent = new https.Agent(options);
var req = https.request(options,function(res){
console.log("statusCode: ", res.statusCode);
  console.log("headers: ", res.headers);
	res.setEncoding('utf-8');
	res.on('data',function(d){
		console.log(d);
	})
});
req.end();
req.on('error',function(e){
	console.log(e);
})

第八步:打开浏览器验证 

Node怎么搭建https服务器

“Node怎么搭建https服务器”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. https 服务器搭建过程
  2. Node中怎么启动https服务器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

node https

上一篇:java多线程怎么通过CompletableFuture组装异步计算单元

下一篇:docker中怎么配置hosts

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》