我正在使用 Nginx 在端口 80 和 433 重定向(使用 SSL)上发布静态内容到 NodeJS。 Nginx的配置如下:
server {
listen 443 ssl;
ssl_certificate /opt/projetos/nodejs-project-ssl/vectortowns-cert.pem;
ssl_certificate_key /opt/projetos/nodejs-project-ssl/vectortowns-key.pem;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
server_name 127.0.0.1:443;
location / {
proxy_pass https://127.0.0.1:8443;
proxy_redirect off;
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 https;
}
使用 NodeJS、Express 和 EJS,我将动态内容发布到端口 8443,也配置为使用 HTTPS。请参阅下面的 javascript 代码(仅对问题重要的部分)。
// other codes...
/* Enable https */
var privateKey = fs.readFileSync('/opt/projetos/nodejs-project-ssl/vectortowns-key.pem');
var certificate = fs.readFileSync('/opt/projetos/nodejs-project-ssl/vectortowns-cert.pem');
var credentials = {
key: privateKey,
cert: certificate
};
// other codes...
/* Controllers */
app.use(require('./controllers'));
https.createServer(credentials, app).listen(
configuration.server.port,
configuration.server.address,
function(){
logger.info('Server started: ' + configuration.server.address + ':' + configuration.server.port);
});
我的问题是:
- 我是否需要在 Nginx 和 NodeJS 中或仅在其中之一中配置 SSL 证书和 key ?
- 如果我只需要一个,最好的选择是什么(NodeJS 或 Nginx)?
谢谢!!
最佳答案
为 SSL 使用 Nginx(并且仅使用 Nginx),这是标准。正如您所设置的,Nginx 用作反向代理,因此它将为您的程序提供本地未加密数据,用于端口 443 上给定的加密数据,因此如果您也在 Node 程序上使用 SSL,它将无法工作
关于node.js - 在 Nginx 和 NodeJS 上配置 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41817197/