1 Answer

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

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 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 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 directive is used to accept multiple values; it is also used to handle wildcard matching and regular expressions.

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  

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: -

server_name .javatpoint.co;  

It is an abbreviation of:

server_name  javatpoint.co  www.javatpoint.co  *.javatpoint.co;  

There is only one difference: .javatpoint.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.

listen 127.0.0.1:80;  

listen 127.0.0.1;    # 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.

Related questions

0 votes
asked Sep 5, 2019 in NGINX by Robin
0 votes
asked Sep 5, 2019 in NGINX by Robin
...