在虚拟专用服务器上运行 Ubuntu 18.04。安装 Nginx(用作代理),使用 PM2 为 Nodejs 应用程序提供服务。
工作了一个月,服务器突然返回 502:
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 [错误] 1009#1009:*19 连接到上游时没有实时上游,客户端:176.104.---.---,服务器:foo.bar.com,请求:“ GET /favicon.ico HTTP/2.0", 上游: " http://localhost/favicon.ico ", 主机: "foo.bar.com", referrer: " https://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() 连接到上游时失败(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 [错误] 1009#1009:*24 连接到上游时没有实时上游,客户端:176.104.---.---,服务器:foo.bar.com,请求:“ GET /favicon.ico HTTP/2.0", 上游: " http://localhost/favicon.ico ", 主机: "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)
Run Code Online (Sandbox Code Playgroud)
我试图只提供一个静态 HTML 文件并且它工作正常。我试图提供一个虚拟的 hello-world Expressjs 应用程序并且它工作正常。所以问题似乎出在 Angular 生成的server.js(通用引擎)上。
小智 5
502 Bad gateway 错误意味着 NGINX 无法访问您的后端。
确保 pm2 运行 NodeJS 应用程序并侦听 4000/TCP。
您可以通过curl或netstat使用以下命令简单地检查它:
netstat -ntlp | grep :4000
curl http://127.0.0.1:4000
Run Code Online (Sandbox Code Playgroud)