繁体   English   中英

除具有NodeJS / Express的3000以外的任何端口上的NGINX Bad Gateway 502

[英]NGINX Bad Gateway 502 on Any Port Except 3000 with NodeJS/Express

我在一些NodeJS实例前面有一个NGINX实例(1.4稳定版)。 我正在尝试使用上游模块像这样与NGINX进行负载平衡:

upstream my_web_upstream {
          server localhost:3000;
          server localhost:8124;
          keepalive 64;
        }



location / {
      proxy_redirect off;
      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_set_header   Connection "";
      proxy_http_version 1.1;
      proxy_cache one;
      proxy_cache_key sfs$request_uri$scheme;
      proxy_pass         http://my_web_upstream;
        }

当端口3000上的实例不可用时,会发生此问题。 我从NGINX得到了502 Bad Gateway。

如果我将上游配置更改为仅指向一个实例,例如8124,则仍会出现502。

运行netstat显示0个其他应用程序正在侦听我尝试的任何端口。

为什么NGINX报告网关错误? 如果其中一个实例发生故障,如何让NGINX进行故障排除?

如果netstat显示您的nodejs应用程序不在端口上运行,则问题在于您尚未启动nodejs应用程序。

此nginx配置知道如何代理到nodejs应用程序,但是如果未启动nodejs应用程序,则可以确保获得502。 如果要在多个端口上运行它,则必须在每个端口上启动应用程序。 因此,不要将端口3000硬编码到NodeJS代码中,而要使其从环境变量中获取该端口,或者不要使用诸如pm2( https://github.com/Unitech/pm2 )之类的流程管理器生成多个实例。 这些运行之后,nginx可以代理它们。

暂无
暂无

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

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