我正在尝试将某些子域重定向到我的 ubuntu AWS EC2 虚拟服务器上的特定端口。已经用 DNS 尝试过它,但基于以下主题 Default route using node-http-proxy? 和 How do I use node.js http-proxy for logging HTTP traffic in a computer? ,这不起作用,我试图创建一个带有日志记录的 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,它将被重定向到 portB相同的 IP 地址,并且两个端口都向公众开放。
有人可以解释如何解决这个问题并解释为什么会这样吗?
最佳答案
端口访问:
如之前的回答和评论所述,普通用户无法打开 1024 以下的端口。这可以通过以下方式克服 these instruction :
如果
cat/proc/sys/net/ipv4/ip_forward
返回 0 取消注释net.ipv4.ip_forward
在文件/etc/sysctl .conf
并启用这些更改: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 -端口 8080;
- 打开 Linux 防火墙以允许端口 80 上的连接:
sudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPT
和sudo iptables -A OUTPUT - p tcp -m tcp --dport 80 -j 接受
注意:要使这些更改即使在重新启动服务器时仍然有效,您可以检查 this出。
http-proxy
's routefeature 被删除:
在处理好端口访问后,代理服务器继续不工作,所以在打开 issue 之后路由功能似乎已被删除,因为根据 Nodejitsu Inc. 的说法:
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
.
使用http-master
:
如 http-master
的 README section 中所述, 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
并且我们的子域路由应该工作正常。
注意:如果您想在后台运行代理服务器,我建议您使用像forever 这样的工具。或 pm2 ,在使用 pm2 的情况下,我建议阅读 this issue .
关于javascript - 托管多个 Node.JS 应用程序,使用代理服务器识别子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24099273/