Created
March 20, 2018 14:54
-
-
Save mrunkel/fe497d35367d55387bbff97cd0ddd736 to your computer and use it in GitHub Desktop.
nginx location matching rules explained
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # READ (AND UNDERSTAND) THIS BEFORE MODIFYING THE location settings | |
| # location optional_modifier location_match { | |
| # .... | |
| # } | |
| # | |
| # The location_match in the above defines what Nginx should check the request URI against. The existence or | |
| # nonexistence of the optional_modifier in the above example affects the way that the Nginx attempts to match the | |
| # location block. The modifiers below will cause the associated location block to be interpreted as follows: | |
| # | |
| # * (none): If no modifiers are present, the location is interpreted as a prefix match. This means that the location | |
| # given will be matched against the beginning of the request URI to determine a match. | |
| # * =: 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. | |
| # | |
| # Nginx begins by checking all prefix-based location matches (all location types not involving a regular expression). | |
| # It checks each location against the complete request URI. | |
| # * First, Nginx looks for an exact match. If a location block using the = modifier is found to match the request URI | |
| # exactly, this location block is immediately selected to serve the request. | |
| # * If no exact (with the = modifier) location block matches are found, Nginx then moves on to evaluating non-exact | |
| # prefixes. It discovers the longest matching prefix location for the given request URI, which it then evaluates as | |
| # follows: | |
| # * If the longest matching prefix location has the ^~ modifier, then Nginx will immediately end its search and | |
| # select this location to serve the request. | |
| # * If the longest matching prefix location does not use the ^~ modifier, the match is stored by Nginx for the | |
| # moment so that the focus of the search can be shifted. | |
| # * After the longest matching prefix location is determined and stored, Nginx moves on to evaluating the regular | |
| # expression locations (both case sensitive and insensitive). Nginx tries the regular expression locations | |
| # sequentially. The first regular expression location that matches the request URI is immediately selected to | |
| # serve the request. | |
| # * If no regular expression locations are found that match the request URI, the previously stored prefix location | |
| # is selected to serve the request. | |
| # | |
| # It is important to understand that, by default, Nginx will serve regular expression matches in preference to prefix | |
| # matches. However, it evaluates prefix locations first, allowing for the administer to override this tendency by | |
| # specifying locations using the = and ^~ modifiers. | |
| # | |
| # It is also important to note that, while prefix locations generally select based on the longest, most specific | |
| # match, regular expression evaluation is stopped when the first matching location is found. This means that | |
| # positioning within the configuration has vast implications for regular expression locations. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment