【问题标题】:NodeJS Express with SSL not working with Nginx reverse proxy带有 SSL 的 NodeJS Express 不适用于 Nginx 反向代理
【发布时间】:2017-09-27 05:37:15
【问题描述】:

当我在 Ubuntu 16.04 上设置 NodeJS 应用程序时,我遇到了一些奇怪的行为。应用程序仅适用于 http 依赖项,但不适用于 https 依赖项。

我的 NodeJS 应用程序在 8081 端口上运行,我正在使用带有 SSL 的 Nginx 反向代理将调用重定向到 8081 端口。以下是我在 Nginx site-enabled 目录中的 default.conf 文件。

# HTTP - redirect all requests to HTTPS:
server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;
        return 301 https://$host$request_uri;
}

# HTTPS - proxy requests on to local Node.js app:
server {
        listen 443;
        server_name test.com;

        ssl on;
        # Use certificate and key provided by Let's Encrypt:
        ssl_certificate /etc/letsencrypt/live/test.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/test.com/privkey.pem;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

        # Pass requests for / to localhost:8081:
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:8081/;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_cache_bypass $http_upgrade;
                proxy_redirect off;
        }
}

以下是我在 Node 服务器上运行的测试脚本。

var https = require('https');
https.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Welcome to Test App');
}).listen(8081, 'localhost');
console.log('Server running at http://localhost:8081/');

当我使用test.com 测试站点时,我收到了502 Bad Gateway。 但奇怪的是,当我将 https 依赖项更改为 http 时,一切都像魅力一样。

奇怪的行为可能是什么问题?我们不能在 Nginx 的 SSL 设置中使用 https 吗? 由于我希望使用受信任的对等连接,因此也有必要将 https 与 NodeJS 一起使用。

【问题讨论】:

  • nginx 错误日志(/var/log/nginx)是否显示了什么?另外,如果不使用任何转发,是否可以毫无问题地访问nginx默认主页?
  • 另外,据我所知,您正在https://localhost:8081/ 上广播(尽管没有在节点服务器上为您的测试配置安全密钥)但转发到http://localhost:8081/ 的非https URL
  • @nb1987 有一个。 23828#23828: *32 上游提前关闭连接,同时从上游读取响应头,客户端:123.123.106.254,服务器:test.com,请求:“GET / HTTP/1.1”,上游:“127.0.0.1:8081”,主机:“ test.com”
  • 好的...我还有几个问题。 1.) proxy_pass 的值仍然是 http://localhost:8081/; 吗?如果您将其设置为 https://localhost:8081/; ,您仍然会收到相同的错误吗? 2.) 你能看到 Node 本身抛出了什么错误吗?您可以将http.createServer 的结果分配给一个变量并监听错误:例如,var server = http.createServer(function ... 然后server.on('error', function (e) { console.log(e); });
  • 我只是将它设置为https://localhost:8081/ 并且它正在工作。我以前试过这个,但没有奏效。它现在正在工作。请添加为接受它的答案。

标签: node.js express ssl nginx proxy


【解决方案1】:

由于您的 Node.js 应用程序正在侦听 https,因此您的 Nginx 服务器应该将请求转发到 https://localhost:8081/ 而不是 http://localhost:8081,因此您应该将您的 proxy_pass 值设置为 https://localhost:8081/

【讨论】:

  • 这工作正常,直到我在 NodeJS 中添加 SSL 选项。 var options = { key: fs.readFileSync(config.keypath), ca: [fs.readFileSync(config.capath)], cert: fs.readFileSync(config.certpath), requestCert: true, rejectUnauthorized: true }; 使用此代码,特别是当我添加 requestCert: true 时,此错误再次开始发生。
  • 如果没有requestCert,连接是否可以正常工作(直接或通过 Nginx)?此外,requestCert 的默认设置是false,在典型配置中它确实是false。但是,如果您确定需要将其设置为 true,我建议您尝试找出 Node 本身报告的错误。您是否可以尝试通过使用我在 cmets 中为您的初始问题提供的示例代码向您的服务器添加一个此类事件侦听器来侦听 error 事件?那么,Node 会报什么错误呢?
  • 我用过。没有错误。当我使用requestCert: true 时,NodeJS 脚本在test.com:8081 上运行良好,但在裸域上却不行。我需要使用它,因为我希望只有拥有证书的用户才能访问它。
  • 老实说,在我自己的应用程序中,我从未将 Node 服务器暴露给外部,因此我只需要在 Nginx 服务器上配置 TLS/SSL。但是,我怀疑在这种情况下,您需要通过在 location / 块下设置 proxy_ssl_certificateproxy_ssl_certificate_key 指令来向上游服务器验证 Nginx。本指南可能会有所帮助:nginx.com/resources/admin-guide/nginx-https-upstreams
猜你喜欢
  • 2020-11-22
  • 2019-05-12
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
相关资源
最近更新 更多