Nginx 允许用户定义 Location block ,并指定一个匹配模式(pattern)匹配特定的 URI。除了简单的字符串(比如文件系统路径),还允许使用更为复杂的匹配模式(pattern)。

Location block 的基本语法形式是:

    location [=|~|~*|^~|@] pattern { ... }       #pattern 模式的意思

[=|~|~*|^~|@] 被称作 location modifier(位置修改器) ,这会定义 Nginx 如何去匹配其后的 pattern ,以及该 pattern 的最基本的属性(简单字符串或正则表达式)。


location modifier详解

=      #进行普通字符精确匹配

^~     #^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录

~      #波浪线表示执行一个正则匹配,区分大小写

~*     #表示执行一个正则匹配,不区分大小写

!~     #区分大小写不匹配 

!~*    #不区分大小写不匹配

/      #通用匹配,任何请求都会匹配到

@     #"@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files


location 匹配的优先级(与location在配置文件中的顺序无关)

= 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。

普通字符匹配。如果该项匹配还会去看有没有正则表达式匹配和更长的匹配。

^~ 只要匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。

最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;

当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。


优先级可以按如下顺序排列:

(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (location /)


1

= 会完全匹配指定的 pattern ,且这里的 pattern 被限制成简单的字符串,也就是说这里不能使用正则表达式。

Example:

server {

    server_name website.com;

    location = /abcd {                   #注意,这里有一个=号

    […]

    }

}

匹配情况:

    http://website.com/abcd        # 正好完全匹配

    http://website.com/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配

    http://website.com/abcd?param1?m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2

    http://website.com/abcd/       # 不匹配,因为末尾存在反斜杠(trailing slash),Nginx 不认为这种情况是完全匹配

    http://website.com/abcde       # 不匹配,因为不是完全匹配


2   

(None)可以不写 location modifier。 Nginx 仍然能去匹配 pattern 。这种情况下,匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,

不能使用正则表达式。

Example:

server {

    server_name website.com;

    location /abcd {              #会发现没有[=|~|~*|^~|@]等location modifier符号            

    […]

    }

}

匹配情况:

    http://website.com/abcd        # 正好完全匹配

    http://website.com/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配

    http://website.com/abcd?param1?m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2

    http://website.com/abcd/       # 末尾存在反斜杠(trailing slash)也属于匹配范围内

    http://website.com/abcde       # 仍然匹配,因为 URI 是以 pattern 开头的

    

    

3

~ 这个 location modifier 对大小写敏感,且 pattern 须是正则表达式

Example:

server {

    server_name website.com;

    location ~ ^/abcd$ {

    […]

    }

}

匹配情况:

    http://website.com/abcd        # 完全匹配

    http://website.com/ABCD        # 不匹配,~ 对大小写是敏感的

    http://website.com/abcd?param1?m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2

    http://website.com/abcd/       # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$

    http://website.com/abcde       # 不匹配正则表达式 ^/abcd$

    

4

~*这个 location modifier 不区分大小写,pattern 须是正则表达式

Example:

server {

    server_name website.com;

    location ~* ^/abcd$ {

    […]

    }

}

匹配情况:

    http://website.com/abcd        # 完全匹配

    http://website.com/ABCD        # 匹配,这就是它不区分大小写的特性

    http://website.com/abcd?param1?m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2

    http://website.com/abcd/       # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$

    http://website.com/abcde       # 不匹配正则表达式 ^/abcd$

  

5    

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


6. 

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


注:

当一台服务器上有多个虚拟主机时,以IP地址的形式去访问时,会按照字母顺序匹配最靠前的一个虚拟主机。