Open source softwares - NGINX

Back to Course

Lesson Description

Lession - #634 NGINX Processing Requests

NGINX Processing Requests

We can indicate different virtual servers, and every server is portrayed by a server {} setting.

server {  
  listen      *:80 default_server;  
  server_name javatpoint.co;  
  return 200 "Hello from javatpoint.co";  
server {  
  listen      *:80;  
  server_name nikita.co;  
  return 200 "Hello from nikita.co";  
server {  
  listen      *:81;  
  server_name deep.co;  
  return 200 "Hello from deep.co";  
The above example will give nginx some knowledge on the most proficient method to deal with approaching solicitations. Most importantly, nginx will check the listen order to test which virtual server is tuning in on the given IP: port blend. Then the worth from the server_name mandate is tried against the Host header, which stores the server's area name. Nginx picks the virtual server in the accompanying request:
  • Server posting on IP: port, with a matching order i.e., server_name.
  • Server posting on IP: port, with a default_server banner;
  • Server posting on IP: port, initial one characterized.
  • In the event that there are no matches, decline the association.
From the above example, the result will be:

Request to nikita.co:80     => "Hello from nikita.co"
Request to www.nikita.co:80 => "Hello from javatpoint.co"
Request to deep.co:80     => "Hello from javatpoint.co"
Request to deep.co:81     => "Hello from nikita.co"
Request to nikita.co:81     => "Hello from deep.co"

The server_name directive

The server_name order is utilized to acknowledge numerous qualities; it is likewise used to deal with trump card coordinating and customary articulations.

server_name javatpoint.co www.javatpoint.co; # exact match  
server_name *.javatpoint.co;              # wildcard matching  
server_name javatpoint.*;                 # wildcard matching  
server_name  ~^[0-9]*\.javatpoint\.co$;   # regexp matching  

there is any ambiguity, then nginx uses the following order: Exact name;
  • Longest wildcard name starting with an asterisk, for example, "*examples.org",
  • Longest wildcard name ending with an asterisk, for example, "mail.*";
  • First matching the regular expression.

Nginx will store three hash tables: careful names, trump cards beginning with a bullet, and special cases finishing with a reference mark. In the event that the outcome isn't in the above-pronounced tables, the standard articulations will be tried successively.

server_name .javatpoint.co;

It is an abbreviation of:

server_name  javatpoint.co  www.javatpoint.co  *.javatpoint.co;  
There is just a single distinction: .javatpoint.co is put away in the subsequent table, and that implies that it is without a doubt more slow than an express statement.

listen Directive

In most of the cases, we will see that the listen directive accepts IP: port values.

listen;    # port :80 is used by default  
listen *:81;  
listen 81;           # all ips are used by default  
listen [::]:80;      # IPv6 addresses  
listen [::1];        # IPv6 addresses  
However, it is also possible to specify the sockets of UNIX-domain:

listen unix:/var/run/nginx.sock;

Even we can use the hostnames:

listen localhost:80;  
listen netguru.co:80;  
And if the directive is not present, then use *:80.