我的目标是在端口 80 上运行 Node.js。这是因为我发现 node.js 被某些网络阻止,这些网络不允许来自任何其他端口的流量。
似乎最好的方法是通过 Node.js 代理 Apache。我试过使用 node-http-proxy这样做,但我没有任何运气。
我使用的代码在这里:
var util = require('util'),
http = require('http'),
httpProxy = require('http-proxy');
httpProxy.createServer(9000, 'localhost').listen(80);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
但我不断收到端口 80 的错误“正在使用的地址”。我一定是做错了什么。
如何使用 node-http-proxy 通过 node.js 代理 Apache?这将使我能够在端口 80 上运行 node.js 吗? node-http-proxy 是实现这一目标的最佳方式吗?
谢谢。
最佳答案
在高端口 8080 或其他端口上运行您的应用程序
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
如果你没有使用 ngnix 或 apache
关于apache - 如何在端口 80 上运行 Node.js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6109089/