nginx 配置示例_Nginx位置指令示例

本文详细介绍了NGINX的location指令,包括其语法、选择位置块的逻辑以及各种匹配示例,如精确匹配、正则表达式匹配等,帮助理解如何在文件系统中路由请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nginx 配置示例

The location directive within NGINX server block allows to route request to correct location within the file system.

NGINX服务器块中的location指令允许将请求路由到文件系统中的正确位置。

The directive is used to tell NGINX where to look for a resource by including files and folders while matching a location block against an URL. In this tutorial, we will look at NGINX location directives in details.

该指令用于通过包含文件和文件夹,同时将位置块与URL匹配,来告诉NGINX在哪里寻找资源。 在本教程中,我们将详细介绍NGINX位置指令。

先决条件 (Prerequisite)

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

    您已经按照此处的教程安装了NGINX。

NGINX位置指令语法 (NGINX location directive syntax)

The NGINX location block can be placed inside a server block or inside another location block with some restrictions.

NGINX位置块可以在某些限制下放置在服务器块内或另一个位置块内。

The syntax for constructing a location block is:

构造位置块的语法为:

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

The modifier in the location block is optional. Having a modifier in the location block will allow NGINX to treat a URL differently. Few most common modifiers are:

位置块中的修饰符是可选的。 在位置块中使用修饰符将使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.

    none :如果位置块中没有修饰符,则所请求的URI将与所请求的URI的开头进行匹配。
  • =: The equal sign is used to match a location block exactly against a requested URI.

    = :等号用于将位置块与请求的URI完全匹配。
  • ~: The tilde sign is used for case-sensitive regular expression match against a requested URI.

    :波浪号用于与请求的URI区分大小写的正则表达式匹配。
  • ~*: The tilde followed by asterisk sign is used for case insensitive regular expression match against a requested URI.

    〜* :代字号后跟星号,用于与请求的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.

    ^〜 :后面跟有波浪号的克拉符号用于对请求的URI执行最长的非正则表达式匹配。 如果请求的URI碰到了这样的位置块,则不会进行进一步的匹配。

NGINX如何选择位置区 (How NGINX choose a location block)

A location can be defined by using a prefix string or by using a regular expression. Case-insensitive regular expressions are specified with preceding “~*” modifier and for a case-insensitive regular expression, the “~” modifier is used.

可以通过使用前缀字符串或使用正则表达式来定义位置。 不区分大小写的正则表达式由前面的“ 〜* ”修饰符指定,对于不区分大小写的正则表达式,使用“ ”修饰符。

To find a location match for an URI, NGINX first scans the locations that is defined using the prefix strings (without regular expression). Thereafter, the location with regular expressions are checked in order of their declaration in the configuration file.

为了找到与URI匹配的位置,NGINX首先扫描使用前缀字符串(没有正则表达式)定义的位置。 此后,将按正则表达式的位置按照它们在配置文件中的声明顺序进行检查。

NGINX will run through the following steps to select a location block against a requested URI.

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.

    NGINX首先寻找由location = /some/path/指定的精确匹配,如果找到匹配,则立即提供此块。
  • 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.

    如果没有这样的确切位置块,则NGINX继续匹配最长的非精确前缀,如果在使用了^〜修饰符的地方找到匹配项,则NGINX将停止进一步搜索,并选择该位置块来满足请求。
  • 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匹配并立即被选择为请求服务的第一个位置块。
    • 如果在上述步骤中未找到可以与请求的URI匹配的位置,则可以使用先前存储的前缀位置来处理请求。

NGINX位置块示例 (NGINX location block examples)

Let us list few examples of NGINX location blocks using modifier and URI.

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

1. NGINX位置匹配所有请求 (1. NGINX location matching all requests)

In the following example the prefix location / will match all requests but will be used as a last resort if no matches are found.

在下面的示例中,前缀位置/将匹配所有请求,但如果找不到匹配项,则将被用作最后的手段。

location / {
    ...
}

2. NGINX位置匹配的确切网址 (2. NGINX location matching exact URL)

NGINX always tries to match most specific prefix location at first. Therefore, the equal sign in the following location block forces an exact match with the path requested and then stops searching for any more matches.

NGINX首先总是尝试匹配最特定的前缀位置。 因此,以下位置块中的等号会强制与请求的路径完全匹配,然后停止搜索更多匹配项。

location = /images { 
    ...
}

The above location block will match with the URL https://domain.com/images but the URL https://domain.com/images/index.html or https://domain.com/images/ will not be matched.

上面的位置块将与URL https://domain.com/images匹配,但URL https://domain.com/images/index.htmlhttps://domain.com/images/将不匹配。

3.目录的NGINX位置块 (3. NGINX location block for a directory)

The following location block will match any request starting with /images/ but continue with searching for more specific block for the requested URI. Therefore the location block will be selected if NGINX does not find any more specific match.

以下位置块将匹配以/ images /开头的所有请求,但继续为请求的URI搜索更具体的块。 因此,如果NGINX没有找到更具体的匹配项,则将选择位置块。

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

4. NGINX位置RegEx示例 (4. NGINX location RegEx Example)

The modifier ^~ in the following location block results in a case sensitive regular expression match. Therefore the URI /images or /images/logo.png will be matched but stops searching as soon as a match is found.

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

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

5.用于图像/ css / js文件类型的NGINX位置块 (5. NGINX location block for image/css/js file types)

The modifier ~* in the next location block matches any request (case-insensitive) ending with png, ico, gif, jpg, jpeg, css or js. However, any requests to the /images/ folder will be served by the previous location block.

下一个位置块中的修饰符〜*匹配任何以png,ico,gif,jpg,jpeg,css或js结尾的请求(不区分大小写)。 但是,对/images/文件夹的任何请求将由上一个位置块提供。

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

6. NGINX位置正则表达式区分大小写 (6. NGINX location RegEx Case Sensitive Match)

The modifier ~ in the following location block results in a case sensitive regular expression match but doesn’t stop searching for a better match.

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

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

7. NGINX位置RegEx不区分大小写的匹配示例 (7. NGINX location RegEx Case Insensitive Match Example)

The modifier ~* in the following location block results in a case insensitive regular expression match but the searching doesn’t stop here for a better match.

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

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

摘要 (Summary)

Understanding NGINX location directive is essential for tracing end points of requested URI in the file system. The modifiers, steps of selecting location block and few examples discussed in this article will help you to get started with location blocks in NGINX easily.

了解NGINX位置指令对于跟踪文件系统中请求的URI的端点至关重要。 本文中讨论的修饰符,选择位置块的步骤以及一些示例将帮助您轻松地开始使用NGINX中的位置块。

翻译自: https://www.journaldev.com/26342/nginx-location-directive

nginx 配置示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值