Mastering Nginx

(Ron) #1

A Configuration Guide


[ 32 ]

Besides a normal string, NGINX will accept a wildcard as a parameter to the


server_name directive:



  • The wildcard can replace the subdomain part: *.example.com

  • The wildcard can replace the top-level-domain part: http://www.example.*

  • A special form will match the subdomain or the domain itself:
    .example.com (matches *.example.com as well as example.com)


A regular expression can also be used as a parameter to server_name by prepending
the name with a tilde (~):


server_name ~^www.example.com$;


server_name ~^www(\d+).example.(com)$;


The latter form is an example using captures, which can later be referenced


(as $1, $2, and so on) in further configuration directives.


NGINX uses the following logic when determining which virtual server should
serve a specific request:



  1. Match the IP address and port to the listen directive.

  2. Match the Host header field against the server_name directive as a string.

  3. Match the Host header field against the server_name directive with a
    wildcard at the beginning of the string.

  4. Match the Host header field against the server_name directive with a
    wildcard at the end of the string.

  5. Match the Host header field against the server_name directive as a regular
    expression.

  6. If all the Host headers match fail, then direct to the listen directive
    marked as default_server.

  7. If all the Host headers match fail and there is no default_server,
    direct to the first server with a listen directive that satisfies step 1.

Free download pdf