承载多个使用代理服务器识别子域的Node.JS应用程序

我正在尝试将某些子域redirect到我的Ubuntu EC2虚拟服务器上的特定端口。 已经尝试过使用DNS,这不会基于以下主题, 默认路由使用节点HTTP代理? 以及如何使用node.js http-proxy在计算机中loggingHTTP通信? ,我试图创build一个Node.JS代理服务器与日志logging。 这就是说我把它混合起来(我是Node.JS的新手,仍然在学习),并做了以下脚本:

var httpProxy = require('http-proxy'); var PORT = 80; logger = function() { return function (request, response, next) { // This will run on each request. console.log(JSON.stringify(request.headers, true, 2)); next(); } } var options = { // this list is processed from top to bottom, so '.*' will go to // 'http://localhost:3000' if the Host header hasn't previously matched router : { 'dev.domain.com': 'http://localhost:8080', 'beta.domain.com': 'http://localhost:8080', 'status.domain.com': 'http://localhost:9000', 'health.domain.com': 'http://localhost:9000', 'log.domain.com': 'http://localhost:9615', '^.*\.domain\.com': 'http://localhost:8080', '.*': 'http://localhost:3000' } }; // Listen to port 80 httpProxy.createServer(logger(), options).listen(PORT); console.log("Proxy server started, listening to port" + PORT); 

那么会发生什么是我不断收到以下错误,不知道如何把这个工作:

 $node proxyServer.js Proxy server started, listening to port80 events.js:72 throw er; // Unhandled 'error' event ^ Error: listen EACCES at errnoException (net.js:904:11) at Server._listen2 (net.js:1023:19) at listen (net.js:1064:10) at Server.listen (net.js:1138:5) at ProxyServer.listen (/home/ubuntu/QuantBull-Project/node_modules/http-proxy/lib/http-proxy/index.js:130:16) at Object.<anonymous> (/home/ubuntu/QuantBull-Project/proxyServer.js:28:43) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) 

简而言之,我试图在端口80上接收http请求,如果它来自su​​b1.domain.com,它将被redirect到portA,如果它来自su​​b2.domain.com,它将被redirect到来自相同IP的portB地址和两个港口都向公众开放。

有人可以解释如何解决这个问题,并解释为什么会发生?

端口访问:

正如前面的回答和评论所述,1024以下的端口不能由普通用户打开。 这可以通过遵循这些指令来克服:

  1. 如果cat /proc/sys/net/ipv4/ip_forward在文件/etc/sysctl.conf处取消注释net.ipv4.ip_forward并启用以下更改: sudo sysctl -p /etc/sysctl.conf ,如果它返回1,跳过此步骤;

  2. 设置从端口80转发到1024以上的端口(即端口8080): sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080;

  3. 打开Linux防火墙以允许端口80上的连接: sudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPTsudo iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT

注意:即使在重新启动服务器时也要保留这些更改,您可以检查一下。

http-proxy的路由特性被删除:

在考虑端口访问后,代理服务器继续工作,所以在打开一个问题之后 ,似乎路由function被删除,因为根据Nodejitsu公司:

该function由于简单而被删除。 它属于一个单独的模块,而不是http-proxy本身,因为http-proxy只负责代理位。

所以他们推荐使用http-master

使用http-master

http-master的README部分所述 ,node.js是必需的,我们需要运行npm install -g http-master (可能需要根据您的设置以root身份运行)。 然后我们创buildconfiguration文件,即http-master.conf,我们添加了我们的路由细节,对于这个特定的问题,configuration文件如下:

 { # To detect changes made to the config file: watchConfig: true, # Enable logging to stdout: logging: true, # Here is where the magic happens, definition of our proxies: ports: { # because we defined that Port 80 would be redirected to port 8080 before, # we listen here to that port, could be added more, ie for the case of a # secure connections trough port 443: 8080 : { proxy: { # Proxy all traffic for monitor subdomains to port 9000 'status.domain.com' : 9000, 'health.domain.com' : 9000, # Proxy all traffic for logger subdomains to port 9615 'log.domain.com' : 9615, # Proxy all traffic from remaining subdomains to port 8000 '*.domain.com' : 8000 }, redirect: { # redirect .net and .org requests to .com 'domain.net': 'http://domain.com/[path]', 'domain.org': 'http://domain.com/[path]' } } } } 

我们差不多完成了,现在我们只需要运行它: http-master --config http-master.conf ,我们的子域路由应该工作得很好。

注意:如果你想在后台运行代理服务器,我推荐使用像永远或pm2这样的工具,而在使用pm2的情况下,我推荐阅读这个问题 。

如果你以普通用户身份运行你的代理(不是root),你不能打开1024以下的端口。有一种方法可以像普通用户那样做,但通常我只是运行root这样的东西。