This is a practical and basic guide to run node.js on an nginx based server.
I won’t go into details on how to install nginx on your Linux based server. The basic idea here is that you use nginx to route all traffic that needs to be processed by node to the specified service on your network or local server. You could do this for PHP processing (via PHP-FPM or Fast-CGI) as well, the same as we are doing it here for .. node.js.
Your site configuration file for nginx might look like this:
These lines tell nginx to route all traffic (location /) to a service running on your localhost on port 8124. This, of course, requires your node.js site to be running on that specified address and port, and that’s what we’ll do next.
A minimal node.js project will, sort of, look like this:
It catches all HTTP requests and sends ‘Rock on!’ back to the browser.
To run it (command-line):
This is, in a most basic form, exactly what you want: an application listening on port 8124 that will process all HTTP requests. Starting your app like this is ok when you’re in development mode. But you probably want your node application to be running as a background process on your production machine and automatically restart the app, when it fails for some reason or another.
I was going to get into the deployment of Node.js apps when DailyJS released a great blog post about it. It covers all sorts of deployment and hosting methods. So I will skip this part and kindly direct you to their ‘Node Deployment Recipes’.