配置https服务器系列之一:自制ca证书并配置到nodejs-express服务器

1、自制证书:

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  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] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  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);  
posted @ 2017-03-06 12:24  白色的番茄  阅读(229)  评论(0编辑  收藏  举报