Nginx中location模块的详细配置(含示例)
题记
此前在配置Nginx location模块的时候玩出了一些bug,折腾了一段时间。后来网上也查阅了相关的资料,看着也比较混乱。周末有空想着好好整理一下location模块的配置,结合自己的亲手实验,总结了location模块的配置。
location模块配置
根据匹配特性大概可以分成以下几个部分(按优先级顺序)
最高优先级(=) 第二优先级(^~) 第三优先级(按照顺序匹配,*) 第四优先级(/)
1. 匹配即停止
=:表示精确匹配,要么就匹配上,要么就不会被匹配。如果匹配上了,就进入该location块,其他都不看。
~:表示优先匹配,如果按从上往下的顺序匹配到了该~后面的URL,那么就进入该location块,其他都不看。
2. 按顺序匹配
~:表示区分大小写的正则匹配,如果依照自上而下的顺序匹配上URL了,那就不会再继续寻找,即使用这个location块。
~*:表示不区分大小写的正则匹配,如果依照自上而下的顺序匹配上URL了,那就不会再继续寻找,即使用这个location块。
3. 通用匹配
/:表示任何请求都会被匹配到。
location使用举例
# 输入http://ip+port/images/1.p
# 此时显示的是'= /images/1.p',因为=匹配优先级最高
location = /images/1.p {
default_type 'text/plain';
echo '= /images/1.p';
}
location ^~ /images/1.p {
default_type 'text/plain';
echo ' /images/1.p';
}
# 输入http://ip+port/images/1.p
# 此时显示到的是'^~ /images/1.p',因为^~只要匹配到了就会停止匹配,哪怕后续的长度更长
location ^~ /images/ {
default_type 'text/plain';
echo '^~ /images/1.p';
}
location ~ /images/1.p {
default_type 'text/plain';
echo '~ /images/1.p';
}
# 输入http://ip+port/images/1.pxyzxyz
# 此时显示到的是'~ /images/',因为这是按照顺序匹配的,匹配到了后面的就不再匹配了
location ~ /images/ {
default_type 'text/plain';
echo '~ /images/';
}
location ~ /images/1 {
default_type 'text/plain';
echo '~ /images/1';
}
# 输入http://ip+port/images/ 显示'/',因为没有匹配到后面的URL,使用默认的/规则
# 输入http://ip+port/images/1xyzxyz 显示'~ /images/1',因为匹配到了后面的正则
location / {
default_type 'text/plain';
echo '/';
}
location ~ /images/1 {
default_type 'text/plain';
echo '~ /images/1';
}
# 输入http://ip+port/images/ 显示'/images/'
# 输入http://ip+port/images/1/ab 显示'/images/'
# 输入http://ip+port/images/1/abc 显示'/images/1/abc' 匹配上第一个location后,会继续向下匹配寻找,如果有更加完整的匹配,则会有下面的。如果没有,则使用当前的。
location /images/ {
default_type 'text/plain';
echo '/images/';
}
location /images/1/abc {
default_type 'text/plain';
echo '/images/1/abc';
}
注意事项
# 在使用“=”的时候会有意外情况,比方说下面这个例子。当输入'http://ip+port/'时,发现返回的状态码是304&404
# 原因在于Nginx发现请求的是个目录时,会重定向去请求'http://ip+port/index.html',此时返回码是304
# 而后Nginx收到了'http://ip+port/index.html'这个请求,发现没有location能够匹配,就返回404了
location = / {
default_type 'text/plain';
index index.html index.htm;
root /web/;
}
Nginx中location模块的详细配置(含示例)的更多相关文章
- nginx中Geoip_module模块的使用
nginx中Geoip_module模块的使用 .安装模块,nginx也是通过yum安装 yum install nginx-module-geoip -y # 可以看到模块的链接库文件 [root@ ...
- nginx 中location和root
nginx 中location和root,你确定真的明白他们关系? 2016-01-17 14:48 3774人阅读 评论(1) 收藏 举报 分类: linux(17) 版权声明:本文为博主原创文 ...
- Nginx 中 location 的匹配顺序
nginx中location的匹配模式有以下几种: 精确匹配:以=开头,只有完全匹配才能生效,例子location = /uri 非正则匹配:以^~开头,^表示非.~表示正则,例子location ^ ...
- 对nginx中location的认识
关于一些对location认识的误区 1.location的匹配顺序是“先匹配正则,在匹配普通”. location的匹配顺序其实是“先匹配普通,在匹配正则”.造成误解的原因是:正则匹配会覆盖普通匹配 ...
- nginx中ngx_http_core_module模块
http核⼼心模块指令:套接字相关的配置3.1 server{ }设置虚拟服务器器的配置Syntax: server { ... }Default: —Context: httpserver {lis ...
- nginx中location、rewrite用法总结
一.location用法总结 location可以把不同方式的请求,定位到不同的处理方式上. 1.location的用法 location ~* /js/.*/\.js 以 = 开头,表示精确匹配:如 ...
- nginx中location详解
Location block 的基本语法形式是: location [=|~|~*|^~|@] pattern { ... } [=|~|~*|^~|@] 被称作 location modifier ...
- nginx中location的顺序(优先级)及rewrite规则写法
一.location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所 ...
- (数据科学学习手札32)Python中re模块的详细介绍
一.简介 关于正则表达式,我在前一篇(数据科学学习手札31)中已经做了详细介绍,本篇将对Python中自带模块re的常用功能进行总结: re作为Python中专为正则表达式相关功能做出支持的模块,提供 ...
随机推荐
- 微信小程序之对象转化为数组
对象转成数组方式一(数组里面是一个个number类型的元素) let dictObject= { , , , , }; // 对象转成数组方式一 var createArr = [] for (let ...
- java书籍推荐转
http://blog.csdn.net/chaozhi_guo/article/details/51274634 一.<深入理解Java虚拟机:JVM高级特性与最佳实践> 如果你不满足于 ...
- Composer更新慢的终极解决方案-转
转自:http://blog.csdn.net/fishermanmax/article/details/51975692 Packagist 镜像 请各位使用本镜像的同学注意: 本镜像已经依照 co ...
- 【Leetcode_easy】812. Largest Triangle Area
problem 812. Largest Triangle Area solution: class Solution { public: double largestTriangleArea(vec ...
- jsplumb 流程图,常用功能配置记录
前言: jsplumb 有2个版本一个Toolkit Edition(付费版),另外一个就是Community Edition(社区版本).Toolkit Edition版本功能集成的比较丰富,社区版 ...
- SpringCloud入门01之基础知识
一.Spring Cloud 什么是spring cloud, 为什么要使用微服务架构? 参考度娘 Spring Cloud是一系列框架的有序集合, 它利用Spring Boot的开发便利性巧妙地简化 ...
- vba Excel连接数据库
PostgreSql: 第一步 在网上下载postres的驱动程序,之后安装,下载地址:https://www.devart.com/odbc/postgresql/download.html 第二步 ...
- python 文件读写操作(24)
以前的代码都是直接将数据输出到控制台,实际上我们也可以通过读/写文件的方式读取/输出到磁盘文件中,文件读写简称I/O操作.文件I/O操作一共分为四部分:打开(open)/读取(read)/写入(wri ...
- Clustering and Exploring Search Results using Timeline Constructions (paper2)
作者:Omar Alonso 会议:CIKM 2009 摘要: 截至目前(2009),通过提取文档中内嵌的时间信息来展现和聚类,这方面的工作并不多. 在这篇文章中,我们将提出一个“小插件”增添到现有的 ...
- 1、2 建立list(RestController),并postman测试
1.主义实体类id 为String 类型(有用) @Entity(name = "t_student") public class Student { @Id private ...