Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I host multiple Node.js sites on the same IP/server with different domains?

Tags:

node.js

I have a linux server with a single IP bound to it. I want to host multiple Node.js sites on this server on this IP, each (obviously) with a unique domain or subdomain. I want them all on port 80.

What are my options to do this?

An obvious solution seems to be to have all domains serviced by a node.js web app that acts as a proxy and does a pass through to the other node.js apps running on unique ports.

like image 699
Guy Avatar asked Oct 08 '13 17:10

Guy


People also ask

Can I host multiple domains on one server?

Virtual hosting allows you to host many domains on one server. A server may have extensive resources like HDD space, CPU, RAM, and so on. You can use the same server resources for different sites. It lets you host multiple websites on a single web server instance.

How many concurrent connections can Nodejs handle?

To address these issues, Node. JS uses a single thread with an event-loop. In this way, Node can handle 1000s of concurrent connections without any of the traditional detriments associated with threads.

Can 2 domains have the same name server?

Yes, indeed it can. Contrary to what another answer says, there doesn't have to be one “main” site with other domain names redirecting to it. Two domains can point to the same site without redirection.

Can Nodejs run on shared hosting?

Node. js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser. Node. js is not supported on Shared and Cloud hosting packages.


2 Answers

Choose one of:

  • Use some other server (like nginx) as a reverse proxy.
  • Use node-http-proxy as a reverse proxy.
  • Use the vhost middleware if each domain can be served from the same Connect/Express codebase and node.js instance.
like image 183
josh3736 Avatar answered Oct 15 '22 15:10

josh3736


Diet.js has very nice and simple way to host multiple domains with the same server instance. You can simply call a new server() for each of your domains.

A Simple Example

// Require diet
var server = require('diet');

// Main domain
var app = server()
app.listen('http://example.com/')
app.get('/', function($){
    $.end('hello world ')
})

// Sub domain
var sub = server()
sub.listen('http://subdomain.example.com/')
sub.get('/', function($){
    $.end('hello world at sub domain!')
})

// Other domain
var other = server()
other.listen('http://other.com/')
other.get('/', function($){
    $.end('hello world at other domain')
})

Separating Your Apps

If you would like to have different folders for your apps then you could have a folder structure like this:

/server
   /yourApp
       /node_modules
       index.js
   /yourOtherApp
       /node_modules
       index.js
   /node_modules
   index.js

In /server/index.js you would require each app by it's folder:

require('./yourApp')
require('./yourOtherApp')

In /server/yourApp/index.js you would setup your first domain such as:

// Require diet
var server = require('diet')

// Create app
var app = server()
app.listen('http://example.com/')
app.get('/', function($){
    $.end('hello world ')
})

And in /server/yourOtherApp/index.js you would setup your second domain such as:

// Require diet
var server = require('diet')

// Create app
var app = server()
app.listen('http://other.com/')
app.get('/', function($){
    $.end('hello world at other.com ')
});

Read More:

  • Read more about Diet.js
  • Read more about Virtual Hosts in Diet.js Read more about Server in Diet.js
like image 57
Adam Halasz Avatar answered Oct 15 '22 17:10

Adam Halasz