美文网首页
nginx 2 参数解析

nginx 2 参数解析

作者: jingqian_xi | 来源:发表于2018-01-28 23:27 被阅读0次

    上一篇文章介绍了安装nginx的过程,这篇文章来记录一下nginx中配置多个server时的匹配顺序。关于安装请查看安装nginx

    首先我们先看一下匹配的几个模式

    1. 精确匹配 (=)
    location = /static/index.html{
      root /home/a/xxx/static;
      rewrite "/(xxx/static/)(.*)/" "$2";
    }
    
    1. 普通匹配 (^~)
      备注:不写符号或者写^~都是普通的匹配
    location  /static/index.html{
      root /home/a/xxx/static;
      rewrite "/(xxx/static/)(.*)/" "$2";
    }
    location ^~ /static/index.html{
      root /home/a/xxx/static;
      rewrite "/(xxx/static/)(.*)/" "$2";
    }
    
    1. 正则匹配 (~)
    location ~ /static/index.html{
      root /home/a/xxx/static;
      rewrite "/(xxx/static/)(.*)/" "$2";
    }
    

    然后看一下location如何发挥作用的

    location.jpg

    匹配顺序
    = 优于 ^~ 优于 ~

    • 普通匹配顺序是无所谓的,最后使用匹配的是最长的那个
    • 正则匹配是要求顺序的,是从前往后依次匹配并返回的

    小注

    • 所有的location尽量使用正则方式
    • 尽量少用location/{} 然后使用if语句

    相关文章

      网友评论

          本文标题:nginx 2 参数解析

          本文链接:https://www.haomeiwen.com/subject/nghfgxtx.html