无法从本地主机访问 AWS EC2 服务器(curl 超时)

Cannot access AWS EC2 server from localhost (curl times out)

我正在尝试使用 ngnix 代理 node.js 服务器。我的问题是我无法访问(curl 或 nginx)本地主机上的 node.js 服务器,

对外开放它的3000端口,可以看到我的nodejs服务器运行。但是 curl 和 nginx 无法访问它。

一件奇怪的事情是我们有一个 mongodb 启动并且可以在本地主机上毫无问题地访问它

我的实例 ID 如下:i-0fef394cbc350a4f4

这是 nginx 服务器的 link,找不到本地主机,因此超时:http://ec2-54-246-136-64.eu-west-1.compute.amazonaws.com/

我找不到其他方法来证明我们无法访问 curl localhost:3000

var sticky = require("socketio-sticky-session");
sticky(function () {
  // This code will be executed only in slave workers

  var http = require('http');
  var socketIO = require('socket.io');

  var server = http.createServer(app);

  var io = socketIO(server);
  require('./app_api/config/socketio')(io);

  io.on('connection', (socket) => {
    log.trace("new user connected");
    socket.on('disconnect', () => {
      log.trace("user disconnected");
    });
  });
  connectToDispatcher();

  process.title = "share_place";

  return server;
}).listen(3000, function () {
      console.log((cluster.worker ? 'WORKER ' + cluster.worker.id : 'MASTER') + '| PORT ' + 3000)

      app.domain.on('error', (er) => {
        log.error('error', er.stack);

        try {
          // make sure we close down within 30 seconds
          var killtimer = setTimeout(() => {
            process.exit(1);
          }, 30000);
          // But don't keep the process open just for that!
          killtimer.unref();

          // stop taking new requests.
          server.close();

          // Let the master know we're dead.  This will trigger a
          // 'disconnect' in the cluster master, and then it will fork
          // a new worker.
          cluster.worker.disconnect();

          // try to send an error to the request that triggered the problem
          res.statusCode = 500;
          res.setHeader('content-type', 'text/plain');
          res.end('Oops, there was a problem!\n');
        } catch (er2) {
          // oh well, not much we can do at this point.
          log.error('Error sending 500!', er2.stack);
        }
      });

    }
);

一个更奇怪的情况:我们在 beantalk 实例上安装了节点代码。

我们从外部访问节点服务器:http://share-place.eu-west-1.elasticbeanstalk.com:8081/

但是来自亚马逊的默认 nginx 回复连接被拒绝的消息(对于我们可以从外部访问的 public 资源)

/var/log/nginx/error.log

2017/04/21 14:32:19 [警告] 3360#0:/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf:42 中的重复 MIME 类型 "text/html" 2017/04/21 14:37:48 [警告] 7358#0:/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf:42 中的重复 MIME 类型 "text/html" 2017/04/21 14:38:41 [错误] 7362#0: *1 connect() 失败(111: 连接被拒绝)连接到上游时,客户端:41.226.2.5,服务器:,请求:"GET / HTTP/1.1" ,上游:“http://127.0.0.1:8081/”,主机:"share-place.eu-west-1.elasticbeanstalk.com" 2017/04/21 14:38:41 [错误] 7362#0: *1 connect() 失败(111: 连接被拒绝)连接到上游时,客户端:41.226.2.5,服务器:,请求:"GET /favicon.ico HTTP/1.1" ,上游:“http://127.0.0.1:8081/favicon.ico", host: "share-place.eu-west-1.elasticbeanstalk.com", referrer: "http://share-place.eu-west-1.elasticbeanstalk.com/” 2017/04/21 14:38:45 [错误] 7362#0: *1 connect() 失败(111: 连接被拒绝)连接到上游时,客户端:41.226.2.5,服务器:,请求:"GET / HTTP/1.1" ,上游:“http://127.0.0.1:8081/”,主机:"share-place.eu-west-1.elasticbeanstalk.com"

这里可能会有很多问题,而且信息太少,也没有提供代码,不可能给你任何明确的答案。

您的应用程序可以侦听端口 3000,但它可能绑定到特定接口,因此它可以通过外部 IP 访问,而不是通过使用环回接口的本地主机访问。

您的应用程序可能 运行 在将其端口转发到外部接口但不转发到其他容器的本地主机的容器中。

您在问题中包含的 URL 实际上可能是处理连接的反向代理,您在开放端口上看到的内容可能无法反映主机的实际状态您的服务 运行 开启。

您可以尝试使用其他外部 IP 而不是本地主机,看看是否可行。您可以尝试绑定到 Node 应用程序中特定接口的端口(我不知道您目前是怎么做的,因为您没有在问题中包含任何代码示例)。然后你可以尝试关闭外部可用的端口并限制访问,看看它什么时候停止工作,但一次做一件事来缩小问题范围。

可能是你使用了net模块,所以在某些情况下你不能发出http请求。

检查 socketio-sticky-session 是否使用模块 net.createServer 或 http.createServer.