在CentOS上配置Node.js应用程序的SSL证书,通常需要以下几个步骤:
获取SSL证书:
安装Certbot:
sudo yum install epel-release
sudo yum install certbot python2-certbot-nginx
获取证书:
example.com
,运行以下命令:sudo certbot --nginx -d example.com -d www.example.com
配置Node.js应用程序:
http
模块来创建一个简单的HTTPS服务器:const https = require('https');
const fs = require('fs');
const express = require('express');
const app = express();
// 读取SSL证书文件
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem')
};
// 创建HTTPS服务器
https.createServer(options, app).listen(443, () => {
console.log('HTTPS Server running on port 443');
});
重定向HTTP到HTTPS:
server {
listen 80;
server_name example.com www.example.com;
location / {
return 301 https://$host$request_uri;
}
}
测试配置:
自动续订证书:
sudo certbot renew --dry-run
通过以上步骤,你可以在CentOS上成功配置Node.js应用程序的SSL证书。确保定期检查和更新证书,以保持安全性。