Skip to content Skip to sidebar Skip to footer

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. Already tried it with DNS and that wouldn't work so based on the following topics

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

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

Post a Comment for "Hosting Multiple Node.js Applications Recognizing Subdomains With A Proxy Server"