Nginx 修饰符 Location 详解
概述
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 详解的更多相关文章
- nginx入门与实战 安装 启动 配置nginx Nginx状态信息(status)配置 正向代理 反向代理 nginx语法之location详解
nginx入门与实战 网站服务 想必我们大多数人都是通过访问网站而开始接触互联网的吧.我们平时访问的网站服务 就是 Web 网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务. Web ...
- nginx语法之location详解
Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 ^~ 以某个字符串开头 ~ 区分大小写的正则匹配 ~* 不区分大小写的正则匹配 !~ 区分大小写不匹配的正则 !~* 不区分大小 ...
- Java中访问控制修饰符的详解和示例——Java学习
Java中的四个访问控制修饰符 简述 在Java中共有四个: public -- 对外部完全可见 protected -- 对本包和所有子类可见 默认(不需要修饰符)-- 对本包可见 private ...
- Java 访问修饰符大全详解
鉴于笔试面试总会遇到,决心仔细认真梳理一下: 1:涉及的关键字:public,default(表示缺省),protected,private,static,final,abstract. 2:关键字含 ...
- java 包 修饰符 权限详解
作用域 当前类 同package 子孙类 其他package public √ √ √ √ protected √ √ √ × friendly(default) √ √ × ...
- 06 nginx Location详解之精准匹配
一:Location详解之精准匹配 location 语法 location 有”定位”的意思, 根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分, ...
- Nginx 配置及参数详解
Nginx 配置及参数详解 Nginx Location 指令语法 如下就是常用的 location 配置的语法格式,其中modifier是可选的,location_match就是制定 URI 应该去 ...
- Nginx 主配置文件参数详解
Nginx 主配置文件参数详解 Nginx 安装完毕后,会有响应的安装目录,安装目录里 nginx.conf 为 nginx 的主配置文件, ginx 主配置文件分为 4 部分,main(全局配置). ...
- [转帖]Nginx rewrite模块深入浅出详解
Nginx rewrite模块深入浅出详解 https://www.cnblogs.com/beyang/p/7832460.html rewrite模块(ngx_http_rewrite_modul ...
- 2-4、nginx特性及基础概念-nginx web服务配置详解
Nginx Nginx:engine X 调用了libevent:高性能的网络库 epoll():基于事件驱动event的网络库文件 Nginx的特性: 模块化设计.较好扩展性(不支持模块动态装卸载, ...
随机推荐
- 异步、多线程、Java爬取某网站图片
一.网页图片爬取类 package com.yhyl.utils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import or ...
- cesiumjs GIS引擎源码编译并运行-2021年3月18日最新版【1.68~1.79.1版本亲测成功】
前言 本篇最初是在2020年的[macOS Big Sur + Cesium 1.76版本]下编译成功,后在[macOS Catalina+cesium 1.79.1版本]编译过程中,出现编译的错误和 ...
- c# 多线程传值注意的地方
前言 下面介绍多线程传值的几种方式,并说明注意点. 正文 static void Main(string[] args) { SampleTread thead = new SampleTread(1 ...
- mmcls/mmdet模型部署至 TorchServe
mmcls/mmdet模型部署至 TorchServe 官方教程:模型部署至 TorchServe - MMClassification 0.23.2 文档 接口说明: serve/inference ...
- MMDeploy部署实战系列【第五章】:Windows下Release x64编译mmdeploy(C++),对TensorRT模型进行推理
MMDeploy部署实战系列[第五章]:Windows下Release x64编译mmdeploy(C++),对TensorRT模型进行推理 这个系列是一个随笔,是我走过的一些路,有些地方可能不太完善 ...
- 操作流程分享:HDMI输入SIL9293C配套NR-9 2AR-18的国产GOWIN开发板
1. 产品概述 HDMI 输入配套板是用于 GOWIN NR-9 和 2AR-18 的开发板配套的 HDMI 输出板子.最 大分辨率支持 1920*1080P,60Hz 刷新率,24 位 RGB 色彩 ...
- 力扣521(java&python)-最长特殊序列Ⅰ(简单)
题目: 给你两个字符串 a 和 b,请返回 这两个字符串中 最长的特殊序列 的长度.如果不存在,则返回 -1 . 「最长特殊序列」 定义如下:该序列为 某字符串独有的最长子序列(即不能是其他字符串的 ...
- Quick BI产品核心功能大图(五)移动端:让数据在更多业务场景中流通
简介:将数据更好的融入日常工作中,一个重要的前提条件就是多端多渠道的数据触达和办公协同能力. Quick BI凭借移动端交互体验,帮助用户随时随地便捷查看报表,并通过在线协同方式,追踪策略的执行落地 ...
- 阿里巴巴 DevOps 工具体系
简介: 随着阿里巴巴多元化业务 20 多年的高速发展,技术体系经历了 web 时代.移动化时代.数据智能时代.云计算时代等多个重大变革.在这些变革中,开发者面对的技术体系.工具体系.知识体系也在不断 ...
- dotnet 使用 XWT 构建跨平台客户端 入门篇
本文告诉大家如何入门开始开发一个基于 mono 组织开源的 XWT 跨平台客户端 UI 框架的应用,本文的 xwt 是在 GitHub 上完全开源的,基于 MIT 协议的,底层采用 GTK# 的 UI ...