001-nginx基础配置-location

一、基础语法

Location block 的基本语法形式是:

    location [=|~|~*|^~|@] pattern { ... }

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

二、基本示例

在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。

示例1、

location ^~ /static_js/ 
{ 
proxy_cache js_cache; 
proxy_set_header Host js.test.com; 
proxy_pass http://js.test.com/; 
}

如上面的配置,如果请求的url是http://servername/static_js/test.html
会被代理成:http://js.test.com/test.html

示例2、

location ^~ /static_js/ 
{ 
proxy_cache js_cache; 
proxy_set_header Host js.test.com; 
proxy_pass http://js.test.com; 
}

如上面的配置,如果请求的url是http://servername/static_js/test.html

则会被代理到http://js.test.com/static_js/test.htm

示例3、可以用如下的rewrite来实现/的功能

location ^~ /static_js/ 
{ 
proxy_cache js_cache; 
proxy_set_header Host js.test.com; 
rewrite /static_js/(.+)$ /$1 break; 
proxy_pass http://js.test.com; 

} 

三、要点

3.1、location匹配顺序

  1. "="前缀指令匹配,如果匹配成功,则停止其他匹配
  2. 普通字符串指令匹配,顺序是从长到短,匹配成功的location如果使用^~,则停止其他匹配(正则匹配)
  3. 正则表达式指令匹配,按照配置文件里的顺序,成功就停止其他匹配
  4. 如果第三步中有匹配成功,则使用该结果,否则使用第二步结果

3.2、注意点

  1. 匹配的顺序是先匹配普通字符串,然后再匹配正则表达式。另外普通字符串匹配顺序是根据配置中字符长度从长到短,也就是说使用普通字符串配置的location顺序是无关紧要的,反正最后nginx会根据配置的长短来进行匹配,但是需要注意的是正则表达式按照配置文件里的顺序测试。找到第一个比配的正则表达式将停止搜索。

    扫描二维码关注公众号,回复: 2667409 查看本文章
  2. 一般情况下,匹配成功了普通字符串location后还会进行正则表达式location匹配。有两种方法改变这种行为,其一就是使用“=”前缀,这时执行的是严格匹配,并且匹配成功后立即停止其他匹配,同时处理这个请求;另外一种就是使用“^~”前缀,如果把这个前缀用于一个常规字符串那么告诉nginx 如果路径匹配那么不测试正则表达式。

3.3、匹配模式及顺序location modifier

  location = /uri    =开头表示精确匹配,只有完全匹配上才能生效。

  location ^~ /uri   ^~ 开头对URL路径进行前缀匹配,并且在正则之前。

  location ~ pattern  ~开头表示区分大小写的正则匹配。

  location ~* pattern  ~*开头表示不区分大小写的正则匹配。

  location /uri     不带任何修饰符,也表示前缀匹配,但是在正则匹配之后。

  location /      通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default。 

3.3.1、【=】精确匹配

server {
    server_name website.com;
    location = /abcd {
    […]
    }
}

匹配情况:

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

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

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

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

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

3.3.2、【none】不写location modifier

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

server {
    server_name website.com;
    location /abcd {
    […]
    }
}

匹配情况:

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

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

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

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

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

3.3.3、【~】对大小写敏感,且 pattern 须是正则表达式

server {
    server_name website.com;
    location ~ ^/abcd$ {
    […]
    }
}

匹配情况:

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

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

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

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

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

注意:对于一些对大小写不敏感的系统,比如 Windows ,~ 和 ~* 都是不起作用的,这主要是操作系统的原因。

3.3.4、【~*】与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式

server {
    server_name website.com;
    location ~* ^/abcd$ {
    […]
    }
}

匹配情况:

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

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

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

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

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

3.3.5、【^~】

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

3.3.6、【@】

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

3.4、搜索顺序以及生效优先级

写在配置文件中每个 Server 块中的 Location 块的次序是不重要的,Nginx 会按 location modifier 的优先级来依次用 URI 去匹配 pattern ,顺序如下:

    1. =

    2. (None)    如果 pattern 完全匹配 URI(不是只匹配 URI 的头部)

    3. ^~

    4. ~ 或 ~*

    5. (None)    pattern 匹配 URI 的头部

3.5、匹配案例

location  = / {

  # 精确匹配 / ,主机名后面不能带任何字符串

  [ configuration A ] 

}

location  / {

  # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求

  # 但是正则和最长字符串会优先匹配

  [ configuration B ] 

}

location /documents/ {

  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索

  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条

  [ configuration C ] 

}

location ~ /documents/Abc {

  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索

  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条

  [ configuration CC ] 

}

location ^~ /images/ {

  # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。

  [ configuration D ] 

}

location ~* \.(gif|jpg|jpeg)$ {

  # 匹配所有以 gif,jpg或jpeg 结尾的请求

  # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则

  [ configuration E ] 

}

location /images/ {

  # 字符匹配到 /images/,继续往下,会发现 ^~ 存在

  [ configuration F ] 

}

location /images/abc {

  # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在

  # F与G的放置顺序是没有关系的

  [ configuration G ] 

}

location ~ /images/abc/ {

  # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用

    [ configuration H ] 

}

location ~* /js/.*/\.js

猜你喜欢

转载自www.cnblogs.com/bjlhx/p/9453294.html
今日推荐