SoFunction
Updated on 2025-04-14

Detailed explanation of path matching location rules in nginx

Nginx's path matching rules

Nginx matching ruleslocationInstructions to implement, NginxlocationInstructions are used to match the requested URI (request path) and execute specific processing instructions based on the matching result.locationIt is one of the core modules that realize functions such as dynamic and static separation, URL rewriting, and reverse proxy.

Location Matching Rule Classification

locationSupports multiple matching patterns, including exact matching, prefix matching, regular matching, etc.

Exact match (=)

The highest priority. When the requested URI islocationWhen the following string is exactly the same, Nginx will select thislocationBlocks are processed. (This is easy to understand, which means that the paths are exactly the same, with the highest priority)
Example

location = / {
    # Processing/request}

The above location block will be used only if the requested URI is strictly /.

Longest prefix matching (^~)

Second priority. The requested URI is onelocationThe following string starts, and this string is the longest (or used^~modifier), Nginx will select thislocationpiece. But please note,^~The modifier will actually stop subsequent regular match searches.
Example

   location ^~ /hello {
     return  601;
   }
    location ^~ /hellow {
     return  602;
   }
   location ^~ /hello/world {
     return  603;
   }
    location ~ /hello {
     return  604;
   }

For the request/hello, satisfying 601 and 604, and actually returning 601 is because ^~ priority is higher than regular match;
For the request/hellow, satisfy 601 and 602, and actually returns 602 because of the longest prefix matching principle;

Regular expression matching (~ and ~*)

Third priority. Regular expression matching allows for more complex URI matching patterns to be defined.~Represents a case-sensitive regular match, and~*Indicates a case-insensitive regular match.
Nginx will check regular expressions one by one in the order in the configuration filelocationblock until the first matching block is found. Therefore, the order of regular expressions is important.

Example:

location ~ \.(gif|jpg|png)$ {
    # Handle requests ending in .gif, .jpg or .png (case sensitive)}
location ~* \.(GIF|JPG|PNG)$ {
    # Handle requests ending in .GIF, .JPG, or .PNG (case insensitive)}

For the request/images/, the first onelocationThe block will be matched (if the request is case sensitive).

Normal prefix matching (no modifier)

Fourth priority. Normal prefix matching is also matched in the order of occurrence in the configuration file, the first one appearslocationDirective matching is preferred.
Example:

   location /hello {
     return  601;
   }
   location /hellow {
     return  602;
   }

For request/hellow, 601 and 602 are satisfied, and 602 is actually returned, proving that the principle of long prefix matching is satisfied

Default Match (/)

The lowest priority. If the requested URI is with any specificlocationBlocks do not match, Nginx will use the default onelocationblock (if any). Usually, the default location block is a without any modifier.location /piece.
Example:

location / {
    # Process all other requests}

Summarize

Nginx's location matching rule priority can be summarized as:

  • Exact match (=

  • Longest prefix matching (^~), but subsequent regular match searches will be stopped

  • Regular expression matching (~and~*), in configuration order

  • Normal prefix matching (no modifier), also in configuration order

  • Default match (/

Things to note

  • Regular Match and Order: The order of matching regular location is important because Nginx matches in the order defined in the configuration file.

  • Path replacement of proxy_pass: When configuring proxy_pass, you need to pay attention to whether the URI behind location contains a slash (/), which will affect the request's forwarding path. It is usually recommended that location and proxy_pass either add slashes or none.

  • Performance optimization: Putting exact matches in front can reduce unnecessary regular matches and improve Nginx processing efficiency.

This is the end of this article about the detailed explanation of the path matching location rules in nginx. For more related nginx path matching rules, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!