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

前端 未结 2 921
旧时难觅i
旧时难觅i 2020-12-22 23:51

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

相关标签:
2条回答
  • 2020-12-23 00:51

    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.

    0 讨论(0)
  • 2020-12-23 00:58

    What about using a proper reverse proxy, like HAProxy, have the proxy listen on port 80, and delegate to multiple node instances on non public ports (e.g. 10000, 10001 etc.), based on headers.host?

    0 讨论(0)
提交回复
热议问题