Nginx

Lightweight open-source web server, as well as a reverse proxy server and load balancer. Its efficient design and modular architecture make it a powerful tool for managing web traffic.

What is NGINX?


Nginx is primarily a web server used in high-performance websites, but it can also be used as a load balancer and reverse proxy.


Igor Sysoev, the creator of Nginx, started its development in the early 2000s to address a major problem. At that time, many web servers couldn't handle more than 10,000 simultaneous connections.


Nginx is especially popular due to its ability to handle growing traffic and be easily scalable with minimal hardware. Additionally, it excels at delivering static files quickly.


While many web servers use a process-based or simple thread-based architecture, Nginx takes a different approach by using an asynchronous, event-driven architecture. This allows the web server to handle multiple connections within a single process.


NGINX has a master process that performs privileged operations such as binding ports, reading and evaluating configuration files, and creating multiple worker processes.


Here are three types of Nginx worker processes:


Cache loader process: It can load disk-based cache into the memory zone. This process has low resource demands as it runs only once, right after NGINX is started.


Cache manager process: It aims to keep the amount of cached data within configured sizes by periodically checking the cache and removing least-recently accessed data.


Worker process: It can handle hundreds of thousands of concurrent HTTP connections, eliminating the need to create new processes or threads for each connection. Instead, each worker process runs independently and contains smaller units called worker connections, with each unit responsible for handling request threads. Worker processes can also communicate with upstream servers, as well as read and write content to disk.


NGINX's event-driven architecture effectively distributes client requests among worker processes, making it perform better than Apache in terms of scalability.


Since NGINX can effortlessly process thousands of requests, even on low-resource systems, this web server is suitable for high-traffic websites such as search engines, e-commerce sites, and cloud storage services.


Install (Debian 11)


Install Nginx


  sudo apt install nginx


Starting Nginx on Debian


  sudo systemctl start nginx


Check the Nginx Status


  sudo systemctl status nginx


Configure a Virtual Host


If you want to set up a virtual host, create a new configuration file in the /etc/nginx/sites-available directory. For example, to create a file for vicente-munoz.cl, run the following command:


  sudo nano /etc/nginx/sites-available/vicente-munoz.cl


Add the following code to the file:


  server {
      listen 80;
      listen [::]:80;
  
      server_name vicente-munoz.cl www.vicente-munoz.cl;
  
      root /var/www/vicente-munoz.cl/html;
      index index.html;
  
      location / {
          try_files $uri $uri/ =404;
      }
  }


In this example, we’re creating a virtual host for vicente-munoz.cl, setting the root directory, and defining which file to use for the index. We’re also configuring file and folder checks when requests are made.


Let’s take a closer look at configuring a virtual host.


listen: determines which IP address and port to listen for incoming connections. In this example, “listen 80” indicates that the server will listen for connections on port 80, which is typically used for HTTP traffic.


server_name: specifies the domain name of the website served by this virtual host. In this example, “server_name vicente-munoz.cl” indicates that this virtual host will serve a website with the domain name example.com.


root: specifies the root directory where the website files are located. In this example, “root /var/www/vicente-munoz.cl” indicates that the website files are located in the directory /var/www/vicente-munoz.cl.


index: specifies the name of the file that will be used as the main page of the website. In this example, “index index.html” indicates that the file index.html will be used as the main page.


location: specifies additional settings for processing requests depending on the URL address. In this example, “location /” indicates that all requests that start with / will be processed according to the specified rules. In this case, if the requested file is not found, Nginx will return a 404 error.


Activate the Virtual Host nginx on Debian


To activate the virtual host, create a symbolic link to the configuration file in the sites-enabled directory:


  sudo ln -s /etc/nginx/sites-available/vicente-munoz.cl /etc/nginx/sites-enabled/vicente-munoz.cl


Restart Nginx


  sudo systemctl restart nginx