node.js - 在 Nginx 和 NodeJS 上配置 HTTPS

标签 node.js ssl nginx https

我正在使用 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);
});

我的问题是:

  1. 我是否需要在 Nginx 和 NodeJS 中或仅在其中之一中配置 SSL 证书和 key ?
  2. 如果我只需要一个,最好的选择是什么(NodeJS 或 Nginx)?

谢谢!!

最佳答案

为 SSL 使用 Nginx(并且仅使用 Nginx),这是标准。正如您所设置的,Nginx 用作反向代理,因此它将为您的程序提供本地未加密数据,用于端口 443 上给定的加密数据,因此如果您也在 Node 程序上使用 SSL,它将无法工作

关于node.js - 在 Nginx 和 NodeJS 上配置 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41817197/

相关文章:

javascript - 开始使用 NodeJS - 在 nodeJS 中找不到模块

node.js - Node Js : Nested API calls: Watch a directory for files, 从每个文件读取数据并从 API 获取 ID,在另一个 API 中使用 ID 发布数据

ruby-on-rails - 使用 Dokku + nginx + unicorn 的未初始化常量 ActiveRecord (NameError)

node.js - 世博会启动后崩溃

node.js - KeystoneJS:管理 UI 中的关系未更新

java - 如何使用 Java 应用程序为我的 AppEngine 激活 SSL?

ssl - 什么可能是这个 : ** Command Error: SSL Error: error:14077410:SSL routines:SSL23_GET_SERVER_HEL LO:sslv3 alert handshake failure 的原因

python - 请求无法连接到 TLS 服务器

regex - 在Apache和Nginx上将URL重写为查询字符串

node.js - 我的 Node.js 应用程序在将其放在 nginx 后面后,在写入请求时随机收到未处理的 'error' 事件