Understanding Nginx Server and Location Block Selection Algorithms | DigitalOcean - 0 views
-
A server block is a subset of Nginx’s configuration that defines a virtual server used to handle requests of a defined type. Administrators often configure multiple server blocks and decide which block should handle which connection based on the requested domain name, port, and IP address.
-
A location block lives within a server block and is used to define how Nginx should handle requests for different resources and URIs for the parent server. The URI space can be subdivided in whatever way the administrator likes using these blocks. It is an extremely flexible model.
-
Nginx logically divides the configurations meant to serve different content into blocks, which live in a hierarchical structure. Each time a client request is made, Nginx begins a process of determining which configuration blocks should be used to handle the request.
- ...37 more annotations...
-
Nginx is one of the most popular web servers in the world. It can successfully handle high loads with many concurrent client connections, and can easily function as a web server, a mail server, or a reverse proxy server.
-
The main server block directives that Nginx is concerned with during this process are the listen directive, and the server_name directive.
-
The listen directive typically defines which IP address and port that the server block will respond to.
-
Nginx translates all “incomplete” listen directives by substituting missing values with their default values so that each block can be evaluated by its IP address and port.
-
If there are multiple server blocks with the same level of specificity matching, Nginx then begins to evaluate the server_name directive of each server block.
-
Nginx will only evaluate the server_name directive when it needs to distinguish between server blocks that match to the same level of specificity in the listen directive.
-
Nginx checks the request’s “Host” header. This value holds the domain or IP address that the client was actually trying to reach.
-
Nginx will first try to find a server block with a server_name that matches the value in the “Host” header of the request exactly.
-
If no exact match is found, Nginx will then try to find a server block with a server_name that matches using a leading wildcard (indicated by a * at the beginning of the name in the config).
-
If no match is found using a leading wildcard, Nginx then looks for a server block with a server_name that matches using a trailing wildcard (indicated by a server name ending with a * in the config)
-
If no match is found using a trailing wildcard, Nginx then evaluates server blocks that define the server_name using regular expressions (indicated by a ~ before the name).
-
If no regular expression match is found, Nginx then selects the default server block for that IP address and port.
-
Location blocks live within server blocks (or other location blocks) and are used to decide how to process the request URI (the part of the request that comes after the domain name or IP address/port).
-
=: If an equal sign is used, this block will be considered a match if the request URI exactly matches the location given.
-
~: If a tilde modifier is present, this location will be interpreted as a case-sensitive regular expression match.
-
~*: If a tilde and asterisk modifier is used, the location block will be interpreted as a case-insensitive regular expression match.
-
^~: If a carat and tilde modifier is present, and if this block is selected as the best non-regular expression match, regular expression matching will not take place.
-
Keep in mind that if this block is selected and the request is fulfilled using an index page, an internal redirect will take place to another location that will be the actual handler of the request
-
Keeping in mind the types of location declarations we described above, Nginx evaluates the possible location contexts by comparing the request URI to each of the locations.
-
Nginx begins by checking all prefix-based location matches (all location types not involving a regular expression).
-
If no exact (with the = modifier) location block matches are found, Nginx then moves on to evaluating non-exact prefixes.
-
After the longest matching prefix location is determined and stored, Nginx moves on to evaluating the regular expression locations (both case sensitive and insensitive).
-
regular expression matches within the longest prefix match will “jump the line” when Nginx evaluates regex locations.
-
The exceptions to the “only one location block” rule may have implications on how the request is actually served and may not align with the expectations you had when designing your location blocks.
-
In the case above, if you really need the execution to stay in the first block, you will have to come up with a different method of satisfying the request to the directory.
-
one way of preventing an index from switching contexts, but it’s probably not useful for most configurations
-
the try_files directive. This directive tells Nginx to check for the existence of a named set of files or directories.
-
the rewrite directive. When using the last parameter with the rewrite directive, or when using no parameter at all, Nginx will search for a new matching location based on the results of the rewrite.