Node搭建https服務器實例詳解
本文旨在分享搭建https服務器的過程,具體知識點以及相關概念請自行查詢。
第一步:創(chuàng)建文件目錄如下,在index中引用外部的script.js文件,server.js是服務器文件。

第二步:創(chuàng)建自己的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.

需要設置項均可以直接enter鍵默認跳過。

第三步:創(chuàng)建服務器端證書
//(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的錯誤,在根目錄下創(chuàng)建一
個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地址需要和你的服務器的監(jiān)聽地址一樣
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
第四步:創(chuàng)建客戶端證書
//(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文件中。

第六步:編寫服務端代碼
注意:代碼里面的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);
})第八步:打開瀏覽器驗證

參考資料:https://cnodejs.org/topic/54745ac22804a0997d38b32d
PS:這里需要注意一點,筆者在測試的時候,由于node.js相關模塊的版本問題,調用 fs.readFileSync 函數需要指明編碼類型,否則返回Buffer結果,并在運行時報錯!這種報錯在很多類似的網絡資料上都會遇到。
相關文章
Node.JS使用Sequelize操作MySQL的示例代碼
Node.JS提供了操作數據庫的基礎接口,本篇文章主要介紹了Node.JS使用Sequelize操作MySQL的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-10-10

