简体   繁体   English

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

[英]Hosting multiple Node.JS applications recognizing subdomains with a proxy server

I am trying to redirect certain subdomains to a specific port on my ubuntu AWS EC2 virtual server. 我正在尝试将某些子域重定向到ubuntu AWS EC2虚拟服务器上的特定端口。 Already tried it with DNS and that wouldn't work so based on the following topics, Default route using node-http-proxy? 已经使用DNS进行了尝试,但根据以下主题, 使用node-http-proxy的默认路由 ,将无法正常工作 and How do I use node.js http-proxy for logging HTTP traffic in a computer? 以及如何使用node.js http-proxy记录计算机中的HTTP流量? , I was trying to create a Node.JS proxy server with logging. ,我尝试使用日志记录创建Node.JS代理服务器。 That said I mixed it a bit up together (I'm new to Node.JS, still learning) and made the following script: 也就是说,我将其混合在一起(我是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);

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)

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. 简而言之,我试图在端口80上接收http请求,如果它来自su​​b1.domain.com,它将被重定向到portA,如果它来自su​​b2.domain.com,它将被从同一IP重定向到portB。地址和两个港口都向公众开放。

Can someone explain how to fix this and explain why it happens? 有人可以解释如何解决此问题,并解释其原因吗?

Port Access: 端口访问:

As mentioned by the previous answer and comments the port below 1024 can't be opened by a regular user. 如前一个答案和评论所述,普通用户无法打开1024以下的端口。 This can be overcome by following these instruction : 可以按照以下说明克服:

  1. If cat /proc/sys/net/ipv4/ip_forward returns 0 uncomment net.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; 如果cat /proc/sys/net/ipv4/ip_forward返回0,则取消对文件/etc/sysctl.conf net.ipv4.ip_forward的注释并启用以下更改: sudo sysctl -p /etc/sysctl.conf ,如果返回1,跳过这步;

  2. Set up forwarding from port 80 to one desired above 1024 (ie port 8080): sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080; 设置从端口80转发到高于1024的端口(即端口8080): sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080;

  3. Open up the Linux firewall to allow connections on port 80: sudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPT and sudo iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT 打开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

Note: To make these changes stick even when restarting the server you may check the this out. 注意:要重新启动服务器,你可以检查时,这些更改坚持甚至出。

http-proxy 's routefeature is removed: http-proxy routefeature已删除:

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.: 在处理完端口访问之后,代理服务器继续不工作,因此在出现问题之后 ,似乎已删除了路由功能,因为据Nodejitsu Inc.称:

The feature was removed due to simplicity. 由于简单性,该功能已被删除。 It belongs in a separate module and not in http-proxy itself as http-proxy is just responsible for the proxying bit. 它属于一个单独的模块,而不属于http-proxy本身,因为http-proxy仅负责代理位。

So they recommended to use http-master . 因此,他们建议使用http-master

Using http-master : 使用http-master

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). http-masterREADME部分中所述 ,node.js是必需的,并且我们需要运行npm install -g http-master (可能需要以root用户身份运行,具体取决于您的设置)。 Then we create the config file, ie http-master.conf, were we add our routing details and for this specific question, the config file is as followed: 然后,在添加路由详细信息的同时,创建配置文件,即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]'
      }
    }
  }
}

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. 差不多完成了,现在我们可以使用以下命令运行它: http-master --config http-master.conf ,我们的子域路由应该可以正常工作。

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 . 注意:如果你想运行我建议使用的工具,像背景,代理服务器永远PM2 ,以及在使用PM2我建议你阅读的情况下这个问题

如果您以普通用户(不是root用户)身份运行代理,则无法打开1024以下的端口。也许可以以普通用户的身份打开端口,但是通常我只是以root用户身份运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM