Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Node.js Multi-tenant for websites on port 80?

Tags:

node.js

My end goal is to make node.js more cost effective per each server instance.

I'm not running a game or chat room but rather simple websites for customers. I would like to house multiple clients on a single server yet have multiple websites running off of port 80 using host header mapping. I would like to still use express as I'm doing but have it be more like a routing thing from port 80 to the other node apps if that is even possible. Node can be cheaper if its done in this way but currently its more expensive for my purposes as each customer would need their own box if running on port 80. Also, my motivation is to focus on node development but there must be a reason to do so in terms of cost.

I do this quite a lot for ASP.NET in Windows as IIS supports this out of the box and I know this is something normal for Apache as well.

Feel free to move this to another forum in stack exchange if this is not the right question or give constructive criticism rather than a random downvote. Thanks.

update

The approach I took was to use static hosting (via gatspy and s3) then an API instead that registered domains through post message from the client and API keys from the server and generates static sites periodically as sites change but thanks for all the suggestions!

like image 760
King Friday Avatar asked Apr 18 '12 18:04

King Friday


People also ask

Can NodeJS run on port 80?

If you want to run NodeJS on different port, change 80 to your required port number (e.g 8080). Also the function defined in createServer() will be executed whenever someone sends a request to your NodeJS server. In this case, it simply returns “Hello World!” string.

How do I run multiple node apps?

Yes you can run multiple node instances, all you have to do is to change the port number for which the server is listening. If you are using express framework then you can simply do it like this in app. js file. If you are not using express framework then you have to change in the main file which is generally server.


1 Answers

In theory, you could build a pure-Node web server that emulated the functionality of Apache/Lighttpd/Nginx, but I wouldn't recommend it. In fact, for serious production services, I'd recommend ALWAYS fronting your service with Nginx or an equivalent (see this and this).

Here's how a simple Nginx config would work for two subservices exposed on port 80.

worker_processes  4;

events {
  worker_connections 1024;
}

http {
  include       mime.types;
  default_type  text/html;

  server {
    listen 80;
    server_name service1.mydomain.com
    location / {
      proxy_pass         http://127.0.0.1:3000/;
    }
  }
  server {
    listen 80;
    server_name service2.mydomain.com
    location / {
      proxy_pass         http://127.0.0.1:3001/;
    }
  }
}

I've seen production boxes kernel panic because Node doesn't throttle load by default and was prioritizing accepting new connections over handling existing requests - granted, it "shouldn't" have crashed the kernel, but it did. Also, by running on port 3000, you can run your Node service as non-root with very few permissions (and still proxy it so that it appears to be on port 80). You can also spread load between multiple workers, serve statics, log requests, rewrite urls, etc, etc, etc. Nginx is very fast (much lighter than Apache). The overhead of same-box proxy forwarding is minimal and buys you so much functionality and robustness that it's a slam dunk in my book. Even minor stuff, like - when I crash or overload my node service, do user get a black hole, or a "pardon our dust, our servers are being maintained" splash.

like image 103
Dave Dopson Avatar answered Oct 14 '22 00:10

Dave Dopson