Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will running node.js with Apache causes too much performance degradation?

I am trying to run Apache and node.js on the same Amazon EC2 instance. After research online, I came up with the following solution:

  1. run Apache on port 9000

  2. run node.js apps on port 8001, 8002 and so on.

  3. create a reverse proxy in node.js, running on port 80. It routes requests to different ports based on the hostname.

This solution works. (Although I haven't found a way to start node.js automatically) My question is, will running multiple node instance causes performance degradation? Or will the reverse proxy be a problem?

Thanks,

like image 417
Xi 张熹 Avatar asked Feb 22 '23 11:02

Xi 张熹


1 Answers

Performance Degradation

On the contrary. If all you do with node is proxying, the overload is insignificant (as compared to apache's). I do have a quite similar setup as yours (small virtual machine, 3 legacy apache websites, node.js proxying and enhancement). So far, apache is the resource eater, not my node apps, which nonetheless proxy/filter/intercept every incoming http request

Here's my setup :

main proxy

which handles all incoming requests (for as many domains as you like) : I personally use nodejitsu's http-proxy which is very robust and simple to configure

var http = require('http');
var httpProxy = require('http-proxy');
var options = {

  hostnameOnly: true,
  router: {    
    'domain1.com': '127.0.0.1:8081',
    'www.domain1.com': '127.0.0.1:8081',
    'subdomain1.domain1.com': '127.0.0.1:8082',
(...)
    'domain2.com': '127.0.0.1:8090',
(...)

   }
}

var mainProxy = httpProxy.createServer(options);
mainProxy.listen(8080);

You can redirect to apache directly from the option object, or do some more url parsing in another (middleware) node app on a different port.

WARN: if you don't wish to install/run node as 'root' (which I'd strongly advise in a production environement) : redirect port 80 to some other port with an IPTABLE directive (let's say 8080) where this proxy runs (see here for detailed example of Iptable directives). Mine, on a debian squeeze, reads :

#REDIRECT port 80 to 8080
$IPTABLES -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

node apps

which do some URL parsing with regexes, or whatever you need. Ex: redirect to a few (legacy) apache servers which (in my case) only serve legacy content not yet served by the 'in developement' node apps.

Daemonisation

There are several solutions to make node run as a daemon. My favorite two are :

  • nodemon will monitor all files in your node app folder and subfolder for change and restart the node app on file change. It's perfect for a development environment
  • forever (yet again by Nodejitsu) will restart your node app if ever it stops. It's very customisable.

Also :

  • init.d script : I've written this debian init.d script for my own servers (should work on ubuntu)
like image 195
Peter Host Avatar answered Feb 26 '23 22:02

Peter Host