【发布时间】: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