Nginx location in the configuration, and matching rules

A, location syntax

location [ = | 空格 | ~ | ~* | ^~ ] uri { 
    ... 
}
  • Precision match: =
  • String match: Space
  • Than the regular matching: ^ ~
  • Regular matches:!! ~ | ~ * | ~ | ~ *
  • General match: /

    Where "~" represents - Regular case-sensitive match; "~ *" represents - Regular case-insensitive match; together represents - does not match the "!"

Second, the location of the plurality of matching order

In order to match the location nginx

    Step: precise match match with "=", if the match, directly returns;

    Step Two: Match with "space" string matching, if the match is to save, not directly return;

    The third step: with matching "^ ~" is better than the regular matching, if the characters match, the process directly returns;

    Step four: Match with "!! ~ | ~ * | ~ | ~ *" Regular matches, if the match, and then directly back; if it does not match, but the match on the second step, the second direct return the matching step;

    Step five: General match only match "/", this is an arbitrary access path can be matched directly returned.

    Note:

    1, at the same level matching rule, in accordance with the order determined profile matches, the matching returns immediately (not directly return the string matching);

    2, for string matching, always scratch to match the longest string of a record that.

Third, the example shows

    Nginx.cof modify the file must first verify that the profile is configured successfully, restart! ! !

    Nginx has written some commonly used commands are interested can look welcome refers to the wrong Another blog. Attach Bowen Address: Nginx explain the installation of windows, run, and configuration files

# 精准匹配
location = /public/index.html {
    default_type text/html ;
    return 601 '==============================    精准匹配601【location = /public/index.html】    ==============================';
}

# 字符串匹配
location  /public/index.html {
    default_type text/html ;
    return 611 '==============================    字符串匹配611【location  /public/index.html】    ==============================';
}
location  /public/login.html {
    default_type text/html ;
    return 612 '==============================    字符串匹配612【location  /public/login.html】    ==============================';
}
location  /public/ {
    default_type text/html ;
    return 613 '==============================    字符串匹配613【location  /public/】    ==============================';
}
location  /public/second.html {
    default_type text/html ;
    return 614 '==============================    字符串匹配614【location  /public/second.html】    ==============================';
}

# 优于正则匹配
# 优于正则匹配中不能写与字符串匹配相同的访问路径,没有意义。
# location ^~ /public/index.html {
    # default_type text/html ;
    # return 621 '==============================    优于正则匹配621【location ^~ /public/index.html】    ==============================';
# }
location ^~ /public/regist.html {
    default_type text/html ;
    return 623 '==============================    优于正则匹配623【location ^~ /public/regist.html】    ==============================';
}
location ^~ /public/first.html {
    default_type text/html ;
    return 623 '==============================    优于正则匹配623_1【location ^~ /public/first.html】    ==============================';
}

# 正则匹配
location ~ \/public\/index\.html$ {
    default_type text/html ;
    return 631 '==============================    正则匹配631【location ~ \/public\/index\.html】    ==============================';
}
location ~ \/public\/first\.html$ {
    default_type text/html ;
    return 633 '==============================    正则匹配633_1【location ~ \/public\/first\.html】    ==============================';
}
location ~ \/public\/second\.html$ {
    default_type text/html ;
    return 634 '==============================    正则匹配634【location ~ \/public\/second\.html】    ==============================';
}

# 通用匹配
location / {
    default_type text/html ;
    return 640 '==============================    通用匹配640【location /】    ==============================';
}

      1 [request] precise matching address: HTTP: // localhost: 5021 / public / index.html

     2, [] matches the character string request address: HTTP: // localhost: 5021 / public / the login.html

    Note: it actually is the result of "string match" will return scratchpad, and then through "better than a regular match" and "regular match", no match is found successful, staging final return results.

 

     3, [] is superior to the regular request address match:  HTTP: // localhost: 5021 / public / regist.html

    Note: The fact is the result of "string matching [ LOCATION / public / ]" will return scratchpad, and then through "better than a regular match", a match is found successful, the final return "better than a regular match" result.

    3-1, [] is superior to the regular request address match:  HTTP: // localhost: 5021 / public / first.html

    4, [is] matching the requested address:  HTTP: // localhost: 5021 / public / second.html

    Note: The fact is the result of "string matching [ LOCATION /public/second.html ]" will return scratchpad, and then through "better than a regular match," no match is found successful, then through "regular match" match is found success eventually return to "regular match" result.

     5, [universal] request address match: HTTP: // localhost: 5021 / static / common.html

    Note: After all the rules after the match, no match is successful, then enter the "universal match", all requests are successful match.

Published 47 original articles · won praise 16 · views 70000 +

Guess you like

Origin blog.csdn.net/zorro_jin/article/details/85003947