Home » NGINX Processing Requests

NGINX Processing Requests

by Online Tutorials Library

NGINX Processing Requests

We can specify multiple virtual servers, and each server is described by a server {} context.

The above example will give nginx some insight on how to handle incoming requests. First of all, nginx will check the listen directive to test which virtual server is listening on the given IP: port combination. Then the value from the server_name directive is tested against the Host header, which stores the server’s domain name.

Nginx chooses the virtual server in the following order:

  • Server listing on IP: port, with a matching directive i.e., server_name.
  • Server listing on IP: port, with a default_server flag;
  • Server listing on IP: port, first one defined.
  • If there are no matches, refuse the connection.

From the above example, the output will be:

Request to nikita.co:80     => "Hello from nikita.co"  Request to www.nikita.co:80 => "Hello from tutoraspire.co"  Request to deep.co:80     => "Hello from tutoraspire.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 directive is used to accept multiple values; it is also used to handle wildcard matching and regular expressions.

If 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: exact names, wildcards starting with an asterisk, and wildcards ending with an asterisk. If the result is not in the above-declared tables, the regular expressions will be tested sequentially.

Note: –

It is an abbreviation of:

There is only one difference: .tutoraspire.co is stored in the second table, which means that it is a bit slower than an explicit declaration.

listen Directive

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

However, it is also possible to specify the sockets of UNIX-domain:

Even we can use the hostnames:

And if the directive is not present, then use *:80.


You may also like