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

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

What are my options for this?

The obvious solution is to provide all the domains served by the Node.js web application, which acts as a proxy server and moves to other Node.js applications running on unique ports.

+78
Oct 08 '13 at 17:40
source share
12 answers

Choose one of:

  • Use another server ( like nginx ) as a reverse proxy.
  • Use node-http-proxy as a reverse proxy.
  • Use vhost middleware if each domain can be served from the same Connect / Express code base and node.js.
+74
Oct 08 '13 at 17:53
source share
β€” -

Diet.js offers a very convenient and easy way to host multiple domains with a single server instance. You can simply call a new server() for each of your domains.

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') }) 

Sharing your applications

If you want to have different folders for your applications, you can create a folder structure similar to this:

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

In /server/index.js , a folder is required for each application:

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

In /server/yourApp/index.js you should configure your first domain , for example:

 // 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 configure your second domain , for example:

 // 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 ') }); 

More details

+45
Aug 17 '14 at 18:58
source share

Hm ... why do you think nodejs should act as a proxy. I suggest running several node applications listening on different ports. Then use nginx to forward the request to the right port. If you use one nodejs, you will also have one point of failure. If this application crashes, all sites will go down.

+18
Oct 08
source share

Use nginx as a reverse proxy.

http://www.nginxtips.com/how-to-setup-nginx-as-proxy-for-nodejs/

Nginx provides a number of benefits to your applications in the form of caching, static file processing, ssl and load balancing.

+12
Oct 08 '13 at
source share

I have an API that I use on the site, and below is my configuration. I also have SSL and GZIP, if anyone needs it, just comment me.

 var http = require('http'), httpProxy = require('http-proxy'); var proxy_web = new httpProxy.createProxyServer({ target: { host: 'localhost', port: 8080 } }); var proxy_api = new httpProxy.createProxyServer({ target: { host: 'localhost', port: 8081 } }); http.createServer(function(req, res) { if (req.headers.host === 'http://www.domain.com') { proxy_web.proxyRequest(req, res); proxy_web.on('error', function(err, req, res) { if (err) console.log(err); res.writeHead(500); res.end('Oops, something went very wrong...'); }); } else if (req.headers.host === 'http://api.domain.com') { proxy_api.proxyRequest(req, res); proxy_api.on('error', function(err, req, res) { if (err) console.log(err); res.writeHead(500); res.end('Oops, something went very wrong...'); }); } }).listen(80); 
+12
Jun 23 '14 at 16:50
source share

If you are using a connect / express server, you can see the vhost . This will allow you to use multiple domains (subdomains) for the server address.

You can follow the example below that looks exactly as you need.

+6
08 Oct '13 at 17:53
source share

The best way to do this is to use Express vhost middleware. Check out this tutorial for a step-by-step explanation:

http://shamadeh.com/blog/web/nodejs/express/2014/07/20/ExpressMultipleSites.html

+4
Jul 21 '14 at 17:47
source share

This is my simplest demo project without any middleware or proxy. This requires only a few codes, and that’s enough.

https://github.com/hitokun-s/node-express-multiapp-demo

With this structure, you can easily configure and maintain each application independently.
Hope this helps you.

+3
Apr 17 '14 at 2:52
source share

Install forever and bouncy first .

Then write a startup script. In this script, add a rule to the iptables firewall utility to tell it to forward traffic to port 80 to port 8000 (or anything else you choose). In my example, 8000 is where I run bouncy

 sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000 

Using eternity, give the script command to run bouncy on port 8000

 forever start --spinSleepTime 10000 /path/to/bouncy /path/to/bouncy/routes.json 8000 

The .json routes will be something like

 { "subdomain1.domain.com" : 5000, "subdomain2.domain.com" : 5001, "subdomain3.domain.com" : 5002 } 

NodeJS1, application2, and application3 applications run on ports 5000, 5001, and 5002, respectively.

script In my case, I can find it here , and you may have to change it a bit in your environment.

I also wrote about this in more detail, and you can find it here .

+3
Mar 01 '15 at 8:15
source share

Here's how to do it using vanilla Node.js:

 const http = require('http') const url = require('url') const port = 5555 const sites = { exampleSite1: 544, exampleSite2: 543 } const proxy = http.createServer( (req, res) => { const { pathname:path } = url.parse(req.url) const { method, headers } = req const hostname = headers.host.split(':')[0].replace('www.', '') if (!sites.hasOwnProperty(hostname)) throw new Error('invalid hostname ${hostname}') const proxiedRequest = http.request({ hostname, path, port: sites[hostname], method, headers }) proxiedRequest.on('response', remoteRes => { res.writeHead(remoteRes.statusCode, remoteRes.headers) remoteRes.pipe(res) }) proxiedRequest.on('error', () => { res.writeHead(500) res.end() }) req.pipe(proxiedRequest) }) proxy.listen(port, () => { console.log('reverse proxy listening on port ${port}') }) 

Pretty simple, huh?

+2
Aug 07
source share

This guide from the digital ocean is a great way. It uses the pm2 module, which demonizes your application (runs them as a service). There is no need for additional modules, such as Forever, because it will automatically restart your application if it works. It has many features that help you track various applications running on your server. This is pretty amazing!

0
Sep 13 '16 at
source share

after all, when you get the request and response object, you can get the domain through "request.headers.host" ... (not ip, actually the domain)

0
Aug 08 '19 at 17:25
source share



All Articles