Node.js in production with forever and connect vhosts / nginx alternative?

I use mainly node.js for my projects and I use nodemon

for development and forever

production.

I usually split my projects into vhosts, so my structure might be something like this:

bootstrap.js
apps/
  admin/
  front/
  api/

      

and my bootstrap.js looks like this:

// Get Express
var express = require('express');

// Create express server
var app = express.createServer();

// Configure Development Environment
app.configure('development', function() {
  //app.use(express.vhost('localhost', require('./apps/front')));
  app.use(express.vhost('localhost', require('./apps/admin')));
  app.use(express.vhost('localhost', require('./apps/api')));
  app.listen(3000);
});

// Configure Production Environment
app.configure('production', function() {
  app.use(express.vhost('example.com', require('./apps/front')));
  app.use(express.vhost('admin.example.com', require('./apps/admin')));
  app.use(express.vhost('api.example.com', require('./apps/api')));
  app.listen(80);
});

      

When I want to run my apps, I use forever start bootstrap.js

and bootstrap.js

run my specific vhost apps.

This way I have each vhost listening on its own port and vhost listening on 3000.

Now my problem is that forever monitor bootstrap.js and this leads to the fact that if any of the vhost is not working for any reason, it will permanently reload bootstra.js, which will also lead to unnecessary restart of other vhosts ...

My question is, what would be the best way in my case?

I though had one bootstrap.js file instead to have a structure like this:

admin.js
front.js
api.js
apps/
  admin/
  front/
  api/

      

and use forever start

root js for each of these files, in which case I won't be using vhost, but I believe vhosts is what I need anyway.

I, however, in case I remove whosts to use nginx and have the following configuration for each application:

server
{
    listen 80;
    server_name admin.example.com;

    access_log /var/log/nginx/admin.example.com.access.log;

    error_log /var/log/nginx/admin.example.com.error.log;

    root /home/panosru/domains/example.com/apps/admin/public;

    location ~* ^.+\.(svg|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
        root /home/panosru/domains/example.com/apps/admin/public;
    }

    location / {
        proxy_pass http://admin.example.com:3001;
    }
}

      

I can see that with nginx I have a few more advantages as I am using nginx to serve static content instead of node, which would be unnecessary overhead for node.

What do you think? Is it correct or could you please suggest something better for me?

Thank!

+3


source to share


1 answer


I think you've basically answered your own question with a sensible answer, but you're looking for some alternatives ...

I am not familiar with the vhost functionality in express. However, I think you are asking for problems by running 3 applications with 3 different scalability requirements in the same Node.js process.



You can still use the express vhosts solution using child_process.fork to start multiple instances of express (all of which can still use the same http server).

+1


source







All Articles