无法在端口80上运行节点服务器

时间:2013-03-05 13:05:32

标签: linux node.js port

我最近购买了一台富士通服务器。我正在运行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
登录后

我的代码几乎适用于我尝试过的任何其他端口。即使是81.只是不是80,它正在把我推向一堵墙。

2 个答案:

答案 0 :(得分:2)

一个选项是以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上可用,但仅在本地。

答案 1 :(得分:1)

善良,多么冒险!

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

  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.
  5. 谢谢大家的帮助,这对我来说已经有两天了,但我正在学习!

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

相关问题