我的服务器在 nginx 代理上运行 Node JS 时出现 502 Bad Gateway 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43818967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:12:25  来源:igfitidea点击:

502 Bad Gateway error for my server running with Node JS on nginx proxy

node.jsexpressnginx

提问by undefined

I am getting 502 bad gateway error: when I check the nginx error log I find this:

我收到 502 bad gateway 错误:当我检查 nginx 错误日志时,我发现:

2017/05/06 02:36:04 [error] 48176#0: *135 connect() failed (111: Connection refused) while connecting to upstream, client: 10.163.XX.X, server: abc-def-ghi, request: "GET /favicon.ico HTTP/1.1", upstream: "https://127.0.0.1:5300/favicon.ico", host: "hostnname", referrer: "hostname-1

2017/05/06 02:36:04 [错误] 48176#0:*135 连接()失败(111:连接被拒绝),同时连接到上游,客户端:10.163.XX.X,服务器:abc-def-ghi,请求:“GET /favicon.ico HTTP/1.1”,上游:“ https://127.0.0.1:5300/favicon.ico”,主机:“hostnname”,引用者:“hostname-1”

I searched internet enough but could not find anything. One thing to note here is that, this intermittent error is coming only on a particular page.

我在互联网上搜索了足够多但找不到任何东西。这里要注意的一件事是,这种间歇性错误仅出现在特定页面上。

Could this be a code issue? or nginx configuration issue> Can anyone please help me here.

这可能是代码问题吗?或 nginx 配置问题> 任何人都可以在这里帮助我。

Some of my nginx conf:

我的一些 nginx conf:

  upstream node_api_server {
    server localhost:5300 fail_timeout=0;
  }

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_read_timeout 5m;
    proxy_connect_timeout 5m;
    proxy_pass_header Set-Cookie;

    proxy_pass https://node_api_server;
    proxy_redirect off;
    proxy_buffer_size   128k;
    proxy_buffers   4 256k;
    proxy_busy_buffers_size   256k;
    break;
}

采纳答案by robertklep

502 errors are generally caused by NGINX being unable to pass a request to "upstream", in this case your Node.js server (which is also what the error message suggests: "Connection refused"").

502 错误通常是由 NGINX 无法将请求传递到“上游”引起的,在这种情况下是您的 Node.js 服务器(这也是错误消息所暗示的:“连接被拒绝”“)。

It may be crashing and restarting, so check its logfiles to see what's causing the crashes.

它可能会崩溃并重新启动,因此请检查其日志文件以查看导致崩溃的原因。

回答by Win Swarr

We got a 502 due to a path not having the correct case in a require call, in this case the file name. The code executed locally (in VS Code) but not when deployed.

由于路径在 require 调用中没有正确的大小写,我们得到了 502,在这种情况下是文件名。代码在本地(在 VS Code 中)执行,但在部署时不执行。

const repoName = require('../data/reponame'); //This should have been repoName

const repoName = require('../data/reponame'); //这应该是repoName

回答by Dimi Ansari

Simple trick: kill your nginx process and restart your UI

简单的技巧:杀死您的 nginx 进程并重新启动您的 UI

回答by Srinivas

Make sure you are running npm startor the script that runs the application.

确保您正在运行npm start或运行应用程序的脚本。