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!
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With