连接到上游时 NodeJS connect() 失败(111:连接被拒绝)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29370360/
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:35:53  来源:igfitidea点击:

NodeJS connect() failed (111: Connection refused) while connecting to upstream

node.jsamazon-web-servicesamazon-elastic-beanstalk

提问by cphill

I am running into an issue today where all of a sudden my Elastic Beanstalk app is sending me to a 502 Bad Gatewaypage. Now I have run into this issue in the past and the reason why this was happening was because the Node command could not start my server. I fixed this by inputting Node command: node main.jsand I never ran into this issue until randomly this morning. All of a sudden it stopped working and I get this error, in my error log:

我今天遇到了一个问题,我的 Elastic Beanstalk 应用程序突然将我发送到一个502 Bad Gateway页面。现在我过去遇到过这个问题,发生这种情况的原因是 Node 命令无法启动我的服务器。我通过输入解决Node command: node main.js了这个问题,直到今天早上随机我才遇到这个问题。突然间它停止工作,我在错误日志中收到此错误:

2015/03/31 13:07:17 [error] 697#0: *519 connect() failed (111: Connection refused) while connecting to upstream, client: 54.146.12.189, server: , request: "HEAD / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "54.152.12.19"
2015/03/31 13:07:17 [error] 697#0: *521 connect() failed (111: Connection refused) while connecting to upstream, client: 54.146.18.189, server: , request: "GET /clientaccesspolicy.xml HTTP/1.1", upstream: "http://127.0.0.1:8081/clientaccesspolicy.xml", host: "54.152.12.19"
2015/03/31 13:16:02 [error] 697#0: *523 connect() failed (111: Connection refused) while connecting to upstream, client: 69.204.65.1321, server: , request: "GET /blog/the-differences-in-segmenting-your-data-by-users-and-sessions HTTP/1.1", upstream: "http://127.0.0.1:8081/blog/the-differences-in-segmenting-your-data-by-users-and-sessions", host: "www.mywebsite.com"

How should I approach solving this issue?

我应该如何解决这个问题?

Here is my main.jsfile:

这是我的main.js文件:

//Load express
var express = require('express');
var app = express();
var router = express.Router(); // get an instance of the router
var bodyParser = require('body-parser'); // configure app to use bodyParser()
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var aws = require('aws-sdk');

app.use(bodyParser.urlencoded({ extended: true})); // get data from a POST method
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use(cookieParser());


var port = process.env.PORT || 8080; // set the port

var DB_CONFIG = process.env.DB_CONFIGURATION;
var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY;
var S3_BUCKET = process.env.S3_BUCKET;

var blogDB = require('./config/blogDB.js');
mongoose.connect(blogDB.url);




require('./config/passport.js')(passport);


app.set('view engine', 'ejs'); // set ejs as the view engine

app.use(express.static(__dirname + '/public')); // set the public directory

app.use(session({ secret: 'thisisatest' }));
app.use(passport.initialize());
app.use(passport.session());

app.use(flash());


var routes = require('./app/routes');

app.use(routes); // use routes.js


app.listen(port);
console.log('magic is happening on port' + port);

回答by Josh Davis

