Nginx中location的配置方法,以及匹配规则

一、location语法

location [ = | 空格 | ~ | ~* | ^~ ] uri { 
    ... 
}
  • 精准匹配: =
  • 字符串匹配: 空格
  • 优于正则匹配: ^~
  • 正则匹配: ~ | ~* | !~ | !~*
  • 通用匹配: /

    其中," ~ "表示-正则匹配且区分大小写;" ~ *"表示-正则匹配且不区分大小写;加上" ! "代表-不匹配

二、多个location的匹配顺序

nginx中location的匹配顺序

    第一步:匹配带 " = " 的精准匹配,若匹配上,则直接返回;

    第二步:匹配带 " 空格 " 的字符串匹配,若匹配上则先保存,不直接返回;

    第三步:匹配带 " ^~ "的优于正则匹配,若匹配上了,则直接返回;

    第四步:匹配带 " ~ | ~* | !~ | !~* "的正则匹配,若匹配上了,则直接返回;若没有匹配上,但是第二步匹配上了,则直接返回第二步匹配上的;

    第五步:匹配仅 " / " 的通用匹配,这个是任意访问路径都能匹配上的,直接返回。

    注:

    1、在同一级匹配规则下,按照在配置文件中的先后顺序匹配判断,匹配中了就立即返回(字符串匹配不直接返回);

    2、对于字符串匹配,总是暂存匹配字符串最长的那一个记录。

三、示例说明

    修改了nginx.cof文件一定要先验证配置文件是否配置成功,再启动!!!

    在另一篇博客中有写到Nginx一些常用命令,感兴趣可以看看,欢迎指错。附上博文地址:Nginx在windows下的安装、运行,以及配置文件讲解

# 精准匹配
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、【精准匹配】请求地址:http://localhost:5021/public/index.html

     2、【字符串匹配】请求地址:http://localhost:5021/public/login.html

    注:实际上是经过"字符串匹配"将返回结果暂存,再经过"优于正则匹配"和"正则匹配"之后,未发现匹配成功的,最终返回暂存结果。

 

     3、【优于正则匹配】请求地址: http://localhost:5021/public/regist.html

    注:实际上是经过"字符串匹配 [location  /public/] "将返回结果暂存,再经过"优于正则匹配",发现匹配成功的,最终返回"优于正则匹配"结果。

    3-1、【优于正则匹配】请求地址: http://localhost:5021/public/first.html

    4、【正则匹配】请求地址: http://localhost:5021/public/second.html

    注:实际上是经过"字符串匹配 [location  /public/second.html] "将返回结果暂存,再经过"优于正则匹配",未发现匹配成功的,再经过"正则匹配"发现匹配成功的,最终返回"正则匹配"结果。

     5、【通用匹配】请求地址:http://localhost:5021/static/common.html

    注:当经过所有的匹配规则之后,都没有匹配成功的,则进入"通用匹配",所有请求都能匹配成功。

发布了47 篇原创文章 · 获赞 16 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/zorro_jin/article/details/85003947