Nginx的location指令示例

在NGINX服务器块中的位置指令可以将请求路由到文件系统中的正确位置。该指令用于告诉NGINX在匹配位置块与URL时在哪里查找资源,包括文件和文件夹。在本教程中,我们将详细了解NGINX位置指令。

先决条件

  • You have already installed NGINX by following our tutorial from here.

nginx的location指令语法

NGINX的位置块可以放置在服务器块内或另一个位置块内,但有一些限制。构建位置块的语法如下:

location [modifier] [URI] {
  ...
  ...
}

在位置块中的修饰符是可选的。在位置块中有一个修饰符将使NGINX以不同的方式处理URL。最常见的几个修饰符是:

  • none: If no modifiers are present in a location block then the requested URI will be matched against the beginning of the requested URI.
  • =: The equal sign is used to match a location block exactly against a requested URI.
  • ~: The tilde sign is used for case-sensitive regular expression match against a requested URI.
  • ~*: The tilde followed by asterisk sign is used for case insensitive regular expression match against a requested URI.
  • ^~: The carat followed by tilde sign is used to perform longest nonregular expression match against the requested URI. If the requested URI hits such a location block, no further matching will takes place.

NGINX如何选择一个location块。

一个位置可以通过使用前缀字符串或使用正则表达式来定义。不区分大小写的正则表达式使用前置的“~*”修饰符指定,而对于不区分大小写的正则表达式,则使用“~”修饰符。为了找到与URI匹配的位置,NGINX首先扫描使用前缀字符串(不包括正则表达式)定义的位置。然后,按照它们在配置文件中声明的顺序检查使用正则表达式的位置。NGINX会按照以下步骤对比请求的URI来选择一个位置块。

  • NGINX starts with looking for an exact match specified with location = /some/path/ and if a match is found then this block is served right away.
  • If there are no such exact location blocks then NGINX proceed with matching longest non-exact prefixes and if a match is found where ^~ modifier have been used then NGINX will stop searching further and this location block is selected to serve the request.
  • If the matched longest prefix location does not contain ^~ modifier then the match is stored temporarily and proceed with following steps.NGINX now shifts the search to the location block containing ~ and ~* modifier and selects the first location block that matches the request URI and is immediately selected to serve the request.
    If no locations are found in the above step that can be matched against the requested URI then the previously stored prefix location is used to serve the request.

NGINX位置块示例

让我们列举出一些使用修饰符和URI的NGINX位置块的例子。

1. 匹配所有请求的NGINX位置

在下面的示例中,前缀位置「/」将匹配所有请求,但仅在找不到其他匹配时作为最后的选择。

location / {
    ...
}

2. 匹配精确URL的NGINX位置

NGINX始终首先尝试匹配最具体的前缀位置。因此,以下位置块中的等号强制与所请求的路径进行精确匹配,然后停止继续搜索其他匹配项。

location = /images { 
    ...
}

上述位置块将与URL https://domain.com/images 匹配,但不会匹配URL https://domain.com/images/index.html或https://domain.com/images/。

3. 用于目录的NGINX位置块

以下位置块将匹配以/images/开头的任何请求,然后继续搜索请求的URI的更具体位置块。因此,如果NGINX找不到更具体的匹配项,将选择该位置块。

location /images/ {
     ...
     ...
}

4. NGINX正则表达式位置示例

以下位置块中的修饰符^~会导致区分大小写的正则表达式匹配。因此,URI /images或/images/logo.png将被匹配,但在找到匹配项后停止搜索。

location ^~ /images {
   ...
   ...
}

5. 图片/CSS/JS文件类型的NGINX位置块

下一个位置块中的修饰符~*匹配以png、ico、gif、jpg、jpeg、css或js结尾的任何请求(不区分大小写)。然而,对/images/文件夹的所有请求将由前一个位置块提供服务。

location ~* .(png|ico|gif|jpg|jpeg|css|js)$ {
    ...
    ...
}

6. NGINX位置正则表达式区分大小写的匹配

在以下位置的修饰符~导致了一个区分大小写的正则表达式匹配,但不会停止寻找更好的匹配。

location ~ /images {
    ...
    ...
}

7. NGINX正则表达式忽略大小写匹配示例

在下面的位置块中,修饰符 ~* 导致不区分大小写的正则表达式匹配,但搜索不会在此处停止以获得更好的匹配。

location ~* /images {
     ...
     ...
}

总结

理解NGINX的location指令对于追踪文件系统中请求的URI的终点至关重要。本文讨论的修饰符、选择location块的步骤以及一些示例将帮助您轻松入门NGINX中的location块。

发表回复 0

Your email address will not be published. Required fields are marked *


广告
将在 10 秒后关闭
bannerAds