[英]Hosting multiple Node.JS applications recognizing subdomains with a proxy server
我正在尝试将某些子域重定向到ubuntu AWS EC2虚拟服务器上的特定端口。 已经使用DNS进行了尝试,但根据以下主题, 使用node-http-proxy的默认路由 ,将无法正常工作? 以及如何使用node.js http-proxy记录计算机中的HTTP流量? ,我尝试使用日志记录创建Node.JS代理服务器。 也就是说,我将其混合在一起(我是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请求,如果它来自sub1.domain.com,它将被重定向到portA,如果它来自sub2.domain.com,它将被从同一IP重定向到portB。地址和两个港口都向公众开放。
有人可以解释如何解决此问题,并解释其原因吗?
如前一个答案和评论所述,普通用户无法打开1024以下的端口。 可以按照以下说明克服:
如果cat /proc/sys/net/ipv4/ip_forward
返回0,则取消对文件/etc/sysctl.conf
net.ipv4.ip_forward
的注释并启用以下更改: sudo sysctl -p /etc/sysctl.conf
,如果返回1,跳过这步;
设置从端口80转发到高于1024的端口(即端口8080): sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080;
sudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPT
和sudo iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT
注意:要重新启动服务器,你可以检查时,这些更改坚持甚至这出。
http-proxy
的 routefeature已删除: 在处理完端口访问之后,代理服务器继续不工作,因此在出现问题之后 ,似乎已删除了路由功能,因为据Nodejitsu Inc.称:
由于简单性,该功能已被删除。 它属于一个单独的模块,而不属于
http-proxy
本身,因为http-proxy
仅负责代理位。
因此,他们建议使用http-master
。
http-master
: 如http-master
的README部分中所述 ,node.js是必需的,并且我们需要运行npm install -g http-master
(可能需要以root用户身份运行,具体取决于您的设置)。 然后,在添加路由详细信息的同时,创建配置文件,即http-master.conf,对于这个特定问题,配置文件如下:
{
# 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, i.e. 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
,我们的子域路由应该可以正常工作。
如果您以普通用户(不是root用户)身份运行代理,则无法打开1024以下的端口。也许可以以普通用户的身份打开端口,但是通常我只是以root用户身份运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.