DEV Community

loading...
Cover image for Configure Nginx as a Web-server :

Configure Nginx as a Web-server :

rahulku48837211 profile image Rahul Kumar ・4 min read

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

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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;
}
}
Enter fullscreen mode Exit fullscreen mode

$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 .

Discussion

pic
Editor guide