node啟一個https服務(wù)

首先,我們需要一個私鑰。

?  https  openssl genrsa -out privatekey.pem 1024
Generating RSA private key, 1024 bit long modulus
...........................++++++
..........................++++++
e is 65537 (0x10001)

之后用該私鑰生成一個證書,按照提示 輸入國家縮寫等信息 需要注意的是 common Name需要填寫 認(rèn)證的域名。

?  https  openssl req -new -key privatekey.pem -out certrequest.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:beijing
Locality Name (eg, city) []:neijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:qunar
Organizational Unit Name (eg, section) []:qunar
Common Name (e.g. server FQDN or YOUR name) []:eva.li.qunar.com
Email Address []:xilixinluo@gmail.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:eva.li
An optional company name []:qunar

之后對私鑰和證書進(jìn)行自簽名的認(rèn)證

?  https  openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
Signature ok
subject=/C=CN/ST=beijing/L=neijing/O=qunar/OU=qunar/CN=eva.li.qunar.com/emailAddress=xilixinluo@gmail.com
Getting Private key

到此為止我們需要的三個基礎(chǔ)文件已經(jīng)準(zhǔn)備就緒,之后就是用https模塊起一個node服務(wù)。

var https = require('https')
    ,fs = require("fs");

var options = {
    key: fs.readFileSync('./privatekey.pem'),
    cert: fs.readFileSync('./certificate.pem'),
    ca: fs.readFileSync('./certrequest.pem')
};

https.createServer(options, function(req,res){
    res.writeHead(200, {
        'Content-type' : 'text/html'
    });
    res.write('<h1>HTTPS server</h1>');
    res.end('<p>Hello baby!</p>');
}).listen(8888);

這時我們起服務(wù)的時候Chrome瀏覽器中顯示不安全提醒,這是因?yàn)闉g覽器沒有ca認(rèn)證的證書。這時需要我們手動拷貝生成好的證書到系統(tǒng)中即可,如下圖所示。

Paste_Image.png
Paste_Image.png

這樣再會刷新頁面,頁面就已經(jīng)是驗(yàn)證通過的狀態(tài)了。

Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 其實(shí),我對https以前只有一個大概的了解,最近工作中遇到一個問題從而將https協(xié)議做了一個徹底的學(xué)習(xí)和認(rèn)知,下...
    一條魚的星辰大海閱讀 3,544評論 0 1
  • HTTPS介紹 超文本傳輸安全協(xié)議(英語:Hypertext Transfer Protocol Secure,縮...
    齊滇大圣閱讀 9,219評論 8 96
  • 圖片發(fā)自簡書App愛情有些像指甲,有些卻像牙齒。 指甲是不斷生長的,不要它,就剪掉它,不會刻骨銘心。 偶一不慎,剪...
    倪雪閱讀 331評論 0 0
  • ndjd……精神打擊打擊打擊打擊新年新農(nóng)村考察進(jìn)行科學(xué)考察咖啡咖啡咖啡份可口的咖啡咖啡免費(fèi)免費(fèi)免費(fèi)免費(fèi)沒錯沒錯沒錯...
    海賊_豬豬閱讀 350評論 0 0
  • Life's altered you, as it's altered me. And what would be...
    branchette閱讀 442評論 2 0

友情鏈接更多精彩內(nèi)容