概述

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. openGauss支持国密SM3和SM4算法

    国密算法介绍 国密即国家密码局认定的国产密码算法,主要有 SM1,SM2,SM3,SM4.密钥长度和分组长度均为 128 位.针对银行客户对数据库安全能力的诉求以及提高产品安全竞争力的要求,进行数据库 ...

  2. Maven 必备技能:MAC 系统下 JDK和Maven 安装及环境变量配置详细讲解

    开发中难免因系统问题或者版本变更反复折腾JDK和Maven环境变量,干脆写个笔记备忘个,也方便小伙伴们节省时间. =================JDK安装与环境变量配置====== 1.官网下载j ...

  3. Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用服务的必需组件-Nacos 、Sentinel等

    概述 Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案.此项目包含开发分布式应用服务的必需组件. 方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发 ...

  4. CentOS GNOME桌面下安装截图工具gnome-screenshot

    CentOS GNOME桌面下安装截图工具gnome-screenshot 1.光盘安装 (1).把镜像光盘放进电脑 (2).切换到 Packages (3).[root@localhost Pack ...

  5. 重新点亮shell————awk函数[十五]

    前言 简单介绍一下awk函数. 正文 算术函数 字符串函数 自定义函数 例子: 结 awk就到这里了.

  6. 当年老夫手写的cookie

    前言 留来来只为了回忆,旧博客迁移. 正文 /** * Created by OC on 20xx/8/27. */ function setCookie(name,value,expires,pat ...

  7. 鸿蒙HarmonyOS实战-ArkUI组件(Tabs)

    一.Tabs Tabs组件是一种常见的用户界面(UI)组件,它是一个可以容纳多个选项卡的容器组件.每个选项卡通常包含一个面板和一个标签,用户可以通过点击标签来切换面板.Tabs组件通常用于展示多个相关 ...

  8. Swin Transformer安装记录(国内源,飞快)

    0. 设备环境 ubuntu--20.10 GPU--3080 cuda--11.0 torch--1.7.0 mmcv--1.3.8 mmdetection--2.11.0 所有的git的项目,都可 ...

  9. 力扣507(java)-完美数(简单)

    题目: 对于一个 正整数,如果它和除了它自身以外的所有 正因子 之和相等,我们称它为 「完美数」. 给定一个 整数 n, 如果是完美数,返回 true:否则返回 false. 示例 1: 输入:num ...

  10. 【阿里云采购季】3月采购完,IT运维躺赢一年2

    阿里云2020上云采购季正式上线啦!今年的采购季可以逛些啥?    采购季正式期时间: 3月2日-3月31日 在这段时间里,想买啥就买吧,别忘了把想买的产品加入购物车噢,特惠产品叠加购物车满减,更划算 ...