• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Nginx location匹配规则的方法示例

    1、语法

    location [=|~|~*|^~|@] /uri/ {
     ...
    }
    

    2、说明

    从上面的语法出发,可以了解到 location 可以区分为三个部分,接下来一个一个的研究一下。

    1) [=|~|~*|^~|@]

    上面定义了几个不同的符号,表示不同的匹配规则,那么先后顺序呢?

    1. = 前缀的指令严格匹配这个查询。如果找到,停止搜索;
    2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用 ^~ 前缀,搜索停止;
    3. 正则表达式,在配置文件中定义的顺序;
    4. 如果第 3 条规则产生匹配的话,结果被使用。否则,使用第 2 条规则的结果。

    测试示例1:

    location = /world {
     return 600;
    }
    
    location = /hello {
     return 600;
    }
    
    location ~ /hellowo {
     return 602;
    }
    
    location ^~ /hello {
     return 601;
    }
    
    
    - 请求 localhost/world 返回600
    - 请求 localhost/world2 localhost/test/world 返回其他
    - 请求 localhost/hello 返回600
    - 请求 localhost/hello/123 返回601
    - 请求 localhost/hellow 返回601
    - 请求 localhost/hellowo 返回601
    - 请求 localhost/test/hellowo 返回602
    - 请求 localhost/test/hello 返回其他

    因此可以知道:

    测试示例2:

    location ~ /hello {
     return 602;
    }
    
    location ~ /helloworld {
     return 601;
    }
    
    
    - 请求 localhost/world/helloworld 返回 602
    - 请求 localhost/helloworld 返回 602

    调整上面的顺序

    location ~ /helloworld {
     return 601;
    }
    
    location ~ /hello {
     return 602;
    }
    
    
    - 请求 localhost/helloworld 返回601
    - 请求 localhost/world/helloworld 返回601
    - 请求 localhost/helloWorld 返回602
    

    所以同时正则匹配时

    测试示例3:

    location ^~ /hello/ {
     return 601;
    }
    
    location /hello/world {
     return 602;
    }
    
    

    这种场景中,存在一个没有符合的路由规则,那么实际的测试是怎样呢?

    - http://localhost/hello/wor 返回601
    - http://localhost/hello/world 返回602
    - http://localhost/hello/world23 返回602
    - http://localhost/hello/world/123 返回602

    从上面的示例可以看出

    2) [uri]

    这里主要填的是需要匹配的 path 路径,根据前面的符号,这里可以填写精确到 path 路径,也可以填正则表达式,下面则主要针对正则进行说明

    路由转发

    请求 path 匹配只是第一步,匹配完成之后,如何将请求转发给其它的 web 服务呢?

    1、反向代理

    通常可见的一种使用姿势就是使用 nginx 代理请求,转发到内部的其它 web 服务上

    主要通过 prixy_pass 来实现

    location ^~ /webs {
     proxy_pass http://127.0.0.1:8080/webs;
    }
    

    上面规则的含义是,将所有以 webs 开头的请求,转发到 8080 端口的 web 服务上。

    上面是直接写死转发到一个 ip 上,如果是多个机器提供服务,可以这样配置

    ## 下面放在http的括号内,作为第一层
    upstream test.online {
     server 120.11.11.11:8080 weight=1;
     server 120.11.11.12:8080 weight=1;
    }
    
    location ^~ /webs {
     proxy_pass http://test.online;
     proxy_redirect default;
    }
    
    

    2、Rewrite 命令

    rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。

    rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用, 如

    http://jb51.net/a/we/index.php?id=1&u=str

    只对/a/we/index.php重写。

    语法: rewrite regex replacement [flag];

    示例:

    location ^~ /hexo {
     root '/Users/yihui/GitHub/';
    }
    
    location ~ /hello {
     rewrite ^(/hello).*$ /hexo/public/index.html last;
     return 603;
    }
    
    

    将hello开头的,全部转发到/hexo/public/index.html

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    上一篇:Nginx整合Kafka的方法示例
    下一篇:浅析docker-compose部署mysql无法访问的问题
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    Nginx location匹配规则的方法示例 Nginx,location,匹配,规则,的,