A 502 Bad Gatewayerror usually suggests that the proxy (Nginx in NodeJS's case) can't find a destination to route the traffic to.

一个502 Bad Gateway错误通常表明代理(Nginx的中的NodeJS的情况下),不能找到一个目的地路由流量。

Looking at your original error logs, it looks like nginx is trying to go to http://127.0.0.1:8081. But your main.jshas port 8080 as the fallback unless the ENV variable PORTis set.

查看您的原始错误日志,看起来 nginx 正试图转到http://127.0.0.1:8081. 但是main.js除非PORT设置了 ENV 变量,否则您将端口 8080 作为后备。

I don't know if you are setting that variable, but try switching your NodeJS app to listen on port 8081 and see if that helps.

我不知道您是否正在设置该变量,但请尝试将您的 NodeJS 应用程序切换为侦听端口 8081,看看是否有帮助。

Additionally, I've written this answer that explains the NodeJS setup for traffic which might help: elastic beanstalk weird nginx configuration

此外,我写了这个答案,解释了NodeJS 的流量设置,这可能会有所帮助:elastic beanstalk 奇怪的 nginx 配置

If you still have issues, you might have to give some more info on your setup.

如果仍有问题,则可能需要提供有关设置的更多信息。

回答by keithics

upstream: "http://[::1]:5555/uploads/logo/df0944721b740b98c10a652ce0dd8296-640.jpg",

If you are having errors with upstream set to ipv6 --> [::1], replace localhost to 127.0.0.1 in your nginx conf.

如果您在将上游设置为 ipv6 --> [::1] 时遇到错误,请在您的 nginx 配置中将 localhost 替换为 127.0.0.1。

server {
listen 80;

server_name mydomain.com;

location / {
    client_max_body_size 20M;
    client_body_buffer_size 128k;
    #proxy_pass http://localhost:5552;
    proxy_pass http://127.0.0.1:5552;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
}

回答by Husk Rekoms

I know that this is a super old post, but I just had the same issue with my node server being restarted by my hosting provider. When the server reset it also caused mongoDB to be shut down.

我知道这是一篇非常老的帖子,但我的节点服务器被我的托管服务提供商重新启动时遇到了同样的问题。当服务器重置时,它也会导致 mongoDB 关闭。

When using forever to try to restart the node servers:

使用永远尝试重新启动节点服务器时:

2018/04/12 06:49:32 [error] 23434#23434: *27 connect() failed (111: Connection refused) while connecting to upstream, client:

The log files do not indicate it's an error with mongo specifically but if you try to start the server manually:

日志文件并未明确指出这是 mongo 的错误,但如果您尝试手动启动服务器:

node /server/server.js

Connection fails: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

Once mongoDB is restarted the server can be restarted:

一旦 mongoDB 重新启动,服务器就可以重新启动:

sudo mongod &

And then simply restart your server and you're good to go.

然后只需重新启动您的服务器,您就可以开始了。

回答by Héctor BlisS

It's important to add the Node command at Configuration Modify software section of your beanstalk app, if your app you are using the command start, so use it as node command, "npm start" that will start your app correctly, this also happens when use the folder and file bin/www to start the Nodejs server.

在 beanstalk 应用程序的 Configuration Modify software 部分添加 Node 命令很重要,如果您的应用程序使用命令 start,那么将其用作 node 命令,“npm start”将正确启动您的应用程序,使用时也会发生这种情况文件夹和文件 bin/www 以启动 Nodejs 服务器。

回答by Ally Makongo

Under beanstalk, go to configuration, then to software, add npm start on node command. This will be your default settings. Nginx works fine with its default settings

在 beanstalk 下,转到配置,然后转到软件,在 node 命令上添加 npm start。这将是您的默认设置。Nginx 在默认设置下运行良好

回答by blamb

Answer

回答

These all point to no http server response, which means your http server is not answering requests.

这些都指向没有 http 服务器响应,这意味着您的 http 服务器没有响应请求。

The exact part that points this out in the logs is the following.

在日志中指出这一点的确切部分如下。

connect() failed (111: Connection refused) while connecting to upstream
server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:

Make sure you have taken steps to start that, and ensure its currently running at the time the error is generated.

确保您已采取措施启动它,并确保它在生成错误时当前正在运行。

It can also possibly point to configuration error on the the http server js file itself.

它也可能指向 http 服务器 js 文件本身的配置错误。

To get a better idea, view error logs in /var/log/nginx/error.log. If you see connection errors to your proxy backend, like above, then that is probably the case.

要获得更好的主意,请查看 /var/log/nginx/error.log 中的错误日志。如果您看到代理后端的连接错误,如上所示,则可能是这种情况。

To see if its having a problem connecting to a proxy that was defined. Make sure the node process manager is running and configured correctly on that proxied port. e.g. If your running a node app, look for pm2, or whatever node module you use to start your http server with.

查看它是否在连接到定义的代理时出现问题。确保节点进程管理器在该代理端口上运行并正确配置。例如,如果您正在运行节点应用程序,请查找 pm2 或用于启动 http 服务器的任何节点模块。

pm2 show

The OA assumed your backed was simply misconfigured, and wasnt clear that an entire http server was needed behind it. Some users are trying to launch apps that contain task managers that are trying to launch things that done exist, and dont realize entire back-end was not needed or running, like myself at the time being.

OA 假设您的支持只是配置错误,并且不清楚背后是否需要整个 http 服务器。一些用户试图启动包含任务管理器的应用程序,这些应用程序试图启动已经完成的事情,并且没有意识到不需要或运行整个后端,就像我当时一样。