博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
配置https服务器系列之一:自制ca证书并配置到nodejs-express服务器
阅读量:6968 次
发布时间:2019-06-27

本文共 2429 字,大约阅读时间需要 8 分钟。

1、自制证书:

[plain]   
 
 
  1. // 生成私钥  
  2. D:\working\zproject-nodejs\https>openssl genrsa -out privatekey.pem 1024  
  3. Generating RSA private key, 1024 bit long modulus  
  4. .................++++++  
  5. .............++++++  
  6. e is 65537 (0x10001)  
  7. // 通过私钥生成CSR证书签名  
  8. D:\working\zproject-nodejs\https>openssl req -new -key privatekey.pem -out certr  
  9. equest.csr  
  10. You are about to be asked to enter information that will be incorporated  
  11. into your certificate request.  
  12. What you are about to enter is what is called a Distinguished Name or a DN.  
  13. There are quite a few fields but you can leave some blank  
  14. For some fields there will be a default value,  
  15. If you enter '.', the field will be left blank.  
  16. -----  
  17. Country Name (2 letter code) [AU]:CN  
  18. State or Province Name (full name) [Some-State]:省份  
  19. Locality Name (eg, city) []:城市  
  20. Organization Name (eg, company) [Internet Widgits Pty Ltd]:xxx.com  
  21. Organizational Unit Name (eg, section) []:xxx.com  
  22. Common Name (e.g. server FQDN or YOUR name) []:名 姓  
  23. Email Address []:xxx@qq.com  
  24.   
  25. Please enter the following 'extra' attributes  
  26. to be sent with your certificate request  
  27. A challenge password []:  
  28. An optional company name []:  
  29.   
  30. // 通过私钥和证书签名生成证书文件  
  31. D:\working\zproject-nodejs\https>openssl x509 -req -in certrequest.csr -signkey  
  32. privatekey.pem -out certificate.pem  
  33. Signature ok  
  34. subject=/C=CN/ST=***/L=***/O=***.com/OU=***.com/CN=***/emailAddress=***@qq.com  
  35. Getting Private key  

2、配置到nodejs-express服务器

将第一步生成的3个文件拷到app.js同目录下。
然后修改bin\www文件。
[javascript]   
 
 
  1. /** 
  2.  * Module dependencies. 
  3.  */  
  4.   
  5. var app = require('../app');  
  6. var debug = require('debug')('VidzyTr14:server');  
  7. var http = require('http');  
  8. //添加  
  9. var https = require('https');  
  10. var fs = require('fs');  
  11.   
  12. /** 
  13.  * Get port from environment and store in Express. 
  14.  */  
  15.   
  16. var port = normalizePort(process.env.PORT || '80'||'443');  
  17. app.set('port', port);  
  18.   
  19. /** 
  20.  * Create HTTP server. 
  21.  */  
  22.   
  23. var server = http.createServer(app);  
  24.   
  25. //添加 Create HTTPS server.  
  26. var options = {  
  27.     key: fs.readFileSync('./privatekey.pem'),  
  28.     cert: fs.readFileSync('./certificate.pem')  
  29. };  
  30. var httpsServer = https.createServer(options,app);  
  31.   
  32.   
  33. /** 
  34.  * Listen on provided port, on all network interfaces. 
  35.  */  
  36. var httpPort = normalizePort(process.env.PORT || '80');  
  37. server.listen(httpPort);  
  38. server.on('error', onError);  
  39. server.on('listening', onListening);  
  40.   
  41. // 添加 监听  
  42. var httpsPort = normalizePort('443');  
  43. httpsServer.listen(httpsPort);  
  44. httpsServer.on('error',onError);  
  45. httpsServer.on('listening',onListening);  

转载于:https://www.cnblogs.com/syuee/p/6509380.html

你可能感兴趣的文章
Javascript单线程实现
查看>>
火电电厂相关业务知识
查看>>
Magento 模版路径
查看>>
Taking water into exams could boost grades 考试带瓶水可以提高成绩?
查看>>
一对多和多对一的关系,用mybatis写
查看>>
七 递归与二分法、匿名函数、内置函数
查看>>
hdu 1001
查看>>
JavaScript速记
查看>>
两栏布局,三栏布局,等高布局,流式布局
查看>>
CSS3弹性盒模型之box-orient & box-direction
查看>>
Codeforces Beta Round #77 (Div. 2 Only) A. Football【字符串/判断是否存在连续7个0或7个1】...
查看>>
洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]
查看>>
动态规划题库
查看>>
[30期] 第一个项目
查看>>
[31期] 第一个项目结束之际-->俺很高调但很真诚地感谢一个人-->涛爷
查看>>
回头再看第一次项目
查看>>
sql 50题
查看>>
Unity3D编辑器之重写Hierarchy的右键菜单
查看>>
有无关键字new的区别
查看>>
svn idea使用
查看>>