使用代理服务器托管可识别子域的多个Node.JS应用程序 [英] Hosting multiple Node.JS applications recognizing subdomains with a proxy server
问题描述
我正在尝试将某些子域重定向到我的ubuntu AWS EC2虚拟服务器上的特定端口.已经使用DNS尝试过,但根据以下主题,它将无法正常工作,默认路由使用node-http-proxy?和
I am trying to redirect certain subdomains to a specific port on my ubuntu AWS EC2 virtual server. Already tried it with DNS and that wouldn't work so based on the following topics, Default route using node-http-proxy? and How do I use node.js http-proxy for logging HTTP traffic in a computer?, I was trying to create a Node.JS proxy server with logging. That said I mixed it a bit up together (I'm new to Node.JS, still learning) and made the following script:
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);
发生了什么事,就是我不断收到以下错误,却不知道如何解决这个问题:
Well what happens is that I keep getting the following error and can't figure out how to put this to work:
$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,它将被从以下地址重定向到portB:相同的IP地址,并且两个端口都向公众开放.
In short I'm trying to receive http request on port 80 and if it came from sub1.domain.com it will be redirected to portA and if it came frome sub2.domain.com it'll be redirected to portB from the same IP adress and both ports are open to the public.
有人可以解释如何解决此问题并解释其发生原因吗?
Can someone explain how to fix this and explain why it happens?
推荐答案
端口访问:
如前一个答案和注释所述,普通用户无法打开1024以下的端口.可以通过遵循来克服.这些说明:
-
如果
cat /proc/sys/net/ipv4/ip_forward
在文件/etc/sysctl.conf
上返回取消注释net.ipv4.ip_forward
的0,并启用以下更改:sudo sysctl -p /etc/sysctl.conf
,如果返回1,则跳过此步骤;
If
cat /proc/sys/net/ipv4/ip_forward
returns 0 uncommentnet.ipv4.ip_forward
at the file/etc/sysctl.conf
and enable these changes:sudo sysctl -p /etc/sysctl.conf
, if it returns 1, skip this step;
设置从端口80转发到1024以上的端口(即端口8080):sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080;
Set up forwarding from port 80 to one desired above 1024 (i.e. port 8080): sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080;
Note: To make these changes stick even when restarting the server you may check the this out.
After taking care of the port access the proxy server continued without working, so after opening an issue it seemed that the routing feature was removed because, according to Nodejitsu Inc.:
该功能由于简单性而被删除.它属于一个单独的模块,而不属于
http-proxy
本身,因为http-proxy
仅负责代理位.
The feature was removed due to simplicity. It belongs in a separate module and not in
http-proxy
itself ashttp-proxy
is just responsible for the proxying bit.
因此,他们建议使用 http-master
.
So they recommended to use http-master
.
如http-master
的自述部分,则需要node.js,我们需要运行npm install -g http-master
(可能需要以root用户身份运行,具体取决于您的设置).然后我们创建配置文件,即http-master.conf,添加路由详细信息,对于这个特定问题,配置文件如下:
As described in http-master
's README section, node.js is required and we need to run npm install -g http-master
(may be needed to run as root depending on your setup). Then we create the config file, i.e. http-master.conf, were we add our routing details and for this specific question, the config file is as followed:
{
# 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
并且我们的子域路由应该可以正常工作.
And we are almost done, now we just run it with: http-master --config http-master.conf
and our subdomain routing should be working just fine.
注意::如果您想在后台运行代理服务器,建议您使用 pm2 ,如果使用pm2,我建议阅读此问题.
Note: If you want to run the proxy server on the background I recommend using a tool like forever or pm2, and in the case of using pm2 I recommend reading this issue.
这篇关于使用代理服务器托管可识别子域的多个Node.JS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!