Monday, December 13, 2021

Installing Nginx Web Server in Ubuntu and a Quick Tour about it

Hi,

In this post we are going to see how to install Nginx web server and perform some actions on it...this would be a quick start guide for those who want to browse about Nginx.

The first and foremost...installating Nginx.

Installing Nginx web server

Use the following commands to install Nginx web server.

$ sudo apt update
$ sudo apt install nginx

Well that is it all about the installation of Nginx...pretty easy right....

Starting Nginx

By default when you install Nginx, the web server would be up and running but when you found that web server is off...how would you turn it on? the answer is the command given below...

$ sudo /etc/init.d/nginx start

Signals of Nginx

With respect to Nginx, the web server can be controlled by signals

  • Signals
    • stop : for fast shutdown
    • quit : for graceful shutdown
    • reload : to reload the configuration changes
    • reopen : to reopen the log files
    • restart : to restart the nginx web server

the signals given above are self explanatory and we will see how to use those signals in the following section

How to? Signal Usages
$ nginx -s stop
$ nginx -s quit
$ nginx -s reload
$ nginx -s reopen
$ nginx -s restart

Structure of the Configuration File

Nginx web server consists of module which are controlled by directives mentioned in the configuration file

There are two types of directives....

  • Directives
    • Simple Directive
      consist of name followed by parameters with space separatThere are two types of directives....ed and terminated by a semi colon
    • Block Directive
      same as simple directive but instead of terminating with the semi colon...this directive is enclosed by set of braces or paranthesis. if a block directive can have other directive other directives inside braces or paranthesis, then it is called context.

Directives placed outside of any contexts are considered to be a part of the main context.

Serving Static Content

The purpose of any web server is to serve contents which is requested through a URL. Nginx is no exception on that and the speciality about Nginx is that we can define the locations where the contents are stored to look for.

For instance, the html files can be served form the location called /var/www/html and the images can be served from a different location such as /var/www/images.

Nginx would be known about these locations via respective entries in the configuration file.

Let us try out some of the entries in the configuration file.

In order to server the html files from the location mentioned above... the configruation file should have a block entry named "location" inside the block entry "server" again which is inside the block entry "http", typically the entry should look like the one given below...

    http{
        server{
            location /login.html{
                root /var/www/
            }
        }
    }

Similar to the entry above, we can have a entry which serves images like the one given below....

    http{
        server{
            location /images{
                root /var/www/images
            }
        }
    }

Having said the above two configurations are in place and nginx server has encountered an url such as...

http://localhost/login.html

Now the nginx websever would refer the configuration details to identify the directory for contents, in this case the the directory would be /var/www and if the server finds the file login.html it would be served to the client.

For the next case assuming the login page referring an image and url for the image resource be like....

http://localhost/images/login.png

Now the nginx server would refer the images directory to server the file login.png.

Once we are sure about the configurations defined in the config file we should now let the web sever know about it.

To make the web server know about the configurations we have to load the configurations defined so far.... To achieve this let us reload the server using the command given below....

$ nginx -s reload

with this we can conclude the static content service provided by the nginx server.

Setting up Proxy Server

Now we are going to talk about a important application of nginx server, which is nginx as a proxy server.

As we all know, a server can be used as a proxy for clients and for servers, to be precise nginx web server can be used as both forward proxy and reverse proxy.

Let us now touch upon these two concepts very briefly...

Forward Proxy

Forward proxy is a concept that is applied on the incoming request form the client for the server.... the proxy server sits in between the client and the actual server that does the real job, in which case the server does not know anything about the actual client.

what the proxy server does is... it intercepts the actual request from the client and mask the IP address as part of the request and forward the request to the actual server for processing.

Now for the actual server the client is the proxy server and not the actual client, the actual server does its work and generate the response and sent it back to the proxy server.

The proxy server receives the response, it unmask the IP address and sent it back to the actual client.

To be precise, forward proxy is for the client and not for the server and to be more precise the forward proxy would normally resides in local network and not on the internet.

Reverse Proxy

Reverse proxy on the other hand is the concept applied on the response generated by the actual server.

Here the clients or the request creators does not know about the actual servers, from the client point of view the proxy server is the actual server that creates the response for the request.

This approach is to protect the servers form the malicious client access.

Rever proxy sits on the internet and not on the local network.

Nginx is designed to be a reverse proxy.

Let us see how these two are done in nginx.

Nginx as Reverse Proxy

Let us now see some examples how Nginx server can be used as a reverse proxy

    http {
        server {
            location /home {
                proxy_pass http://localhost:8080/
            }
            location /images{
                root /var/www/images
            }
        }
    }

from the above example given, the first location uses the proxied server for the mapping /home and for serving images the mapping /images is used which is the local folder.

Nginx as FastCGI proxy

Another application of Nginx server is to proxy the fastCGI server where there application build on various frameworks and languages like PHP would run.

Taking the above configuration as a basis, we can have similar configuration for FastCGI proxying.

We will be using fastcgi_pass directive rather proxy_pass for FastCGI configuration.

    http {
        server {
            location /home {
                fastcgi_pass localhost:8080
            }
            location /images{
                root /var/www/images
            }
        }
    }

Well with this we can conclude the installation of Nginx Server and its quick tour.

Let me know what you feel about this post through comments...

No comments:

Post a Comment

How to change the root password in linux when it is forgotten/to change

This blog is all about changing the root password of the Linux system when it is forgotten or to reset the password...   Let's get it ...