node启一个https服务

首先,我们需要一个私钥。

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

之后用该私钥生成一个证书,按照提示 输入国家缩写等信息 需要注意的是 common Name需要填写 认证的域名。

➜  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

之后对私钥和证书进行自签名的认证

➜  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

到此为止我们需要的三个基础文件已经准备就绪,之后就是用https模块起一个node服务。

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);

这时我们起服务的时候Chrome浏览器中显示不安全提醒,这是因为浏览器没有ca认证的证书。这时需要我们手动拷贝生成好的证书到系统中即可,如下图所示。

Paste_Image.png
Paste_Image.png

这样再会刷新页面,页面就已经是验证通过的状态了。

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 其实,我对https以前只有一个大概的了解,最近工作中遇到一个问题从而将https协议做了一个彻底的学习和认知,下...
    一条鱼的星辰大海阅读 8,767评论 0 1
  • HTTPS介绍 超文本传输安全协议(英语:Hypertext Transfer Protocol Secure,缩...
    齐滇大圣阅读 12,937评论 8 96
  • 图片发自简书App爱情有些像指甲,有些却像牙齿。 指甲是不断生长的,不要它,就剪掉它,不会刻骨铭心。 偶一不慎,剪...
    倪雪阅读 2,383评论 0 0
  • ndjd……精神打击打击打击打击新年新农村考察进行科学考察咖啡咖啡咖啡份可口的咖啡咖啡免费免费免费免费没错没错没错...
    海贼_猪猪阅读 2,532评论 0 0
  • Life's altered you, as it's altered me. And what would be...
    branchette阅读 2,958评论 2 0

友情链接更多精彩内容