Is it good idea to put NodeJs behind nginx - node.js

Is it good Idea to put nodeJs behind nginx , also can someone let me know nginx supports http 1.1;
Also how to make sure websockets works with this setup ( nodeJs Behind nginx)

If you want WebSockets, don't put it behind nginx. There might be some way that I don't know of, but DotCloud doesn't support WebSockets with Node.js because of its reliance on nginx, and they know nginx pretty well.
I assume you want to run your server on Port 80. If node is your main server, that will mean either:
Running node as root. This is often not ideal because there is potential for bugs in app code and with root access it could cause more damage. If a VM is set aside for a very particular purpose, all backups are made to outside of the VM, and rebuilding is quick, this may not be a big problem, though.
Using iptables to forward network traffic to port 80 to a higher-numbered port. I set this up and I felt like it was a good solution.
Edit: You can also run node.js as root and downgrade to a non-root user with setuid after binding to Port 80. The Jetty project (a web server for Java) suggests this technique.

nginx doesn't fully support HTTP 1.1. However, work is being done and will possibly be integrated into the development branch soon so keep your digits crossed and have a look at this mailing list thread to see what I'm talking about (there are patches but I haven't tried them as yet). More discussion here.
Depending on your needs you can do what Ben suggests using IPTables although I would also 'stealth' the high port using the mark module; I've upped a simple shell script that will do it for you.
If you need other applications on 80 you'll need to proxy; haproxy is one option but you can keep it all node using the excellent node-http-proxy.

Related

Use nodejs to host multiple html websites instead of apache

I currently use Apache2 to host multiple websites for some friends, and as i've been experimenting with NodeJS I was wondering if it was possible to host these sites with NodeJs?
I wanted to have a folder structure like the following:
App
--> server
--> websites
`--> site1 (example1.com)
`--> site2 (example2.com)
With more people asking me to host there sites I need to be able to easily create a new site quickly with out restarting the server effecting other hosted sites.
Currently I use a bash script to create the folder structure for apache and add a new virtualhost in the apache conf file and finally reload apache.
So my main question is, should I even be looking at nodejs for this or stick with apache?
Any opinions, examples or tutorials would be great.
You're probably better off staying with apache (or possibly switching to nginx). In fact, best practice for production node.js servers is typically to run them behind apache/nginx through a reverse proxy. Few reasons for that:
You have to run node.js as root to give it access to port 80/443 (generally bad idea)
You're going to be very hard-pressed (and probably a lot of trial and error) to get the security, performance and stability of apache/nginx.

Node.js for a website

Can I use node.js to build a web server and domain from scratch, instead of using apache for a website? Can All the backend hosting and domain work be done by node.js?
Thank in advanced, I'm trying to build a site for my resumé, would like to build it from scratch, to showcase my resumé. Back to front end. ...etc
So, sort of yes, but also sort of no. To build a back end with node you need to use a framework like express, and to serve it you'd need something like this. But to safely expose it to the open internet, you should definitely use apache or nginx to proxy requests to the port that your app is listening on. This is because it's extremely unsafe to expose your node server to the internet, and because both ports 80 and 443 require root privilege, making it even more unsafe. But if you really want to serve directly to your js backend, you can do so with something like this Best practices when running Node.js with port 80 (Ubuntu / Linode) for linux or this https://unix.stackexchange.com/questions/319734/pf-forwarding-all-packets-on-port-80-from-any-interface-to-socks-proxy for mac. This also requires root privilege to implement, and is still extremely unsafe.

FeathersJS on server with multiple hosts

I have a VPS that runs apache on with many domains (all same IP), but I want to start running nodeJS/feathersJS for some of my sites.
I can't seem to figure out how to run multiple instances other than with different ports. However, all the feathers sites are available to all the others provided you add the port (even sites running an apache site).
Is there an easy way to limit domain-1.com to show feathersjs site, and domain-2.com to still use apache?
IS there something I am missing?
I am new to node and transitioning from a PHP person to a nodeJS person... so please forgive my ignorance.
I found some non-feathersJS modules that I could figure out to use, but there has to be an easier way then modifying feathersJS... no?
*Edit I found the apache proxy solution already and implemented it. However, now I need to make sure that the port that runs node isn't used on my other domains.
Example.com now using apache proxy stuff run localhost:3030 but so does anotherexample.com:3030
Is there a way to limit this?
I found some libraries that do this for node, but none that seem to be nicely implemented in feathersJS.
*edit again I think the mentioned vHost feathers thing is what I am looking for, will update when i test this.
There is different ways to go about it but one way would be to use mod_proxy for Apache. In your domain configuration you'd then point to the port where the application you want is running on:
ProxyPass / http://www.example.com:8001/
ProxyPassReverse / http://www.example.com:8001/
While putting an Apache or NginX proxy in front of a Node application (and to serve static content) is usually a good idea for higher traffic sites, for smaller projects you can also just use Node without having to worry about Apache. To host different apps on different domains, you can use the vhost Express middleware. An example how to set it up with Feathers can be found here.

Where to run node.js

So I thought about giving node.js a try seeing the possibilities it has for a little test chat project (with mysql) I'm doing.
But what I couldn't find out is where to run the file from and whats most common.
What I currently have:
A FreeBSD server with latest Node and PHP 5.3.x
A vhost
some tutorials on how to start with node (which I looked through and got exited about)
knowledge on how to run it from terminal without having to keep my terminal open (screen)
So far so good.
What I need:
Some basic information of where to put the (lets say:) chat.js file.
Most logical port to run it on
So the web root (www) runs on a user (not root obviously). And the webroot has an underlying folder where I could put the script (away from visitors grabby little hands). This seems to me to be the most safe place to put it seeing nobody can get to it, which is probably what I want seeing I'm going to connect to a db and don't want my DB login data out there (I don't know how this works yet but I'll figure out db connect with node later, no answer required).
But if a file is not in the webroot, it seems to me a connection cannot be made from outside. Cause my webroot is configured to only allow 80 (or ssl on 443) incomming traffic, which is logical. Outgoing obviously has no problems.
All the examples that I found don't really help me. They just do everything on a local machine, which sucks for me cause I don't want to do that.
So what I would like to is the best practice for:
Where to put the file
port to run it on.
What is Node.js?
A lot of the confusion for newcomers to Node is misunderstanding exactly what it is. The description on nodejs.org definitely doesn't help.
An important thing to realize is that Node is not a webserver. By itself it doesn't do anything. It doesn't work like Apache. There is no config file where you point it to you HTML files. If you want it to be a HTTP server, you have to write an HTTP server (with the help of its built-in libraries). Node.js is just another way to execute code on your computer. It is simply a JavaScript runtime.
A nice tutorial How to Deploy Node JS Applications, With Examples
You'll need to have your non-node application on port 9000 (for
Apache, this will be in /etc/apache2/ports.conf and in your
sites-available file for your site), and you'll need your node
application to listen on 8080. You'll also need to set up DNS 'A'
records for the different hostnames you'll be using for your servers.
Companies like Heroku allow for automated deployment of apps from the desktop to the cloud.
Nodejitsu provides a tool called jitsu that makes deploying an Node.js application super simple. You can install jitsu with npm.
npm install jitsu -g
Heroku How To
Getting started with jitsu
Use monit, forever, upstart or systemd to start your node server. Use Varnish or HAProxy or Nginx (Nginx not work with websockets).
Ultimately you can stick it anywhere you want. I recommend running your application using Forever or similar instead of directly with Node. I usually keep my apps in /var/ and let each one run under a unique user. I do not recommend keeping them in your http root, as your .js files are should NOT be interpreted by Apache, php, etc.
To be clear - you do NOT need a traditional webserver, nor do you need php,mySQL or anything else. Node is all you need. It'll serve content directly - it IS the webserver.
Often times you'll have each app use a high port number (3000+) and use NGINX to proxy them all to different hostnames off of port 80 (allowing you to easily have multiple apps on a single machine). If you aren't using some sort of proxy, then 3000 is very default, but there is no correct or incorrect port, so long as you don't use a reserved port.

Is it reasonable to put nginx in front of nodejs to serve static assets?

Is it reasonable to put nginx in front of nodejs to serve static assets?
I asked on #node.js irc and Ryan replied for security - "you just may be hacked when some yet-unknown buffer overflow is discovered. Not that that couldn't happen behind nginx, but somehow having a proxy in front makes me happy".
That said is you use a hosted Node service like joyent No.de default app runs on port 80 without Nginx as far as I know.
To setup Node with Nginx see: http://wellconsidered.be/nodejs-on-nginx
Yes you can do that. I like using Node with Express and the thin stack that results. I believe with early versions of Node using a web server like Nginx was recommended due to performance concerns. I'm not sure that's valid anymore.

Resources