The HTTP Server

The HTTP server handles the connections with the client and forwards all the requests to the Node server. In some way, it is a kind of proxy. Currently, there are two main HTTP servers on the market that were used widely in production: Apache and Nginx, both can be used to server Node applications. However, in this book, we will cover Nginx. The main reason for this decision is its simplicity and performance and it is smaller than Apache.

To install Nginx, use apt-get:

$ sudo apt-get install nginx
[sudo] password for abiee:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  nginx-common nginx-core
Suggested packages:
  fcgiwrap nginx-doc
The following NEW packages will be installed:
  nginx nginx-common nginx-core
0 upgraded, 3 newly installed, 0 to remove and 30 not upgraded.
Need to get 348 kB of archives.
After this operation, 1297 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

After the Nginx server is installed, Ubuntu will start the server automatically; however, you can manage the server daemon with the service command:

#Start the nginx server
$ sudo service nginx start
#Stop the nginx server
$ sudo service nginx stop
#Restart the nginx server
$ sudo service nginx restart

You can check whether the server is running by pointing your browser to the server IP, as shown in the following screenshot:

The HTTP Server

Figure 9.6 Nginx fresh installation

The Nginx configuration files are located at /etc/nginx, in this path are two more paths, as follows:

  • sites-available: Each file is a configuration of a single host (subdomain). Note that these files are not active until they are not in sites-enabled.
  • sites-enabled: While the sites-available has a set of configuration files, the sites-enabled are a set of sites that are actually active.

To create a new site, you need to create a new configuration file in the sites-available path:

$ sudo editor /etc/nginx/sites-available/webapp

The configuration content is shown in the following:

upstream webapp {
  server 127.0.0.1:8000;
}

server {
  listen 80 default_server;

  # Configure logs
  access_log /var/log/nginx/webapp.access.log;
  error_log /var/log/nginx/webapp.error.log;

  # Make site accessible from http://www.example.com/
# server_name localhost;
  server_name www.example.com;

  location / {
    # Proxy headers
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    # Proxy to Nodejs
    proxy_pass http://webapp;
    proxy_redirect off;
  }
}

The upstream module of Nginx defines a server or a group of servers that can be referenced as proxy_pass, what it means is that the target to hit when a request is incoming at Nginx. Server configuration creates a new virtual host listening for requests to the server_name address. In this case, it is listening for www.example.com.

In the location block, it describes how to handle the requests; in the previous example, it will forward the request to the webapp upstream, which points to 127.0.0.1:8000. To activate the site, you need to link the contents of this file to the sites-enabled path:

$ sudo ln -s /etc/nginx/sites-available/webapp /etc/nginx/sites-enabled/

Maybe you will need to delete the previous default-enabled site:

$ sudo rm /etc/nginx/sites-enabled/default

Then restart the Nginx server in order to load the new configuration:

$ sudo service nginx restart

If everything is OK, the server will be up:

The HTTP Server

Figure 9.7 Nginx running without node.js working

The preceding image shows a 502 error, that's because the Nginx server is pointing to the proxy_pass setting that has the 127.0.0.1:8000 address; however, nothing is running on that socket. You need to have something listening for requests on the 127.0.0.1:8000 socket, therefore, you should run the project in the same host and the 502 error will go away:

$ npm install --production
$ nodejs app.js

This should be enough to make the server work. However, we don't want to run the app.js script manually each time, there is a better way to launch the node process automatically.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.27