What is NGINX?

Nginx is a web server, proxy, reverse proxy, smtp server and load balancer. Let's break down these concepts right away. Magic ceases to be magic when you understand how the world works.

A web server is a program that accepts and processes requests from clients using the HTTP and HTTPS protocols and returns a response to them in the form of an HTML page. The proxy server receives and processes client requests, and then passes them on to other programs. Reverse proxy server - receives the result of the work of other servers and gives it to clients. The SMTP server is the mail service server. A load balancer is a program that distributes network requests between servers, following the balancing settings.

In this way of working, NGINX can handle up to 1024 simultaneous requests, so it is great for busy websites such as online stores, search engines, and cloud storage, and NGINX also uses a modular system, thanks to which it can expand its functions.

Modules are configured through NGINX configuration files, and we'll talk about them.

NGINX works with two levels of configuration files. The first level is global, it includes the etc/nginx/nginx.conf configuration file. The second level is local, it includes configuration files for specific sites, usually located in /etc/nginx/site-available or /etc/nginx/conf.d/

nginx.conf is the root configuration file for NGINX. It contains its global settings:

With Nginx.conf, NGINX starts parsing configuration files that consist of directives. Directives can be simple - single-line and can be blocked. If a block directive contains another nested block directive, then that block directive is called a context.

All configurations are described using directives, they are also used to connect modules.

For example, in the configuration, I made on this project

The Behavior of this configuration above is described here:

server
{
    listen 443 ssl;
    root /var/www/html;
    index index.php;
    server_name localhost;
		ssl_certificate     /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
    ssl_protocols       TLSv1.2;

location ~ .php$ {
        try_files $uri =404;
        fastcgi_pass wordpress:9000; 
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
  1. When a client connects to the server on port 443, Nginx will first check the SSL certificate and key specified in the configuration to establish a secure SSL/TLS connection.
  2. Nginx will then check the server_name directive to match the hostname of the incoming request. In this case, the server_name is set to "localhost" so it will match any request with that hostname.
  3. Nginx will then check the root directive to determine the root directory for the server. In this case, it is set to "/var/www/html" so any files requested by the client will be searched for in that directory.
  4. Nginx will then check the index directive to determine the default index file. In this case, it is set to "index.php" so if a client requests a directory without specifying a specific file, Nginx will look for this file in the requested directory.