简体   繁体   English

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

[英]502: Nodejs app via Nginx proxy Ubuntu 18.04

Setup 设定

Running an Ubuntu 18.04 on a Virtual Private Server. 在虚拟专用服务器上运行Ubuntu 18.04。 Installed Nginx (to work as a proxy), serving Nodejs app using PM2. 安装了Nginx(用作代理),使用PM2服务Nodejs应用程序。

Problem 问题

Worked for a month now suddenly the server returns a 502: 现在工作了一个月,服务器突然返回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 [错误] 1009#1009:* 19连接到上游时,connect()失败(111:连接被拒绝),客户端:176.104 .---.---,服务器:foo。 bar.com,请求:“ GET / HTTP / 2.0”,上游:“ http://127.0.0.1:4000/ ”,主机:“ 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 00:39:51 [错误] 1009#1009:* 19连接上游时没有实时上游,客户端:176.104 .---.---,服务器:foo.bar.com,请求:“ GET /favicon.ico HTTP / 2.0”,上游:“ http://localhost/favicon.ico ”,主机:“ foo.bar.com”,引荐来源网址:“ 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 [错误] 1009#1009:* 24连接到上游时,connect()失败(111:连接被拒绝),客户端:176.104 .--.---,服务器:foo.bar .com,请求:“ GET / HTTP / 2.0”,上游:“ http:// [:: 1]:4000 / ”,主机:“ 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 [错误] 1009#1009:* 24连接到上游时失败connect()(111:连接被拒绝),客户端:176.104 .---.---,服务器:foo。 bar.com,请求:“ GET / HTTP / 2.0”,上游:“ http://127.0.0.1:4000/ ”,主机:“ 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/ " 2018/10/10 01:10:15 [错误] 1009#1009:* 24连接上游时没有实时上游,客户端:176.104 .---.---,服务器:foo.bar.com,请求:“ GET /favicon.ico HTTP / 2.0”,上游:“ http://localhost/favicon.ico ”,主机:“ foo.bar.com”,引荐来源网址:“ https://foo.bar.com/

Configs: 配置:

Important 重要

The most important - this config worked perfectly for month. 最重要的-此配置可以完美使用一个月。 Then it stopped working. 然后它停止工作。 This happened when the new version of the app was uploaded. 上载新版本的应用程序时发生了这种情况。 Have rolled back to the previous version (full copy of the whole system). 已回滚到以前的版本(整个系统的完整副本)。 But now even previous version (which worked) doesn't work with the same error. 但是现在,即使以前的版本(有效)也无法使用相同的错误。 So scratching the head here. 所以在这里挠头。

Firewall 防火墙功能

sudo ufw status
Status: active

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

Update 更新资料

I have tried to serve just a static HTML file and it worked. 我试图提供一个静态HTML文件,但它确实起作用。 I have tried to serve a dummy hello-world Expressjs app and it worked. 我曾尝试提供一个虚拟的hello-world Expressjs应用,但它确实有效。 So the problem seems to be with the Angular's generated server.js (Universal engine). 因此,问题似乎出在Angular生成的server.js (通用引擎)上。

The 502 Bad gateway error means your backend is not reachable by NGINX. 502错误网关错误表示NGINX无法访问您的后端。
Make sure pm2 runs the NodeJS app and listens on 4000/TCP. 确保pm2运行NodeJS应用程序并侦听4000 / TCP。
You can simply check it by curl or netstat with these commands: 您可以使用以下命令通过curlnetstat对其进行简单检查:

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

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

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