Configure Nginx as a Web-server :
Tags: webserver, webdev, backend, nginx
Published: 2020-11-14 05:29:43

# __What is Nginx :__
Nginx is a high performance web server through which we can make sure that our page load time is reduced .

# __Features of Nginx :__
* __web server for reverse proxying , load balancing, and caching .__
* __provides http server capabilities .__
* __Designed for maximum server capabilities .__
* __Functions a proxy server for email(IMAP, POP3 and SMTP) .__
* __Uses a non-threaded and event-driven architecture .__  

# __What is web server :__
When we type www.google.com a __http request__ is generated and travels through the local area network to the computer that has the contents of the web page and then in the response we got our desired web page . or we can say that a web server are computers which deliver the requested web pages .Every web server has a ip address and a domain name .

# __Nginx Architecture :__
Nginx uses Master slave architecture by supporting event-driven , asynchronous and non-blocking model .

![Nginx Architecture](https://dev-to-uploads.s3.amazonaws.com/i/ikjmr40f6n1zs6xtsqo0.png)

When we have a process thread model each and every process can be used for input output operation and we can generate a separate thread that is required . very inefficient in terms of memory and cpu utilization , just imagine you have a large enterprise .
spinning a separate thread basically requires a new runtime environment including allocation of heap memory . additional cpu time is utilized for creating these items this leads to a  poor performance due to thread crashing and extensive context switching . 
Nginx uses multiplexing and event notification heavily and dedicates a specific task to separate process . for example if we have ten tasks then we have dedicated ten different process .
Connections are also processed in a highly efficient run loop in limited number of single thread process called works . 

In the above diagram it is mentioned three worker node so from that Nginx can process thousands of concurrent connections and request per seconds . 

worker process accept new request from a shared listen socket and execute a highly efficient run loop inside each worker process to process thousands of process .

Master process is for reading and validating configuration .Master is responsible for creating , binding sockets and also used for stating , terminating and maintaining the configured worker process . Also responsible for re-configuring without any server interaction also compiles embedded Perl scripts .

Coming to the __cache loader__ is responsible for checking on disk cache item and populating the Nginx in memory database with cache metadata (already stored on the disk with a specific allocated directory structure).

__Cache manager__ is responsible for cache expiration and invalidation . It basically stores in the memory in normal Nginx operation and restarted by the master in case of failure .   

# __Why one should use Nginx :__
* __Ease of installation and maintenance :__
Easy to install simply just we can do in two to three commands .
* __Improve performance :__
if we have two or more web servers running then Nginx can routing the traffic through those web servers . 
* __offers scalability :__
Easily handle increasing number of concurrent requests .
* __Reduces the wait time for users :__
If there is a web server which is heavy we can make sure that the page load time is reduced .
* __load balancing :__
If a lot of traffic is coming to our site Nginx can route those traffics to different web servers . 
* __on the fly upgrades :__
we can patch or upgrade our server without a downtime .

# __Configuration settings :__
The core settings of Nginx are mainly configured in the nginx.conf file . The configuration file is mainly structured into contexts .

* __Worker processes :__ A setting that define Number of worker process that we can use (Number of cpu cores) 
* __Worker connection :__ how many simultaneously user can serve . 
* __access log and error log :__ debugging and trouble shootings
* __gzip :__ gzip compression of Nginx responses .

# __How to install Nginx :__
* Install Nginx
``` $sudo apt install nginx ```
* Adjust the firewall 
``` $sudo ufw enable ```
``` $sudo ufw app list ```
``` $sudo ufw allow Nginx FULL ```
``` $sudo ufw allow Nginx HTTPS ```
``` $sudo ufw allow Nginx FULL ```
``` $sudo ufw allow Nginx HTTP ``` 
``` $sudo ufw status ```

# __Hands On :__
Lets deploy a static web page application .
prerequisites before we started :
```
* install curl 
* install openssh
* install a editor
```
* copy the demo file to the /var/www directory (The basic static site is located in this directory) : 
``` $sudo cp -r /home/rahul/downloads/. /var/www ```

* point your domain name to the new server .
``` $cd /demo ```
``` $sudo nano /etc/hosts ```
```
0.0.0.0 sample.com
```
* if you doing in a remote server 
__root@ip_add_of_remote_server__ instead of __0.0.0.0__
 
add the static files to the server
``` $sudo chmod -r 777 * ```
``` $scp -r * 0.0.0.0:/var/www/demo ```

``` $cd/etc/nginx ```
sites available contains the configuration files for the Nginx web server and the sites enable contains the links to that files that Nginx has been configured to run .

``` $sudo vi sites-available/demo ```
```
server {
listen 80 default_server;
listen [::] default_server;
root /var/www/demo;
index index.html;
server_name demo;
location / {
try_files $uri $uri/ = 404;
}
}
```

``` $sudo ln -s /etc/nginx/sites-available/demo /etc/sites-enabled/demo ```

since the default file  of Nginx is also configured to run on port 80 we have to move it to another directory 

``` $sudo mv /etc/nginx/sites-enabled/default /home/rahul/default ```
``` $sudo systemctl restart nginx ```

__Now you can see your web page up and running .__