溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Node怎么搭建https服務器

發布時間:2023-04-28 15:34:51 來源:億速云 閱讀:138 作者:iii 欄目:開發技術

本篇內容介紹了“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:設置自己的密碼,要記住,接下來的操作中要多次驗證??蛻舳撕头斩说拇a中要用,這里我設置的是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服務器”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女