使用openSSL构造一个支持https的nodejs服务器

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 首先通过下面的链接下载openSSLhttps://slproweb.com/products/Win32OpenSSL.html下载完毕后,执行openssl进入交互式界面:使用命令生成privatekey.pem 1024意思是1024位长度。

首先通过下面的链接下载openSSL
https://slproweb.com/products/Win32OpenSSL.html

下载完毕后,执行openssl进入交互式界面:

使用命令生成privatekey.pem 1024意思是1024位长度。

openssl genrsa -out privatekey.pem 1024

生成的privatekey.pem,打开看一看长啥样:

什么是pem文件?

.pem - Defined in RFCs 1421 through 1424, this is a container format that may include just the public certificate (such as with Apache installs, and CA certificate files /etc/ssl/certs), or may include an entire certificate chain including public key, private key, and root certificates. Confusingly, it may also encode a CSR (e.g. as used here) as the PKCS10 format can be translated into PEM. The name is from Privacy Enhanced Mail (PEM), a failed method for secure email but the container format it used lives on, and is a base64 translation of the x509 ASN.1 keys.

简单的说,就是一个密钥文件。

第二步,基于第一步生成的密钥文件生成一个证书请求:
openssl req -new -key privatekey.pem -out certrequest.csr

如果懒得维护证书明细,直接敲回车,会自动填入默认值:

最后基于第一步生成的密钥和证书请求生成一个数字证书:当然颁发机构就是自己了,仅用于测试目的。
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem

至此我们有了privatekey.pem和Certificate.pem两个证书了。

下面是我https服务器的代码,很简单,只有50几行:

var app = require('express')();
var fs    = require('fs');
var https = require('https');

var httpOptions =  {
 key: fs.readFileSync("keys/privatekey.pem"),
 cert: fs.readFileSync("keys/certificate.pem")
}

var server = https.createServer(httpOptions, app);
var io = require('socket.io')(server);

console.log("https server listens on port 8080...");

server.listen(8080);

function print_env(){
  console.log(process.env);
}

app.get('/', function (req, res) {

  var response = "Hello World";
  res.send(response);
});

app.get('/env', function (req, res) {

  print_env();
  // res.sendFile(__dirname + '/index.html');
  var response = JSON.stringify(process.env);
  res.send(response);
});

app.get('/redis', function (req, res) {

  var redisClient = require("./redisClient");
  
  function callback(response){
    // var response = "ok";//JSON.stringify(process.env);
    res.send(response);
  }
  redisClient.test(callback);
});

io.on('connection', function (socket) {
  console.log("connect comming from client: " + socket.id);
  socket.emit('messages_jerry', { hello: 'world greeting from Server!' });
  socket.on('messages', function (data) {
    console.log("data received from Client:" + JSON.stringify(data,2,2));
  });
});

从代码里不难理解这两个pem文件是如何用在https服务器里的。
最后在浏览器里测试。因为是自己颁发的证书,没有经过CA验证,所以浏览器会显示一个警告。

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
3月前
|
JavaScript Ubuntu
百度搜索:蓝易云【Ubuntu搭建Nodejs服务器】
请注意,以上步骤仅是简单的示例,实际上,你可以根据需要配置更多Node.js服务器设置和添加更多功能。
36 2
|
12天前
|
域名解析 网络协议 应用服务中间件
阿里云服务器配置免费https服务
阿里云服务器配置免费https服务
|
1月前
|
数据采集 缓存 前端开发
http和https请求服务器的时候在请求头部分都带什么到服务器呢?
HTTP和HTTPS请求头基本结构相似,HTTPS多了一层SSL/TLS加密。常见请求头如Accept(指定内容类型)、Authorization(身份验证)、Cookie(会话跟踪)、User-Agent(标识用户代理)等。HTTPS特有的头包括Upgrade-Insecure-Requests(升级到HTTPS)、Strict-Transport-Security(强制使用HTTPS)、Sec-Fetch-*(安全策略)和X-Content-Type-Options、X-Frame-Options等(增强安全性)。实际应用中,请求头会根据需求和安全策略变化。
20 0
|
1月前
|
监控 JavaScript 安全
监控内网电脑软件设计与实现:基于Node.js的服务器端架构分析
在当今信息技术高度发达的时代,监控内网电脑的需求日益增长。企业需要确保网络安全,个人用户也需要监控家庭网络以保护隐私和安全。本文将介绍一种基于Node.js的服务器端架构,用于设计和实现监控内网电脑软件。
99 0
|
1月前
|
网络安全 Apache PHP
Liunx服务器如何配置https(二)
Liunx服务器如何配置https(二)
35 0
Liunx服务器如何配置https(二)
|
1月前
|
Apache
Liunx服务器如何配置https(一)
Liunx服务器如何配置https(一)
32 0
Liunx服务器如何配置https(一)
|
1月前
|
弹性计算 网络安全 Apache
windows server2012服务器下PHPstudy配置ssl证书(https配置)
windows server2012服务器下PHPstudy配置ssl证书(https配置)
69 0
|
2月前
|
网络协议 Unix 应用服务中间件
如何进行 Nginx HTTPS服务器搭建
【2月更文挑战第6天】
63 0
|
3月前
|
JavaScript 前端开发 网络协议
如何使用Node.js快速创建本地HTTP服务器并实现异地远程访问
如何使用Node.js快速创建本地HTTP服务器并实现异地远程访问
75 0
|
1月前
|
前端开发
webpack如何设置devServer启动项目为https协议
webpack如何设置devServer启动项目为https协议
139 0