概述

location 指令可以用在虚拟服务器 server 部分,并且意味着提供来自客户端的 URI 或者内部重定向访问。

location 的定义如下:

location [modifier] uri {...}
或者是命名 location
location @name {...}

命名 location 仅对内部访问重定向,在进入一个 location 之前,它会保留请求的 URI 部分。命名 location 只能够在 server 级别定义。

location 修饰符类型

= 修饰符

该修饰符使用精确匹配并且终止搜索。

精确匹配 /test 路径。

location = /test {
echo "matching";
}

注:这里的 echo 需要安装 echo-nginx-module 模块。可以参考文章: https://www.cnblogs.com/yxhblogs/p/12901575.html

测试一:完全匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test
matching

测试二:路径大写不匹配(如果操作的文件系统大小写不敏感,也会匹配)。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/TEST
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.9.9</center>
</body>
</html>

测试三:忽略路径上的参数,会匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea
matching

测试四:带 / 结尾的,不会匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.9.9</center>
</body>
</html>

测试五:路径不一致,不匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test1
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.9.9</center>
</body>
</html>

~ 修饰符

该修饰符使用区分大小写的正则表达式匹配

表示必须以 / 开始,以 $ 结束,中间是 test。

location ~ ^/test$ {
echo "matching";
}

测试一:匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test
matching

测试二:不匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/TEST
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.9.9</center>
</body>
</html>

测试三:忽略路径上的参数,会匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea
matching

测试四:不匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.9.9</center>
</body>
</html>

测试五:不匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test1
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.9.9</center>
</body>
</html>

~* 修饰符

该修饰符使用不区分大小写的正则表达式匹配

不区分大小写 test 路径

location ~* ^/test$ {
echo "matching";
}

测试一:匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test
matching

测试二:匹配,大小写不敏感。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/TEST
matching

测试三:忽略路径上的参数,会匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea
matching

测试四:不匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.9.9</center>
</body>
</html>

测试五:不匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test1
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.9.9</center>
</body>
</html>

^~ 修饰符

如果该 location 是最佳的匹配,那么对于匹配这个 location 的字符串,该修饰符不再进行正则表达式检测。注意:这不是一个正则表达式匹配,它的目的是优先于正则表达式的匹配。

location ^~ /test {
echo "matching";
}

测试一:匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test
matching

测试二:不匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/TEST
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.9.9</center>
</body>
</html>

测试三:匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea
matching

测试四:匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test/
matching

测试五:匹配。

ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test1
matching

Nginx 修饰符 Location 详解的更多相关文章

  1. nginx入门与实战 安装 启动 配置nginx Nginx状态信息(status)配置 正向代理 反向代理 nginx语法之location详解

    nginx入门与实战 网站服务 想必我们大多数人都是通过访问网站而开始接触互联网的吧.我们平时访问的网站服务 就是 Web 网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务. Web ...

  2. nginx语法之location详解

    Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 ^~ 以某个字符串开头 ~ 区分大小写的正则匹配 ~* 不区分大小写的正则匹配 !~ 区分大小写不匹配的正则 !~* 不区分大小 ...

  3. Java中访问控制修饰符的详解和示例——Java学习

    Java中的四个访问控制修饰符 简述 在Java中共有四个: public -- 对外部完全可见 protected -- 对本包和所有子类可见 默认(不需要修饰符)-- 对本包可见 private ...

  4. Java 访问修饰符大全详解

    鉴于笔试面试总会遇到,决心仔细认真梳理一下: 1:涉及的关键字:public,default(表示缺省),protected,private,static,final,abstract. 2:关键字含 ...

  5. java 包 修饰符 权限详解

    作用域   当前类    同package   子孙类 其他package  public √   √  √ √  protected √ √ √ ×  friendly(default) √ √ × ...

  6. 06 nginx Location详解之精准匹配

    一:Location详解之精准匹配 location 语法 location 有”定位”的意思, 根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分, ...

  7. Nginx 配置及参数详解

    Nginx 配置及参数详解 Nginx Location 指令语法 如下就是常用的 location 配置的语法格式,其中modifier是可选的,location_match就是制定 URI 应该去 ...

  8. Nginx 主配置文件参数详解

    Nginx 主配置文件参数详解 Nginx 安装完毕后,会有响应的安装目录,安装目录里 nginx.conf 为 nginx 的主配置文件, ginx 主配置文件分为 4 部分,main(全局配置). ...

  9. [转帖]Nginx rewrite模块深入浅出详解

    Nginx rewrite模块深入浅出详解 https://www.cnblogs.com/beyang/p/7832460.html rewrite模块(ngx_http_rewrite_modul ...

  10. 2-4、nginx特性及基础概念-nginx web服务配置详解

    Nginx Nginx:engine X 调用了libevent:高性能的网络库 epoll():基于事件驱动event的网络库文件 Nginx的特性: 模块化设计.较好扩展性(不支持模块动态装卸载, ...

随机推荐

  1. HarmonyOS实践之应用状态变量共享

      平时在开发的过程中,我们会在应用中共享数据,在不同的页面间共享信息.虽然常用的共享信息,也可以通过不同页面中组件间信息共享的方式,但有时使用应用级别的状态管理会让开发工作变得简单. 根据不同的使用 ...

  2. 第二十篇:cookie和session

    一.Cookie是什么鬼 二.基于cookie实现用户登录 三.基于cookie实现定制显示数据条数 四.带签名的cookie 五.CBV和FBV用户认证装饰器

  3. 重新点亮shell————awk 控制语句[十三]

    前言 简单介绍一下控制语句. 正文 例子1: 例子2: 例子3 for循环: 例子4, sum会复用: 同样,其他的while 和 do while 也是可以在awk中使用的. 结 下一节awk数组.

  4. .Net core 3.0 SignalR+Vue 实现简单的IM(无jq依赖)

    .Net core 中的SignalR JavaScript客户端已经不需要依赖Jquery了 一.服务端 1.nuget安装 Microsoft.AspNetCore.SignalR2.在start ...

  5. 浅析Golang map的实现原理

    Golang中的map底层使用的数据结构是hash table,基本原理就和基础的散列表一致,重点是Golang在设计中采用了分桶(Bucket),每个桶里面支持多个key-value元素的这种思路, ...

  6. 力扣283(java)-移动零(简单)

    题目: 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 请注意 ,必须在不复制数组的情况下原地对数组进行操作. 示例 1: 输入: nums = [0, ...

  7. 牛客网-SQL专项练习4

    ①向表evaluate的成绩列添加成绩,从表grade中的成绩一列提取记录,SQL语句为: INSERT INTO  evaluate(grade.point) SELECT grade.point  ...

  8. 力扣682(java)-棒球比赛(简单)

    题目: 你现在是一场采用特殊赛制棒球比赛的记录员.这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分. 比赛开始时,记录是空白的.你会得到一个记录操作的字符串列表 ops,其中 ops ...

  9. 客户端单元测试实践——C++篇

    简介: 我们团队在手淘中主要负责BehaviX模块,代码主要是一些逻辑功能,很少涉及到UI,为了减少双端不一致问题.提高性能,我们采用了将核心代码C++化的策略.由于团队项目偏底层,测试同学难以完全覆 ...

  10. [FAQ] 快速上手 Final Cut Pro X 的入门教程

    FinalCutPro视频剪辑 基本操作教学,看下面的视频作为一个大致了解.另外遇到其它问题再针对性搜索解决即可. > 在线CF靶场 射击消除烦闷 Link:https://www.cnblog ...