繁体   English   中英

使用SSL的NodeJS Express无法使用Nginx反向代理

[英]NodeJS Express with SSL not working with Nginx reverse proxy

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

我的NodeJS应用程序在端口8081上运行,我使用带有SSL的Nginx反向代理将调用重定向到8081端口。 以下是我在site-enabled 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时,一切都像魅力一样。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM