Nodejs webserver for production - node.js

A little update to the common question. As of the current version of Nodejs v0.6.5, is it safe to run it as a webserver in production? I really wanna skip the step of using nginx for example for proxy. I am gonna use Expressjs, nowjs, gzippo. And nginx doesnt support websockets yet, and it's a little hard to setup socket.io over ssl. Are there any more benfits to nginx other than that it serves static files better?
Any advice on this matter? And if it's ok to run as a webserver, are there any other modules worth concidering?

To be honest aside from serving static file I don't really see any important benefits (though Nginx may have more server-specific extensions).
Also you might want to use bouncy or node-http-proxy for proxying and browserify to use you server-side modules on the frontend.
Edit: also you would not be the first using Node without Nginx, as far as I know Trello and other websites are also using it.

Other benefits of Nginx besides serving static files.
You can have it compress dynamically or load up a .gz file even if the non-compressed is reqeusted.
You can cache the generation of anything, reducing a call back to node.js.
You can have it route to a cluster of node application servers
Lots of other neat stuff http://wiki.nginx.org/Modules
Using nginx though isn't required, and running node with nothing in front of it is perfectly fine.

Related

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.

Serve nodejs app from nginx like with php?

I use Nginx to serve my php applications for dev purposes.
On Ubuntu it works out of the box.
I want to do the same for Node.js apps.
Is this possible without doing nodejs app.js before?
How to achieve this in a single Nginx conf file?
PHP and node.js are oil and water. PHP requires a web server to run the .php files, however node.js typically creates its own web server. Since you are creating your own web server, in many cases you wouldn't find it necessary to serve your application from Nginx, however, if you truly insisted on "serving" it from Nginx, you would need to proxy it.
This is not possible without doing nodejs app.js before, due to the way node.js works.
This question best answers your question regarding proxy'ing via Nginx.
As a closing remark, its good to remember that node.js does in fact (in most cases) implement its own web server, and PHP does not.

node.js socket.io apache server

I have some sort of noob question regarding to node.js and socket.io. These two guys are usually needed to use websockets for server to browser communication. Once that my project gets done, do I have to upload my php files to my apache server. Now my question is do I have also to upload my node_modules directory to my apache server? along with socket.io directory?
In order to run your node.js process you need to have ability to execute own applications on server. Classic virtual server does not allow of such functionality. There is virtual server hosting for node.js specifically, check out nodejitsu, heroku or any other.
You can run node.js as web platform by it self, without of use of any apache, that will make your life much easier. But if you still need apache and PHP, then you need to consider proxying from apache to your node.js process. This still can be tricky as proxying WebSockets is not straight forward.
If you have access to Apache settings, then you can enable proxying for HTTP/HTTPS, read this: http://ronenagranat.blogspot.co.uk/2011/02/apache2-reverse-proxy-for-nodejs.html
For WebSockets, there is not much data available but here is possible solution: http://blog.cafarelli.fr/post/2013/04/26/Backporting-Apache-support-for-websockets-reverse-proxy-(aka-getting-GateOne-to-work-behind-Apache)
Although if you have own server and have ability to choose between web platform, I would recommend to look into nginx, as it has support for WebSockets proxying (might require module and nginx recompiling).

Node equivalent to index.html

I'm coming from a LAMP background and want to test out node for production.
What seems a little confusing to me is that in Apache url's map to folders, and the server will automatically look for an index.html or index.php if you aren't re-writing urls.
What would the equivalent be in node?
I'm thinking it'd be something like checking the request url and matching it, then loading a specific node module which runs the app.
This might seems simple for a single app, but we run tons of client apps on our server so i'm used to having different frameworks in different folders and index.php just runs it.
to be more specific. i'm currently running a few codeigniter and wordpress installations on our server. so i'd want to run a few node apps/frameworkds in different 'subfolders'
With Node you don't really use Apache. It's similar to Ruby in that is runs its own web server.
However, you can probably get Apache to run Node files using mod_node. As far as I know this is non-standard though and you definitely lose the "non-blocking" advantages of Node. But for experimenting (and not load testing) it's fine.
Check out Express if you're looking for an MVC architecture written in Node.
If you're just looking to run the most basic sample web server, just run the example hosted on the main page: http://nodejs.org/
Lastly, I had that same issue you are experiencing where I have one box hosting a lost of stuff and Apache taking up port 80. The answer here is to use a reverse proxy like Nginx to run on port 80 and redirect traffic to Apache / Node / Ruby / etc. Best of both worlds and since Nginx is written non-blocking you still gain the benefits of node.
I actually wrote an in-depth blog article about this a few months ago:
http://readystate4.com/2011/07/15/nginx-apache-and-node-all-living-harmony/

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