以下是收集的,对于不是很熟的朋友,配置转发很有帮助

1、location匹配命令和优先级:

优先级:

  1. Directives with the = prefix that match the query exactly. If found, searching stops.
  2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
  3. Regular expressions, in order of definition in the configuration file.
  4. If #3 yielded a match, that result is used. Else the match from #2 is used.
  1. =前缀的指令严格匹配这个查询。如果找到,停止搜索。
  2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
  3. 正则表达式,在配置文件中定义的顺序。
  4. 如果第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
}
访问根目录/, 比如http://localhost/ 将匹配规则A
访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
访问 http://localhost/static/a.html 将匹配规则C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到 规则C
访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

 

nigx配置location规则的更多相关文章

  1. Nginx 配置 Location 规则优先级问题

    nginx 的 location与配置中 location 顺序没有关系,与 location 表达式的类型有关.相同类型的表达式,字符串长的会优先匹配. 以下是按优先级排列说明: 等号类型(=)的优 ...

  2. rewrite规则写法及nginx配置location总结

    rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...

  3. nginx配置 location及rewrite规则详解

    1. location正则写法 语法规则: location [=|~|~*|^~] /uri/ { … } =    开头表示精确匹配 ^~  开头表示uri以某个常规字符串开头,理解为匹配 url ...

  4. Nginx配置location及rewrite规则

    Nginx配置location及rewrite规则 示例: location  = / {   # 精确匹配 / ,主机名后面不能带任何字符串   [ configuration A ] } loca ...

  5. nginx配置location总结及rewrite规则写法【转】

    转自 nginx配置location总结及rewrite规则写法 | Sean's Noteshttp://seanlook.com/2015/05/17/nginx-location-rewrite ...

  6. nginx 反向代理 配置 https 实现http https同时存在 经测试 支持location 规则

    server { listen ssl; #监听443端口 server_name www.app01.com; ssl on; #启用ssl加密 ssl_certificate /etc/cert/ ...

  7. 详细解析 nginx uri 如何匹配 location 规则

    location 是 nginx 配置中出现最频繁的配置项,一个 uri 是如何与多个 location 进行匹配的? 在有多个 location 都匹配的情况下,如何决定使用哪一个 location ...

  8. Nginx正确配置Location

    文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 之前已经讲过Nginx的基本配置,本篇文章主要对Nginx中Location指令的作用进行介绍.本篇文章主要对 ...

  9. Nginx 核心配置-location的登录账户认证实战篇

    Nginx 核心配置-location的登录账户认证实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用ab命令模拟网站攻击 1>.安装httpd-tools工具 ...

随机推荐

  1. 前端之CSS——属性和定位

    一.字体属性 1.font-size(字体大小) p { font-size: 14px; } font-size 属性可设置字体的尺寸. px:像素,稳定和精确 %:把 font-size 设置为基 ...

  2. cf567E. President and Roads(最短路计数)

    题意 题目链接 给出一张有向图,以及起点终点,判断每条边的状态: 是否一定在最短路上,是的话输出'YES' 如果不在最短路上,最少减去多少权值会使其在最短路上,如果减去后的权值\(< 1\),输 ...

  3. mac的svn

    http://xclient.info/s/cornerstone.html?t=c5242a66e53f1d866afe8c42aace2738c04ce9ee#versions 破解版的地址 打开 ...

  4. 移动端点击a链接出现蓝色背景问题解决

    a:link, a:active, a:visited, a:hover { background: none; -webkit-tap-highlight-color: rgba(0,0,0,0); ...

  5. SharePoint 2013 - Add-ins

    1. App Web & Host Web The special website to which the app is deployed is called an App Web. The ...

  6. 刚在虚拟机上装的Linux系统,ifconfig后IP地址怎么成了127.0.0.1了

    之前在虚拟机上装了Linux系统,用了一段时间后想删除了重新装一下,然而装完以后ifconfig后,出现的是 [root@localhost ~]# ifconfig lo Link encap:Lo ...

  7. 【Linux】Core dump故障分析

    引入: Q:如果一个程序运行3天后才会出错,这个时候难道需要我们一直用GDB调试程序3天吗? A:答案当然是否定的. 我们有更厉害的工具--Core dump 一.Coredump定义 Core Du ...

  8. Android 自定义圆形图表

    <com...SignChartView android:id="@+id/signChart" android:layout_width="265dp" ...

  9. html 表格边框的设置

    对于很多初学HTML的人来说,表格<table>是最常用的标签了,但对于表格边框的控制,很多初学者却不甚其解. 一般我们用表格的时候总会给它个border属性,比如:<table b ...

  10. 搭建 Spring 2.5.6 开发环境

    1.jar 包准备: spring 2.5.6 的 jar 包(链接: http://pan.baidu.com/s/1skVFfcx 密码: mbiz),如图: commons-logging-1. ...