SoFunction
Updated on 2025-04-23

Detailed explanation of Nginx location matching patterns and rules

1. Environment

Nginx 1.19

2. Matching mode

In NginxlocationIn the instructions, the commonly used matching patterns are:

  • Precision mode(=)
  • Prefix pattern, does not continue to match regular(^~),
  • Prefix pattern, continue to match the regular,
  • Regular mode, case sensitive (~)
  • Regular mode, case insensitive (~*)

1. Accurate mode

location = /path {
    default_type text/html;
    return 200 'hello';
}

2. Prefix pattern (do not continue to match regular)

location ^~ /path {
    default_type text/html;
    return 200 'hello';
}

3. Prefix pattern (continue to match the regular)

location /path {
    default_type text/html;
    return 200 'hello';
}

4. Regular mode (case sensitive)

location ~ /path {
    default_type text/html;
    return 200 'hello';
}

5. Regular mode (case insensitive)

location ~* /path {
    default_type text/html;
    return 200 'hello';
}

Nginx will followPrecision mode -> Prefix pattern -> Regular modeto match the order.

The precision mode has the highest priority, and after matching, it will no longer be matched with other modes. After the prefix pattern is matched, it depends on the command configuration to decide whether to continue matching the regular pattern.

3. Things to note

1. Priority when hitting multiple regular modes

See example:

# Regular mode (case sensitive)location ~ /a {
    default_type text/html;
    return 200 '111';
}

# Regular mode (case sensitive)location ~ /a/b {
    default_type text/html;
    return 200 '222';
}

If visithttp://localhost/a/b, which one will hitlocation? The answer is the first one.

Because twolocationAll are regular modes (whether case sensitive or not),From the top, which one matches first will be responsible for handling

2. Priority when hitting multiple prefix modes

# Prefix pattern (continue to match regular)location /a {
    default_type text/html;
    return 200 '111';
}

# Prefix pattern (continue to match regular)location /a/b {
    default_type text/html;
    return 200 '222';
}
  • accesshttp://localhost/a, hit the first one;
  • accesshttp://localhost/a/b, hit the second one;
  • accesshttp://localhost/a/b/c, hit the second one;

Simply put, which onelocationThe matching string is the longest, which one will handle it, for examplehttp://localhost/a/b/cThe longest string that can be matched is/a/b, so by the secondlocationTo deal with it.

The prefix pattern (not continuing to match the regular) is the same matching rule.

3. Priority when hitting multiple different modes

# Prefix pattern (do not continue to match regular)location ^~ /a {
    default_type text/html;
    return 200 '111';
}

# Prefix pattern (continue to match regular)location /a/b {
    default_type text/html;
    return 200 '333';
}

# Regular mode (case sensitive)location ~ /a/b {
    default_type text/html;
    return 200 '222';
}

accesshttp://localhost/a/b, will hit the third onelocation

The first twolocationAll are prefix patterns, processed by the longest matching string (i.e. the second one). The second onelocationThere is no preventing the regular matching, so the third one continues to be matchedlocation(regular mode), so the last one is the third onelocationdeal with.

4. Both prefix patterns cannot exist at the same time

See example:

# Prefix pattern (do not continue to match regular)location ^~ /a {
    default_type text/html;
    return 200 '333';
}

# Prefix pattern (continue to match regular)location /a {
    default_type text/html;
    return 200 '444';
}

The above configuration Nginx will report an error becauseWith the same path, these two modes cannot coexist.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.