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

折月煮酒 提交于 2019-12-11 12:45:36

问题


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)

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?


回答1:


Port Access:

As mentioned by the previous answer and comments the port below 1024 can't be opened by a regular user. 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;

  2. 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;

  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

Note: To make these changes stick even when restarting the server you may check the this out.

http-proxy's routefeature is removed:

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.:

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.

So they recommended to use http-master.

Using 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). 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]'
      }
    }
  }
}

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.

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.




回答2:


If you are running your proxy as a regular user (not root), you can't open ports below 1024. There may be a way to do this as a normal user but usually I just run such things as root.



来源:https://stackoverflow.com/questions/24099273/hosting-multiple-node-js-applications-recognizing-subdomains-with-a-proxy-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!