...

Open source softwares - NGINX

Back to Course

Lesson Description


Lession - #622 Directive and Context


Directive and Context

By default, the nginx configuration file can be located in:

/etc/nginx/nginx.conf,  
/usr/local/etc/nginx/nginx.conf, or  
/usr/local/nginx/conf/nginx.conf  
The location of the configuration file will vary depending on the installation procedure of Nginx. This file has the following content:

Directive

Setup choices in Nginx are known as mandates. This choice has name and boundaries, and this should end with a semicolon (;>
if not Nginx will neglect to stack the arrangement and produce a blunder. Example:

gzip on;  


Context

At the point when we open the center Nginx arrangement record in a content tool, the absolute first thing we will see that the setups are coordinated in a tree-like design encompassed by wavy supports for example "{" and "}". These areas encompassed by supports is called setting for putting arrangement order. Besides, the design mandates alongside their boundaries, end with a semicolon. Here we can pronounce mandates. It is like the degree in a programming language. Setting can be settled inside different settings, accordingly making a setting pecking order. Example:

worker_processes 2; # directive in the global context  
http {              # http context  
    gzip on;        # directive in http context  
  
  server {          # server context  
    listen 80;      # directive in server context  
  }  
}  


Directive Types Since the legacy model varies for various mandates, thusly, we need to focus while involving similar order in different settings. There is a sum of three kinds of mandates, each with its legacy model. Normal It has one worth for each specific situation. What's more, we can characterize it just a single time in the specific situation. Subcontexts can abrogate the parent order, however this supersede will be legitimate just in a given subcontext.

gzip on;  
gzip off; # illegal to have two normal directives in the same context   
  
server {  
  location /downloads {  
    gzip off;  
  }  
  
  location /assets {  
    # gzip is in here  
  }  
}  


Array Adding such a large number of orders in a similar setting will add to the qualities as opposed to overwriting them through and through. Characterizing an order in a subcontext will abrogate every one of the upsides of a parent in the given subcontext.

error_log /var/log/nginx/error.log;  
error_log /var/log/nginx/error_notive.log notice;  
error_log /var/log/nginx/error_debug.log debug;  
  
server {  
  location /downloads {  
    # this will override all the parent directives  
    error_log /var/log/nginx/error_downloads.log;  
  }  
}  


Action Directive

Actions are directives that are used to change things. Their behavior of inheritance will depend on the module. For example: In the rewrite directive case, every matching directive will be executed.

server {  
  rewrite ^ /foobar;  
  
  location /foobar {  
    rewrite ^ /foo;  
    rewrite ^ /bar;  
  }  
} 
If we try to fetch /sample:
  • A server rewrite is executed, rewriting is done from /sample to /foobar.
  • Then the location /foobar is matched.
  • The 1st location rewrite is executed, rewriting from /foobar, to /foo.
  • The 2nd location rewrite is executed, rewriting from /foo, to /bar.


Type of Context

Let's see an example.

# Global context  
 ...  
 ...  
 # http context  
http{  
     ...  
     ...  
     # Server context  
     server {  
              listen 80;  
              server_name example.com;  
              ...  
              ...  
              # Location context  
              location / {              
                          root /var/www/html;              
                          try_files $uri $uri/ =404;          
                          ...  
                          ...  
             }  
    }  
    # Server context  
    server {  
             ...  
             ...  
             # Location context  
             location / {   
                         ...  
                         ...  
             }  
    }  
    ...  
    ...  
}  
From the above example, we can see that the HTTP setting announces settings for HTTP convention. Virtual host settings are proclaimed in the server setting and furthermore contained in the http setting. Areas settings that are utilized to store URLs settings are held inside a server setting.

Main Context

The most broad setting is the primary setting. It is likewise called the worldwide setting. The principal setting sets the settings for Nginx all around the world, and is the main setting that isn't held back inside the run of the mill setting squares and that isn't encircled by wavy supports. The primary setting is put toward the start of the center Nginx design document. The mandates for this setting can't be acquired in some other setting and hence can't be abrogated. The primary setting is utilized to design subtleties that influence the whole application on an essential level. A few normal subtleties that are designed in the principal setting are the client and gathering to run the laborer processes as, the absolute number of laborers, and the document to save the primary interaction ID. The default mistake document for the whole application can be set at the primary setting level.


user nginx;  
worker_processes auto;  
pid /run/nginx.pid;  
...  
...  


Events Context

The events setting sets worldwide choices for association handling. The occasions setting is held inside the principal setting. There can be just a single occasion setting characterized inside Nginx setup. Nginx utilizes an occasion based association handling model, so the order characterized inside this setting decides how specialist cycles ought to deal with associations.

# main context  
events {  
        # events context  
        worker_connections 768;   
        multi_accept on;  
}  
...  
...  


HTTP Context

The HTTP setting is utilized to hold the mandates for taking care of HTTP or HTTPS traffic. The HTTP setting is a kin of the setting of the occasion, so they should be recorded next to each other, as opposed to settled. The two of them are the offspring of the fundamental setting. Lower settings handle the solicitation, and the mandates at this level control the characterized defaults for each virtual server.

user nginx;  
worker_processes auto;  
pid /run/nginx.pid;  
...  
...  
events {  
        # events context  
        worker_connections 768;   
        multi_accept on;  
        ...  
        ...  
}  
http {  
       sendfile on;   
       tcp_nopush on;     
       tcp_nodelay on;    
       keepalive_timeout 65;  
       ...  
       ...  
}  


Server Context

The server setting is pronounced inside the http setting. The server setting is utilized to characterize the Nginx virtual host settings. There can be different server settings inside the HTTP setting. The orders inside the server setting handle the handling of solicitations for assets related with a specific area or IP address. The orders in this setting can abrogate large numbers of the mandates that might be characterized in the http setting, including the archive root, logging, pressure, and so forth. Notwithstanding the mandates that are taken from the http setting, we can likewise arrange records to attempt to answer demands, issue diverts, and modifies, and set erratic factors.

user nginx;  
worker_processes auto;  
pid /run/nginx.pid;  
...  
...  
events {  
         # events context  
         worker_connections 768;      
         multi_accept on;  
         ...  
         ...  
 }  
http {  
       sendfile on;   
       tcp_nopush on;     
       tcp_nodelay on;    
       keepalive_timeout 65;  
       ...  
       ...  
  
       server {  
                listen 80;  
                server_name domain1.com;  
                root /var/www/html/wordpress;  
                ...  
       
       }  
       server {  
                listen 80;  
                server_name domain2.com;  
                root /var/www/html/drupal;  
                ...  
       
       }  
}  


Location Context

Area settings characterize mandates to deal with the solicitation of the client. Whenever any solicitation for asset shows up at Nginx, it will attempt to match the URI (Uniform Resource Identifier>
to one of the areas and handle it in like manner. Numerous area settings can be characterized inside the server blocks. Besides, an area setting can likewise be settled inside another area setting.

http {  
       ...  
       ...  
  
       server {  
                listen 80;  
                server_name domain1.com;  
                root /var/www/html/wordpress;  
                ...  
                location /some_url {    
                # configuration for processing URIs starting with /some_url  
                }  
                location /another_url {    
                # configuration for processing URIs starting with /another_url  
                }      
       }  
       server {  
                listen 80;  
                server_name domain2.com;  
                root /var/www/html/drupal;  
                ...  
                location /some_url {    
                # configuration for processing URIs starting with /some_url    
                }  
                location /some_other_url {    
                # configuration for processing URIs starting with /some_other_url  
                }    
       
       }  
}  


Upstream Context

The upstream setting is utilized to arrange and characterize an upstream server. This setting is permitted to characterize a pool of backend servers that Nginx could intermediary the pre-owned when at any point demand. This setting is generally we are arranging intermediaries of different kinds. Upstream setting empowers Nginx to perform load adjusting while at the same time proxying the solicitation. This setting is characterized inside the HTTP setting and outside any server setting. The upstream setting is referred to by name inside server or area blocks. And afterward pass the solicitations of a particular kind to the pool of characterized servers. The upstream will then, at that point, utilize a calculation (as a matter of course cooperative effort>
to figure out which explicit server should be utilized to deal with the solicitation.

http{  
     ...  
     ...  
     upstream backend_servers {  
                                server host1.example.com;  
                                server host2.example.com;  
                                server host3.example.com;  
     }  
  
server {  
             listen 80;  
             server_name example.com;  
             location / {  
                           proxy_pass http://backend_servers;  
             }  
  }  
}  


Mail Context

In spite of the fact that Nginx is most frequently utilized as a web or opposite intermediary server, it can likewise be utilized as an elite execution mail intermediary server. The setting that is utilized for this kind of mandates is known as the mail setting. The mail setting is characterized inside the fundamental or worldwide setting or beyond the http setting. The primary reason for the mail setting is to give a region to arranging a mail proxying arrangement on the server. Nginx can divert verification solicitations to an outside confirmation server. It can then give admittance to POP3, SMTP, and IMAP mail servers for serving the real mail information. As a rule, a mail setting will seem to be this:

# main context  
mail {  
       server_name mail.example.com;  
       auth_http   localhost:9000/cgi-bin/nginxauth.cgi;  
       proxy_pass_error_message on;  
       ...  
}  
http {  
  
}  
...  
...  


If context

The assuming setting is utilized to permit restrictive execution of mandates characterized inside it. On the off chance that the setting is very much like "if articulation" of some other programming dialects. The assuming that setting will execute the contained guidelines assuming that a given condition brings valid back. The assuming setting ought to be kept away from quite far because of certain impediments of it. Utilize this connect to find out about why nginx ought to be kept away from, which is examined here.

http {  
        server {  
                     location /some_url {  
                     if (test_condition>
{ # do some stuff here } } } }


Limit_except context

The limit_except setting is utilized to forestall the utilization of all HTTP techniques inside the area setting with the exception of the ones that we unequivocally permit. For instance, assuming specific clients ought to approach POST content and everybody ought to can peruse the substance, then, at that point, we can utilize limit_except setting for this.

...  
...  
location /wp-admin/ {   
    limit_except GET {   
      allow 127.0.0.1;   
      deny all;   
    }   
}  
...  


Miscellaneous Contexts

Aside from the above settings, there are not many different settings accessible in Nginx and are depicted underneath. These settings are subject to discretionary modules and are utilized once in a blue moon.
  • split_clients: The split_client setting parts the client's solicitation into at least two classifications. This setting is characterized in the HTTP setting and is essentially utilized for A/B testing.
  • geo: The geo setting arranges client IP addresses. It is utilized to plan the worth of a variable relying upon the associating IP addresses.
  • charset_map: This setting is utilized to add explicit charset to the "Content-Type" reaction header field. Likewise, utilizing the specific situation, one can change over information starting with one charset then onto the next charset for certain restrictions.
  • map: The guide setting is utilized to make factors whose values rely upon the upsides of different factors and are characterized in the http setting.
  • perl/perl_set: It is utilized to execute area and variable controllers in Perl and embed Perl calls into SSI. Further, with the assistance of the perl_set setting, we can introduce a Perl controller for a particular variable.
  • types: The sorts setting maps the MIME types with right document expansions. This setting might show up in the http setting, server setting or area setting.