502:通过 Nginx 代理的 Nodejs 应用程序 Ubuntu 18.04

502: Nodejs app via Nginx proxy Ubuntu 18.04

设置

运行 一个 Ubuntu 18.04 在虚拟专用服务器上。安装了 Nginx(作为代理工作),使用 PM2 服务 Nodejs 应用程序。

问题

工作了一个月现在突然服务器returns一个502:

2018/10/10 00:39:51 [error] 1009#1009: *19 connect() failed (111: Connection refused) while connecting to upstream, client: 176.104.---.---, server: foo.bar.com, request: "GET / HTTP/2.0", upstream: "http://127.0.0.1:4000/", host: "foo.bar.com"

2018/10/10 00:39:51 [error] 1009#1009: *19 no live upstreams while connecting to upstream, client: 176.104.---.---, server: foo.bar.com, request: "GET /favicon.ico HTTP/2.0", upstream: "http://localhost/favicon.ico", host: "foo.bar.com", referrer: "https://foo.bar.com/"

2018/10/10 01:10:15 [error] 1009#1009: *24 connect() failed (111: Connection refused) while connecting to upstream, client: 176.104.--.---, server: foo.bar.com, request: "GET / HTTP/2.0", upstream: "http://[::1]:4000/", host: "foo.bar.com"

2018/10/10 01:10:15 [error] 1009#1009: *24 connect() failed (111: Connection refused) while connecting to upstream, client: 176.104.---.---, server: foo.bar.com, request: "GET / HTTP/2.0", upstream: "http://127.0.0.1:4000/", host: "foo.bar.com"

2018/10/10 01:10:15 [error] 1009#1009: *24 no live upstreams while connecting to upstream, client: 176.104.---.---, server: foo.bar.com, request: "GET /favicon.ico HTTP/2.0", upstream: "http://localhost/favicon.ico", host: "foo.bar.com", referrer: "https://foo.bar.com/"

配置:

重要

最重要的是 - 这个配置完美地工作了一个月。然后它停止工作。这发生在上传新版本的应用程序时。已回滚到以前的版本(整个系统的完整副本)。但是现在即使是以前的版本(有效)也不会出现相同的错误。所以在这里挠头。

防火墙

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
**/tcp                     ALLOW       ******
Nginx Full                 ALLOW       Anywhere
Nginx Full (v6)            ALLOW       Anywhere (v6)

更新

我试过只提供一个静态 HTML 文件并且它有效。我尝试提供一个虚拟的 hello-world Expressjs 应用程序并且它有效。所以问题似乎出在 Angular 生成的 server.js(通用引擎)。

502 Bad gateway 错误意味着 NGINX 无法访问您的后端。
确保 pm2 运行 NodeJS 应用程序并侦听 4000/TCP。
您可以使用以下命令通过 curlnetstat 简单地检查它:

netstat -ntlp | grep :4000
curl http://127.0.0.1:4000