Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1453071
  • 博文数量: 263
  • 博客积分: 10851
  • 博客等级: 上将
  • 技术积分: 2627
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-26 22:40
文章分类

全部博文(263)

文章存档

2013年(4)

2012年(25)

2011年(33)

2010年(50)

2009年(138)

2008年(13)

分类: LINUX

2012-02-23 16:37:20

转自: http://blog.chinaunix.net/space.php?uid=25196855&do=blog&id=108805

最近一段时间在学习Nginx,以前一直对Nginx的Location配置很头大,最近终于弄出点眉目。总结如下:
nginx配置文件,自下到上分为三种层次分明的结构:
 |    http block            the protocol level
 |    server block          the server level
 V    location block        the requested URI

Nginx允许用户定义Location block,并指定一个匹配模式(pattern)匹配特定的URI。 除了简单的字符串(比如文件系统路径),还允许使用更为复杂的匹配模式(pattern)。
Location block的基本语法形式是:
    location [=|~|~*|^~|@] pattern { ... }
[=|~|~*|^~|@]被称作location modifier,用来定义Nginx如何去匹配其后的pattern,以及该pattern的最基本的属性(简单字符串或正则表达式)。


-------关于location modifier -------
1. =
    完全匹配指定的pattern ,且这里的pattern被限制成简单的字符串,也就是说这里不能使用正则表达式。
Example:
server {
    server_name website.com;
    location = /abcd {
    […]
    }
}
匹配情况:
    # 正好完全匹配
    # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1¶m2
    # 不匹配,因为末尾存在反斜杠(trailing slash),Nginx 不认为这种情况是完全匹配
    # 不匹配,因为不是完全匹配

2. (None)
    可以不写location modifier ,Nginx仍然能去匹配pattern。这种情况下,匹配那些以指定的patern开头的URI,注意这里的URI只能是普通字符串,不能使用正则表达式。
Example:
server {
    server_name website.com;
    location /abcd {
    […]
    }
}
匹配情况:
    # 正好完全匹配
    # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1¶m2
    # 末尾存在反斜杠(trailing slash)也属于匹配范围内
    # 仍然匹配,因为 URI 是以 pattern 开头的

3. ~
    这个location modifier对大小写敏感,且pattern须是正则表达式
Example:
server {
    server_name website.com;
    location ~ ^/abcd$ {
    […]
    }
}
匹配情况:
    # 完全匹配
    # 不匹配,~ 对大小写是敏感的
    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1¶m2
    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
    # 不匹配正则表达式 ^/abcd$
注意:对于一些对大小写不敏感的系统,比如 Windows ,~ 和 ~* 都是不起作用的,这主要是操作系统的原因。

4. ~*
    与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式
Example:
server {
    server_name website.com;
    location ~* ^/abcd$ {
    […]
    }
}
匹配情况:
    # 完全匹配
    # 匹配,这就是它不区分大小写的特性
    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1¶m2
    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
    # 不匹配正则表达式 ^/abcd$

5. ^~
    匹配情况类似2.(None)的情况,以指定匹配模式开头的URI被匹配,不同的是,一旦匹配成功,那么 Nginx 就停止去寻找其他的 Location 块进行匹配了(与 Location 匹配顺序有关)

6. @
    用于定义一个 Location 块,且该块不能被外部 Client 所访问,只能被 Nginx 内部配置指令所访问,比如 try_files or error_page


------- 搜索顺序以及生效优先级 -------
    因为可以定义多个Location块,每个Location块可以有各自的 pattern 。因此就需要明白(不管是Nginx还是你),当Nginx收到一个请求时,它是如何去匹配 URI 并找到合适的 Location 的。
要注意的是,写在配置文件中每个Server 块中的Location 块的次序是不重要的,Nginx 会按location modifier的优先级来依次用 URI 去匹配 pattern ,顺序如下:
    1. =
    2. (None)    如果pattern完全匹配 URI(不是只匹配 URI 的头部)
    3. ^~
    4. ~ 或 ~*  (按照在配置文件中的位置, 匹配了即停止搜索)
    5. (None)    pattern 匹配 URI 的头部 (会找到最佳匹配)


----------------------------------------------------------------------------------
注:虽说是总结,其实基本上就是 Nginx HTTP Server 该部分的翻译……,原文见该书电子版的第四章p133-138

阅读(2983) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~