草庐IT

nginx的location指令(实战示例、匹配顺序、匹配冲突)

开发运维玄德公 2023-10-19 原文

1. 对url的匹配

1.1 默认匹配

  • 语法示例
    location /crow/ {
       return  501 "通用匹配\n";
    }

1.2 精确匹配( = )

  • 语法示例
    location = /crow/ {
       return  501 "精确匹配\n";
    }

1.3 正则,区分大小写 ( ~ )

  • 语法示例
    location ~ /crow/.*\.md {
       return  501 "正则表达式,区分大小写\n";
    }

1.4 正则表达式,不区分大小写 ( ~* )

  • 语法示例
    location ~* /crow/.*\.md {
       return  501 "正则表达式,不区分大小写\n";
    }

2. 匹配顺序

  1. 精确匹配(=
  2. 字串匹配(^~
  3. 正则匹配(~~*
  4. 默认匹配(

2.1 示例(精确匹配最高)

  • 配置文件内容:
server {
    listen    1840;
    root   /usr/share/nginx/html;

    location / {
        index  index.html index.php index.htm;
    }
    location /crow/  {
       return  501 "通用匹配\n";
    }
    location = /crow/test.md {
       return  501 "精确匹配\n";
    }
    location ~ /crow/.*\.md {
       return  501 "正则表达式,区分大小写\n";
    }
    location ~* /crow/.*\.md {
       return  501 "正则表达式,不区分大小写\n";
    }
    location ^~ /crow/test.md {
       return  501 "字串匹配\n";
    }

}
  • 访问测试
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
精确匹配

可见精确匹配被匹配到。

下边我们去掉精确匹配:

2.2 示例(字串匹配次之)

  • 配置文件内容:
server {
    listen    1840;
    root   /usr/share/nginx/html;

    location / {
        index  index.html index.php index.htm;
    }
    location /crow/  {
       return  501 "通用匹配\n";
    }
    #location = /crow/test.md {
    #   return  501 "精确匹配\n";
    #}
    location ~ /crow/.*\.md {
       return  501 "正则表达式,区分大小写\n";
    }
    location ~* /crow/.*\.md {
       return  501 "正则表达式,不区分大小写\n";
    }
    location ^~ /crow/test.md {
       return  501 "字串匹配\n";
    }

}
  • 访问测试

如下可见,还剩 字串匹配正则匹配通用匹配,结果匹配到了 字串匹配

[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
字串匹配

2.3 示例(正则匹间配高于通用匹配)

  • 配置文件
server {
    listen    1840;
    root   /usr/share/nginx/html;

    location / {
        index  index.html index.php index.htm;
    }
    location /crow/  {
       return  501 "通用匹配\n";
    }
    #location = /crow/test.md {
    #   return  501 "精确匹配\n";
    #}
    location ~ /crow/.*\.md {
       return  501 "正则表达式,区分大小写\n";
    }
    location ~* /crow/.*\.md {
       return  501 "正则表达式,不区分大小写\n";
    }
    #location ^~ /crow/test.md {
    #   return  501 "字串匹配\n";
    #}

}
  • 访问测试
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
正则表达式,区分大小写

2.4 示例(正则表达式间前边的为准)

上例中我们看到:~在前边,因此先匹配了 ~。如果我们把~~*换个位置

  • 配置文件
server {
    listen    1840;
    root   /usr/share/nginx/html;

    location / {
        index  index.html index.php index.htm;
    }
    location /crow/  {
       return  501 "通用匹配\n";
    }
    
    location ~* /crow/.*\.md {
       return  501 "正则表达式,不区分大小写\n";
    }
    location ~ /crow/.*\.md {
       return  501 "正则表达式,区分大小写\n";
    }
}
  • 访问测试
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
正则表达式,不区分大小写

2.5 示例(通用匹配兜底)

  • 配置文件

我们还是将所有匹配都写上

server {
    listen    1840;
    root   /usr/share/nginx/html;

    location / {
        index  index.html index.php index.htm;
    }
    location /crow/  {
       return  501 "通用匹配\n";
    }
    location = /crow/test.md {
       return  501 "精确匹配\n";
    }
    location ~ /crow/.*\.md {
       return  501 "正则表达式,区分大小写\n";
    }
    location ~* /crow/.*\.md {
       return  501 "正则表达式,不区分大小写\n";
    }
    location ^~ /crow/test.md {
       return  501 "字串匹配\n";
    }

}
  • 访问测试
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.txt
通用匹配

3. 匹配间的冲突

3.1 通用匹配 VS 字串匹配

通用匹配字串匹配相同时,启动报错

  • 配置文件
    location /crow/test.md {
       return  501 "通用匹配\n";
    }
    location ^~ /crow/test.md {
       return  501 "字串匹配\n";
    }
  • 启动报错如下:
nginx-crow-test | nginx: [emerg] duplicate location "/crow/test.md" in /etc/nginx/conf.d/default.conf:45

3.2 暂时没有发现 :)

3.1 很孤独,我又暂时没有发现 3.2 ,留个位置,欢迎大家补充。


有关nginx的location指令(实战示例、匹配顺序、匹配冲突)的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby 正则表达式 - 如何替换字符串中匹配项的第 n 个实例 - 2

    在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg

  3. ruby - 匹配未转义的平衡定界符对 - 2

    如何匹配未被反斜杠转义的平衡定界符对(其本身未被反斜杠转义)(无需考虑嵌套)?例如对于反引号,我试过了,但是转义的反引号没有像转义那样工作。regex=/(?!$1:"how\\"#expected"how\\`are"上面的正则表达式不考虑由反斜杠转义并位于反引号前面的反斜杠,但我愿意考虑。StackOverflow如何做到这一点?这样做的目的并不复杂。我有文档文本,其中包括内联代码的反引号,就像StackOverflow一样,我想在HTML文件中显示它,内联代码用一些spanMaterial装饰。不会有嵌套,但转义反引号或转义反斜杠可能出现在任何地方。

  4. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  5. ruby - 匹配大写字母并用后续字母填充,直到一定的字符串长度 - 2

    我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种

  6. ruby-on-rails - 我该怎么办 :remote location validation with CarrierWave? - 2

    我在我的Rails3示例应用程序上使用CarrierWave。我想验证远程位置上传,因此当用户提交无效URL(空白或非图像)时,我不会收到标准错误异常:CarrierWave::DownloadErrorinImageController#createtryingtodownloadafilewhichisnotservedoverHTTP这是我的模型:classPaintingtrue,:length=>{:minimum=>5,:maximum=>100}validates:image,:presence=>trueend这是我的Controller:classPaintingsC

  7. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  8. ruby - rbenv 安装 ruby​​ 校验和不匹配 osx - 2

    我已经在mountainlion上成功安装了rbenv和ruby​​build。运行rbenvinstall1.9.3-p392结束于:校验和不匹配:ruby-1.9.3-p392.tar.gz(文件已损坏)预期f689a7b61379f83cbbed3c7077d83859,得到1cfc2ff433dbe80f8ff1a9dba2fd5636它正在下载的文件看起来没问题,如果我使用curl手动下载文件,我会得到同样不正确的校验和。有没有人遇到过这个?他们是如何解决的? 最佳答案 tl:博士;使用浏览器从http://ftp.rub

  9. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  10. ruby - 正则表达式将非英文字母匹配为非单词字符 - 2

    @raw_array[i]=~/[\W]/非常简单的正则表达式。当我用一些非拉丁字母(具体来说是俄语)尝试时,条件是错误的。我能用它做什么? 最佳答案 @raw_array[i]=~/[\p{L}]/使用西里尔字符进行测试。引用:http://www.regular-expressions.info/unicode.html#prop 关于ruby-正则表达式将非英文字母匹配为非单词字符,我们在StackOverflow上找到一个类似的问题: https://

随机推荐