【问题标题】:Cannot run Node server on port 80 [closed]无法在端口 80 上运行节点服务器 [关闭]
【发布时间】:2013-02-19 21:33:43
【问题描述】:

我最近购买了一台富士通服务器。我正在上面运行 Linux Mint ( Cinnamon )。

我安装了 Node.js 没有问题,并且可以在除 80 之外的任何端口上运行我的服务器脚本。起初,它以 EACCES 错误响应,但是当我以 root 身份运行 node.js 时,该错误消失了.现在它的输出与我在任何其他端口上运行它时的输出相同,但是当我进入域时它将无法工作。

var http = require('http');

http.createServer(server).listen(80);

function server(req, res) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end('Hello World\n');


    console.dir(req);
}

在 shell 中运行命令行为:

/home/xymon/node/node server.js

su 登录后。

我的代码几乎可以在我尝试过的任何其他端口上运行。甚至 81。只是不是 80,它把我逼疯了。

【问题讨论】:

  • 您的脚本的旧进程是否仍在运行?如果正在运行旧进程,则端口 80 仍在使用中。如果正在运行您的脚本的旧实例,请查看您的进程列表 (ps aux)。
  • 如果您不想使用 nginx 或 httpproxy,此答案可能会很有用。 stackoverflow.com/a/6848861/1349025
  • 没有其他节点正在运行...有没有办法查看进程列表以及它们正在使用的端口?我知道在我购买 fujitsu 之前在 Windows 上运行我的服务器时我必须更改 Skype 的端口...我对 linux 世界还很陌生。
  • @thtsigma 我已经尝试过这种方法,但它对我没有任何作用。
  • 可以使用sudo lsof -i tcp:80查看80端口是否有其他进程监听(听起来好像有)。

标签: linux node.js port


【解决方案1】:

一种选择是作为 sudo 运行,这不是一个很好的选择,因为你们所有的网站代码都会被提升。

另一种选择是在备用端口上运行站点并将其放在 nginx 或 httpproxy 后面。

var proxyPort = 80;
var http = require('http');
var httpProxy = require('http-proxy');

var options = {
    router: {
        'localhost': '127.0.0.1:3000',
        'site1.com': '127.0.0.1:3000',
        'site2.com': '127.0.0.1:4000'
    }
};
console.log('Proxy Routing:')
console.log(options);
console.log();

var proxyServer = httpProxy.createServer(options);
proxyServer.listen(proxyPort);
console.log('Proxy listening on port ' + proxyPort);

这还具有能够在端口 80 下运行多个站点的好处。如您所见,我还使该站点在端口 3000 上可用,但仅在本地可用。

【讨论】:

    【解决方案2】:

    天哪,多么冒险!

    我已经通过以下步骤解决了这个问题。

    1. 打开端口 80 - 100。
    2. 在端口 100 上运行服务器。
    3. 将端口 80 重定向到 100... iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 100
    4. 在 sudo 中运行 Node.js。

    感谢大家的帮助,这对我来说已经跑了两天,但我正在学习!

    成品:http://io-chat.com/home

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 2020-07-09
      • 2023-03-25
      • 2021-09-16
      • 2013-09-16
      • 1970-01-01
      • 2019-11-05
      • 2021-06-05
      • 2019-03-01
      相关资源
      最近更新 更多