Node.js:在端口 80 上 ECONNREFUSED

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

Node.js: ECONNREFUSED on Port 80

node.jsconnectport80

提问by limlim

I've written a web-server using Node.js. When I tried to test my server with the tester I've written for it, I succeed only if the port I'm using for the server is not 80. I've checked on netstat, and no other application is using port 80. The error I'm getting on the console is:

我已经使用 Node.js 编写了一个网络服务器。当我尝试用我为它编写的测试仪测试我的服务器时,只有当我用于服务器的端口不是 80 时,我才能成功。我已经检查了 netstat,没有其​​他应用程序在使用端口 80。我在控制台上遇到的错误是:

Error: connect ECONNREFUSED
  at errnoException (net.js:640:11)
  at Object.afterConnect [as oncomplete] (net.js:631:18)

What can be done in this case?

在这种情况下可以做什么?

采纳答案by maerics

You might not have permission to bind to ports below 1024 (including 80) as a normal user (e.g. non-root), try a higher numbered port (above 1024, e.g. 8080).

您可能没有权限以普通用户身份(例如非 root)绑定到 1024(包括 80)以下的端口,请尝试使用更高编号的端口(1024 以上,例如 8080)。

[Edit]

[编辑]

Depending on your target system, and assuming you are its administrator, then you can probably work around the problem like so:

根据您的目标系统,并假设您是其管理员,那么您可能可以像这样解决问题:

  1. Execute your program using "sudo", such as: sudo node ./myprogram.js.

  2. Or, login as the "root" user on your system and run the program as normal.

  1. 使用“ sudo”执行您的程序,例如:sudo node ./myprogram.js

  2. 或者,以系统上的“ root”用户身份登录并正常运行程序。

However, don't get in the habit of doing either of these on a regular basis (until you understand why), they can potentially introduce security holes which may be exploitable by malicious, motivated individuals.

但是,不要养成定期执行其中任何一项操作的习惯(直到您明白原因),它们可能会引入安全漏洞,这些漏洞可能会被恶意的、有动机的个人利用。

回答by talentedmrjones

You should not make a habit of running node as a privileged user. Here is a method I'm using on 6 different machines:

您不应该养成以特权用户身份运行 node 的习惯。这是我在 6 台不同机器上使用的方法:

Use iptablesto forward incoming traffic on port 80 to 8080 like so:

用于iptables将端口 80 上的传入流量转发到 8080,如下所示:

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

Then be sure to save that

然后一定要保存

sudo iptables-save

Youd also want to add that to rc.local so those are set at reboot or instance launch (in the case of EC2 or other cloud)

你还想把它添加到 rc.local 以便在重启或实例启动时设置(在 EC2 或其他云的情况下)

Then you can safely start node listening on 8080 as any user.

然后,您可以安全地以任何用户身份启动节点侦听 8080。

回答by Bastian

This is basically an addition to the answer from maerics, but since I want to add a code example I write another answer with additional information how to prevent security problems.

这基本上是对 maerics 答案的补充,但由于我想添加一个代码示例,因此我编写了另一个答案,其中包含如何防止安全问题的附加信息。

Usually web servers are started as root, because this is needed to bind on ports below 1024, but then they change the user they run under to an not privileged user.

通常 Web 服务器以 root 身份启动,因为这需要绑定在 1024 以下的端口上,但随后它们会将运行的用户更改为非特权用户。

You can do this with node.js using process.setuid("username").

您可以使用 node.js 执行此操作process.setuid("username")

The following example starts the server and then drop the root permissions to the user "www-data" after it bound itself to port 80 when started as root:

以下示例启动服务器,然后在以 root 身份启动时将自身绑定到端口 80 后,将 root 权限删除给用户“www-data”:

function StartServer() {
  console.log("Starting server...");
  // Initalizations such as reading the config file, etc.
  server = http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  });
  try {
    server.listen(80, "0.0.0.0", function(){
      process.setuid("www-data");
    });
  }
  catch(err) {
    console.error("Error: [%s] Call: [%s]", err.message, err.syscall);
    process.exit(1);
  }
}

回答by Pivert

An alternate option that I haven't seen in the above list is also to permit your process to bind to a privileged port (<1024) by executing the following command on the node executable. (Adapt the path)

我在上面的列表中没有看到的另一个选项是通过在节点可执行文件上执行以下命令来允许您的进程绑定到特权端口 (<1024)。(调整路径)

setcap 'cap_net_bind_service=+ep' /opt/meteor/.meteor/tools/latest/bin/node

The annoyance is that you must reissue this command every time you update or change the node executable.

令人烦恼的是,每次更新或更改节点可执行文件时都必须重新发出此命令。

Regards,

问候,

回答by Roman Podlinov

I use Nginx server (port 80) in front of Node.js (any port >1024). It works great.

我在 Node.js 前面使用 Nginx 服务器(端口 80)(任何端口 >1024)。它工作得很好。