在Debian系统上为Node.js应用程序配置SSL证书,通常需要以下几个步骤:
获取SSL证书:
安装Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx
配置Nginx:
/etc/nginx/sites-available/default
),添加SSL配置:server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
proxy_pass http://localhost:3000; # 你的Node.js应用监听的端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
获取并安装证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
配置Node.js应用:
const express = require('express');
const https = require('https');
const fs = require('fs');
const app = express();
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/fullchain.pem')
};
app.get('/', (req, res) => {
res.send('Hello, HTTPS!');
});
https.createServer(options, app).listen(443, () => {
console.log('Server is running on https://yourdomain.com');
});
自动续订证书:
sudo certbot renew --dry-run
通过以上步骤,你可以在Debian系统上为Node.js应用程序配置SSL证书。确保你的域名解析正确,并且防火墙允许HTTP(80)和HTTPS(443)流量。