【问题标题】:NodeJS connect() failed (111: Connection refused) while connecting to upstreamNodeJS connect() 在连接到上游时失败(111:连接被拒绝)
【发布时间】:2015-06-04 21:27:24
【问题描述】:

我今天遇到一个问题,我的 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"

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

这是我的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);

【问题讨论】:

    标签: node.js amazon-web-services amazon-elastic-beanstalk


    【解决方案1】:

    502 Bad Gateway 错误通常表明代理(在 NodeJS 中为 Nginx)找不到将流量路由到的目的地。

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

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

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

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

    【讨论】:

    • 感谢您的解决方案。就我而言,nginx 正在尝试从日志中找到的端口 8080。然后我更改了环境变量中的端口,它工作得很棒。 ---------------------------------------- /var/log/nginx/error.log -------------------------------------- 2021/11/17 03:00:51 [错误] 4105#4105: *10072 connect() 失败(111:连接被拒绝)同时连接到上游,客户端:172.31.40.153,服务器:,请求:“GET / HTTP/1.1”,上游:“127.0.0.1:8080” ,主机:“172.31.36.155”
    【解决方案2】:
    upstream: "http://[::1]:5555/uploads/logo/df0944721b740b98c10a652ce0dd8296-640.jpg",
    

    如果您在将上游设置为 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;
    }
    }
    

    【讨论】:

      【解决方案3】:

      在您的 beanstalk 应用程序的配置修改软件部分添加 Node 命令很重要,如果您的应用程序正在使用命令启动,因此将其用作节点命令,“npm start”将正确启动您的应用程序,这也使用文件夹和文件 bin/www 启动 Nodejs 服务器时发生。

      【讨论】:

        【解决方案4】:

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

        当使用forever尝试重启节点服务器时:

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

        日志文件并不表明这是 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]
        

        mongoDB 重启后,服务器就可以重启了:

        sudo mongod &
        

        然后只需重新启动服务器即可。

        【讨论】:

          【解决方案5】:

          回答

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

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

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

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

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

          为了更好地了解,请查看 /var/log/nginx/error.log 中的错误日志。如果您看到与代理后端的连接错误,如上所示,那么可能就是这种情况。

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

          pm2 show
          

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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2022-01-18
              • 2015-04-03
              • 2017-07-08
              • 2016-11-20
              • 2014-02-26
              • 2012-11-21
              • 2017-07-17
              相关资源
              最近更新 更多