nigx配置location规则
以下是收集的,对于不是很熟的朋友,配置转发很有帮助
1、location匹配命令和优先级:
优先级:
- Directives with the = prefix that match the query exactly. If found, searching stops.
- All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
- Regular expressions, in order of definition in the configuration file.
- If #3 yielded a match, that result is used. Else the match from #2 is used.
- =前缀的指令严格匹配这个查询。如果找到,停止搜索。
- 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
- 正则表达式,在配置文件中定义的顺序。
- 如果第3条规则产生匹配的话,结果被使用。否则,使用第2条规则的结果。
匹配命令:
"="前缀指令匹配,如果匹配成功,则停止其他匹配
普通字符串指令匹配,后续还会匹配正则表达,^~ 也表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项
~ 正则表达式指令匹配,区分大小写
~* 正则表达式指令匹配,不区分大小写
/ 通用匹配,任何未匹配到其它location的请求都会匹配到
2、例子
1、测试"^~"和"~",nginx配置如下。浏览器输入http://localhost/helloworld/test,返回601。如将#1注释,#2打开,浏览器输入http://localhost/helloworld/test,返回603。注:#1和#2不能同时打开,如同时打开,启动nginx会报nginx: [emerg] duplicate location "/helloworld"...,因为这两个都是同一个字符串。
location ^~ /helloworld { #1
return 601;
}
#location /helloworld { #2
# return 602;
#}
location ~ /helloworld {
return 603;
}
2、测试普通字符串的长短(普通字符串的匹配与顺序无关,与长短有关)。浏览器输入http://localhost/helloworld/test/a.html,返回601。浏览器输入http://localhost/helloworld/a.html,返回602。
location /helloworld/test/ { #1
return 601;
}
location /helloworld/ { #2
return 602;
}
3、测试正则表达式的顺序(正则匹配与顺序相关)。浏览器输入http://localhost/helloworld/test/a.html,返回602;将#2和#3调换顺序,浏览器输入http://localhost/helloworld/test/a.html,返回603
location /helloworld/test/ { #1
return 601;
}
location ~ /helloworld { #2
return 602;
}
location ~ /helloworld/test { #3
return 603;
}
4、总结
location = / {
#规则A
}
location = /login {
#规则B
}
location ^~ /static/ {
#规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则D
}
location ~* \.png$ {
#规则E
}
location !~ \.xhtml$ {
#规则F
}
location !~* \.xhtml$ {
#规则G
}
location / {
#规则H
}
nigx配置location规则的更多相关文章
- Nginx 配置 Location 规则优先级问题
nginx 的 location与配置中 location 顺序没有关系,与 location 表达式的类型有关.相同类型的表达式,字符串长的会优先匹配. 以下是按优先级排列说明: 等号类型(=)的优 ...
- rewrite规则写法及nginx配置location总结
rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...
- nginx配置 location及rewrite规则详解
1. location正则写法 语法规则: location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url ...
- Nginx配置location及rewrite规则
Nginx配置location及rewrite规则 示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } loca ...
- nginx配置location总结及rewrite规则写法【转】
转自 nginx配置location总结及rewrite规则写法 | Sean's Noteshttp://seanlook.com/2015/05/17/nginx-location-rewrite ...
- nginx 反向代理 配置 https 实现http https同时存在 经测试 支持location 规则
server { listen ssl; #监听443端口 server_name www.app01.com; ssl on; #启用ssl加密 ssl_certificate /etc/cert/ ...
- 详细解析 nginx uri 如何匹配 location 规则
location 是 nginx 配置中出现最频繁的配置项,一个 uri 是如何与多个 location 进行匹配的? 在有多个 location 都匹配的情况下,如何决定使用哪一个 location ...
- Nginx正确配置Location
文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 之前已经讲过Nginx的基本配置,本篇文章主要对Nginx中Location指令的作用进行介绍.本篇文章主要对 ...
- Nginx 核心配置-location的登录账户认证实战篇
Nginx 核心配置-location的登录账户认证实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用ab命令模拟网站攻击 1>.安装httpd-tools工具 ...
随机推荐
- mysql六:mysql内置功能(视图、触发器、事务、存储过程、函数)
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 通过使用视图可以把查询过程中的 ...
- QBC(Query By Criteria) QBE (Query By Example)
QBC 参考:(Hibernate的QBC查询) //is empty and is not empty @Test public void testQBC(){ Session session = ...
- Oracle自定义行转列函数
--行转列自定义函数,只针对TABLE1表 --paramType是参数类型,用于判断,param1和param2是条件参数 create or replace function My_concat( ...
- Visual Studio 快捷键汇总
常见方法: 强迫智能感知:Ctrl+J.智能感知是Visual Studio最大的亮点之一,选择Visual Studio恐怕不会没有这个原因. 撤销:Ctrl+Z.除非你是天才,那么这个快捷键也是 ...
- SSM整合的简单实现
整合需要的jar包和源码将在文末给出 本文参考黑马程序员视频,由于视频用的环境和我使用的环境不同,建议使用我的环境及jar包(比较新) 一 整合思路 第一步 整合dao层 mybatis和spring ...
- Flask入门模板过滤器与测试器(五)
1 模板引擎之过滤器 概念 : 过滤器本质上是个转换函数,第一个参数是待过滤的变量.如果它有第二个参数,模板中就必须传进去. 过滤器使用管道符| 放在{{ }} Jinja2模板引擎提供了丰富的内置过 ...
- laravel + haproxy + https 后生成分页 url 非 https 解决办法
更合适的解决办法:在 AppServiceProvider boot 方法中使用 \URL::forceScheme('https'); 即可. 背景 近日对所有的客户都上线了 https ,本来在 ...
- 配置xtrabackup备份mysql数据库
下载地址:https://www.percona.com/downloads/XtraBackup/LATEST/ 为了方便起见本次安装使用yum源安装方式 1 安装yum源:yum insta ...
- oozie fork join结点
oozie可以用fork和join节点进行多任务并行处理,同时fork和join也是同时出现,缺一不可. 语法: <workflow-app name="[WF-DEF-NAME]&q ...
- python入门19 异常及异常处理 异常捕获
常见异常举例 """ 一些异常举例:""" '''模块不存在 ModuleNotFoundError: No module named 'd ...