Techyou labs
文章RSS
评论RSS
登录
真正的爱应该超越生命的长度、心灵的宽度、 灵魂的深度
搜索
关于作者
文章分类
Default
Linux/Unix
Database
Cloud
Networking
Security
Programming
最新文章
商城优惠券设计
Pulumi 新一代基础设施管理工具
装机神器-u盘启动手术刀Ventoy
使用 mergefs 创建虚拟挂载点
使用minio存储docker容器数据
Linux Mint 19/Ubuntu18 多点触摸和手势
Mint 19.2升级后若干问题
poetry, python依赖管理新军刀
docker 中运行 openwrt
Inxi:一把获取 Linux 系统信息的军刀
最新评论
renothing: 二次反向代理跟你应用程序得处理时间有关系吧?尤其是...
二次反向代理性能很差,怎么优化的?: 我也用nginx 做了个二次反向代理,但是并发连3...
hostyep: 交换链接么?目前每天保持30个左右对口IP,每月都...
yzhkpli: error while loading share...
美肤宝: 感谢分享。。。
lq: 嗯 喜欢弄得点单点
按月归档
February 2021
September 2020
May 2020
September 2019
August 2019
July 2019
June 2019
May 2019
January 2019
December 2018
November 2018
October 2018
September 2018
August 2018
July 2018
June 2018
April 2018
March 2018
December 2017
October 2017
September 2017
August 2017
April 2017
March 2017
February 2017
August 2016
July 2015
November 2014
September 2014
August 2014
July 2014
June 2014
July 2013
April 2013
September 2012
July 2012
May 2012
April 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
June 2009
May 2009
April 2009
March 2009
February 2009
December 2008
November 2008
September 2008
August 2008
July 2008
June 2008
常用标签
Mysql
nginx
mysql优化
linux
apache
Powered by
Typecho)))
Optimized by
EAimTY
nginx location匹配规则详解
July 21, 2013
#####Nginx location 配置语法 ``` Syntax: location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } Default: — Context: server, location ``` ``` 1. location [ = | ~ | ~* | ^~ ] uri { ... } 2. location @name { ... } ``` location 配置可以有两种配置方法 > 1.前缀 + uri(字符串/正则表达式) 2.@ + name 前缀含义 > = :精确匹配(必须全部相等) ~ :大小写敏感 ~* :忽略大小写 ^~ :只需匹配uri部分 @ :内部服务跳转 Location 基础知识 1.location 是在 server 块中配置。 2.可以根据不同的 URI 使用不同的配置(location 中配置),来处理不同的请求。 3.location 是有顺序的,会被第一个匹配的location 处理。 #####Location 配置 1.=,精确匹配 ``` location = / { #规则 } # 则匹配到 `http://www.example.com/` 。 ``` 2.~,大小写敏感 ``` location ~ /Example/ { #规则 } #请求示例 #http://www.example.com/Example/ [成功] #http://www.example.com/example/ [失败] ``` 3.~*,大小写忽略 ``` location ~* /Example/ { #规则 } # 则会忽略 uri 部分的大小写 #http://www.example.com/Example/ [成功] #http://www.example.com/example/ [成功] ``` 4.^~,只匹配以 uri 开头 ``` location ^~ /img/ { #规则 } #以 /img/ 开头的请求,都会匹配上 #http://www.example.com/img/a.jpg [成功] #http://www.example.com/img/b.mp4 [成功] ``` 5.@,nginx内部跳转 ``` location /img/ { error_page 404 @img_err; } location @img_err { # 规则 } #以 /img/ 开头的请求,如果链接的状态为 404。则会匹配到 @img_err 这条规则上。 ``` #####匹配优先级 1:前缀匹配和精确匹配优先级最高,取最长匹配。 2:正则匹配顺序为nginx配置文件里location书写顺序,无相 应匹配规则时,以最早一次前缀匹配为准。 3:@匹配通常用于重定向,无单独匹配规则,无法被嵌套,本身也不能包含嵌套。 4:location可嵌套。 5:nginx先检查前缀字符串,然后检查正则表达式,如果前缀匹配匹配到了,并且前缀匹配有“^~” 修饰符,就不配正则了;如果没有“^~” ,即使前缀匹配到了,也要去匹配正则表则,如果正则表达式匹配到了,就是用正则表达式的,没有就是用前缀字符串匹配到的路径; 6: #####参考例子 ``` location = / { [ configuration A ] } location / { [ configuration B ] } location /documents/ { [ configuration C ] } location ^~ /images/ { [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] } ``` 上述匹配顺序如下: 1: “/” 匹配规则 A, 2:“/index.html” 匹配规则B, 3:“/documents/document.html” 匹配规则C, 4:“/images/1.gif” 匹配规则D, 5:“/documents/1.jpg”匹配规则E。 > 顺序A>E>C>D>C>B ######有正则匹配,无符号修饰 规则:无“^~”, 最终使用正则匹配到的路径,正则匹配不到,使用字符串前缀匹配最长的 ``` xxxx/ ├── image1 │ └── test.txt ├── img │ └── test.txt ├── img2 ├── t1.txt │ └── test.txt └── test.txt ``` nginx规则如下: ``` #规则A location / { root /var/www; } #规则B location /img { root /var/www/img; } #规则C location ^~ /img/ { root /var/www/img2; } #规则D location ~* \/img\/ { root /var/www/image1; } #规则E location ~* \.txt { root /var/www/image1; } ``` 验证: http://localhost/image1/xxx 匹配规则A http://localhost/img 匹配规则B http://localhost/img/xxx 匹配规则C http://localhost/img/xxx/t.txt 匹配规则B > txt匹配规则 http://localhost/img1/xxx/t.txt http://localhost/img1/xxx/t.txt?xxxx=aaa http://localhost/img.txt 匹配规则E http://localhost/test.txt 匹配规则A http://localhost/img/test1.txt 匹配规则C > 当规则E不存在时 http://localhost/test.txt http://localhost/test1.txt http://localhost/t1.txt/test.txt 匹配规则F #####参考: http://nginx.org/en/docs/http/ngx_http_core_module.html#location https://segmentfault.com/a/1190000009237425 https://blog.csdn.net/truong/article/details/49967393
暂无评论
添加新评论
称呼
Email
网站
取消回复
内容
发表评论