I've set up Node.js and Nginx on my server. Now I want to use it, but, before I start there are 2 questions:
- How should they work together? How should I handle the requests?
There are 2 concepts for a Node.js server, which one is better:
a. Create a separate HTTP server for each website that needs it. Then load all JavaScript code at the start of the program, so the code is interpreted once.
b. Create one single Node.js server which handles all Node.js requests. This reads the requested files and evals their contents. So the files are interpreted on each request, but the server logic is much simpler.
It's not clear for me how to use Node.js correctly.
Nginx works as a front end server, which in this case proxies the requests to a node.js server. Therefore you need to set up an Nginx config file for node.
This is what I have done in my Ubuntu box:
Create the file
yourdomain.example
at/etc/nginx/sites-available/
:In it you should have something like:
If you want Nginx (>= 1.3.13) to handle websocket requests as well, add the following lines in the
location /
section:Once you have this setup you must enable the site defined in the config file above:
Create your node server app at
/var/www/yourdomain/app.js
and run it atlocalhost:3000
Test for syntax mistakes:
Restart Nginx:
Lastly start the node server:
Now you should see "Hello World" at
yourdomain.example
One last note with to starting the node server: you should use some kind of monitoring system for the node daemon. There is an awesome tutorial on node with upstart and monit.