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工具 ...
随机推荐
- 2729:Blah数集
2729:Blah数集 查看 提交 统计 提问 总时间限制: 3000ms 内存限制: 65536kB 描述 大数学家高斯小时候偶然间发现一种有趣的自然数集合Blah,对于以a为基的集合Ba定义如下: ...
- 最新机动车行驶证模板PSD可编辑分层文件下载
机动车行驶证PSD模板下载地址: http://www.qijieworld.com/thread-1834752-1-1.html 模板为psd格式,内容可编辑修改,需使用 Photoshop CS ...
- 使用angular帮你实现拖拽
拖拽有多种写法,在这里就看一看angular版的拖拽. <!DOCTYPE html> <html ng-app="myApp"> <head> ...
- javascript实现数据结构:稀疏矩阵的十字链表存储表示
当矩阵的非零个数和位置在操作过程中变化大时,就不宜采用顺序存储结构来表示三元组的线性表.例如,在作“将矩阵B加到矩阵A上”的操作时,由于非零元的插入或删除将会引起A.data中元素的移动.为此,对这种 ...
- Linux中怎么从root用户切换到普通用户
su是在用户间切换,可以是从普通用户切换到root用户, test@ubuntu:~$ su Password: root@ubuntu:/home/test# 也可以是从root用户切换到普通用户 ...
- SqlServer数据库全角和半角互转的方法
---摘要: SqlServer数据库全角和半角互转的方法 CREATE FUNCTION f_Convert( @str NVARCHAR(4000), --要转换的字符串 @flag bit -- ...
- ArcGIS Enterprise 10.5.1 静默安装部署记录(Centos 7.2 minimal)- 5、安装Datastore
安装Datastore 解压datastore安装包,tar -xzvf ArcGIS_DataStore_Linux_1051_156441.tar.gz 切换到arcgis账户静默安装server ...
- 保存及读取keras模型参数
转自:http://blog.csdn.net/u010159842/article/details/54407745,感谢分享~ 你可以使用model.save(filepath)将Keras模型和 ...
- Dynamics CRM 之汇总字段
用插件汇总数据,速度很慢,导数据的时候更慢!那就用汇总字段- - 新建个汇总字段,字段类型选择汇总.点击编辑进入逻辑编辑 相关实体:对当前实体或者相关联的实体的字段值进行判断筛选. 筛选器:对相关实体 ...
- MSSQL的SQL语句独立执行消耗与线上执行消耗差异
环境: SQL Server 2012 疑问:同样的一条语句,使用Profile跟踪出来的消耗与单独拿出来执行的消耗存在非常大的差距 语句如下: declare @str nvarchar(max) ...