Nginx - HTTP Configuration, the Location Block

Nginx offers you the possibility to fine-tune your configuration down to three levels — at the protocollevel (http block), the server
level (server block), and the requested URI level (location block). Let us now detail the latter.

Location Modifier

Nginx allows you to define location blocks by specifying a pattern that will be matched against the requested document URI.

server {
  server_name website.com;
  location /admin/ {
    # The configuration you place here only applies to
    # http://website.com/admin/
  }
}

Instead of a simple folder name, you can indeed insert complex patterns. The syntax of the location block is:

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

The first optional argument is a symbol called location modifier that will define the way Nginx matches the specified pattern and also defines the very nature of the pattern (simple string or regular expression). The following paragraphs detail the different modifiers and their behavior.

The = modifier

The requested document URI must match the specified pattern exactly. The pattern here is limited to a simple literal string; you cannot use a regular expression:

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

The configuration in the location block:

  • Applies to http://website.com/abcd (exact match)
  • Applies to http://website.com/ABCD (it is case-sensitive if your operating system uses a case-sensitive filesystem)
  • Applies to http://website.com/abcd?param1&param2 (regardless of query string arguments)
  • Does not apply to http://website.com/abcd/ (trailing slash)
  • Does not apply to http://website.com/abcde (extra characters after the specified pattern)

No modifier

The requested document URI must begin with the specified pattern. You may not use regular expressions:

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

The configuration in the location block:

  • Applies to http://website.com/abcd (exact match)
  • Applies to http://website.com/ABCD (it is case-sensitive if your operating system uses a case-sensitive filesystem)
  • Applies to http://website.com/abcd?param1&param2 (regardless of query string arguments)
  • Applies to http://website.com/abcd/ (trailing slash)
  • Applies to http://website.com/abcde (extra characters after the specified pattern)

The ~ modifier

The requested URI must be a case-sensitive match to the specified regular expression:

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

The ^/abcd$ regular expression used in this example specifies that the pattern must begin (^) with /, be followed by abc, and finish ($) with d. Consequently, the configuration in the location block:

  • Applies to http://website.com/abcd (exact match)
  • Does not apply to http://website.com/ABCD (case-sensitive)
  • Applies to http://website.com/abcd?param1&param2 (regardless of query string arguments)
  • Does not apply to http://website.com/abcd/ (trailing slash) due to the specified regular expression
  • Does not apply to http://website.com/abcde (extra characters) due to the specified regular expression

With operating systems such as Microsoft Windows, ~ and ~* are both case-insensitive, as the OS uses a case-insensitive filesystem.

The ~* modifier

The requested URI must be a case-insensitive match to the specified regular expression:

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

The regular expression used in the example is similar to the previous one. Consequently, the configuration in the location block:

  • Applies to http://website.com/abcd (exact match)
  • Applies to http://website.com/ABCD (case-insensitive)
  • Applies to http://website.com/abcd?param1&param2 (regardless of query string arguments)
  • Does not apply to http://website.com/abcd/ (trailing slash) due to the specified regular expression
  • Does not apply to http://website.com/abcde (extra characters) due to the specified regular expression

The ^~ modifier

Similar to the no-symbol behavior, the location URI must begin with the specified pattern. The difference is that if the pattern is matched, Nginx stops searching for other patterns (read the section below about search order and priority).

The @ modifier

Defines a named location block. These blocks cannot be accessed by the client, but only by internal requests generated by other directives, such as try_files or error_page.

Search Order and Priority

Since it‘s possible to define multiple location blocks with different patterns, you need to understand that when Nginx receives a request, it searches for the location block that best matches the requested URI:

server {
  server_name website.com;
  location /files/ {
    # applies to any request starting with "/files/"
    # for example /files/doc.txt, /files/, /files/temp/
}
location = /files/ {
    # applies to the exact request to "/files/"
    # and as such does not apply to /files/doc.txt
    # but only /files/
  }
}

When a client visits http://website.com/files/doc.txt, the first location block applies. However, when they visit http://website.com/files/, the second block applies (even though the first one matches) because it has priority over the first one (it is an exact match).

The order you established in the configuration file (placing the /files/ block before the = /files/ block) is irrelevant. Nginx will search for matching patterns in a specific order:

  1. location blocks with the = modifier: If the specified string exactly matches the requested URI, Nginx retains the location block.
  2. location blocks with no modifier: If the specified string exactly matches the requested URI, Nginx retains the location block.
  3. location blocks with the ^~ modifier: If the specified string matches the beginning of the requested URI, Nginx retains the location block.
  4. location blocks with ~ or ~* modifier: If the regular expression matches the requested URI, Nginx retains the location block.
  5. location blocks with no modifier: If the specified string matches the beginning of the requested URI, Nginx retains the location block.

Case 1:

server {
  server_name website.com;
  location /doc {
    […] # requests beginning with "/doc"
  }
  location ~* ^/document$ {
    […] # requests exactly matching "/document"
  }
}

You might wonder: when a client requests http://website.com/document, which of these two location blocks applies? Indeed, both blocks match this request. Again, the answer does not lie in the order in which the blocks appear in the configuration files. In this case, the second location block will apply as the ~* modifier has priority over the other.

Case 2:

server {
  server_name website.com;
  location /document {
    […] # requests beginning with "/document"
  }
  location ~* ^/document$ {
    […] # requests exactly matching "/document"
  }
}

The question remains the same — what happens when a client sends a request to download http://website.com/document? There is a trick here. The string specified in the first block now exactly matches the requested URI. As a result, Nginx prefers it over the regular expression.

Case 3:

server {
  server_name website.com;
  location ^~ /doc {
    […] # requests beginning with "/doc"
  }
  location ~* ^/document$ {
    […] # requests exactly matching "/document"
  }
}

This last case makes use of the ^~ modifier. Which block applies when a client visits http://website.com/document? The answer is the first block. The reason being that ^~ has priority over ~*. As a result, any request with a URI beginning with /doc will be affected to the first block, even if the request URI matches the regular expression defined in the second block.

时间: 2024-08-04 19:32:46

Nginx - HTTP Configuration, the Location Block的相关文章

Nginx - HTTP Configuration, Module Directives

Socket and Host Configuration This set of directives will allow you to configure your virtual hosts. In practice, this materializes by creating server blocks that you identify either by a hostname or by an IP address and port combination. In addition

nginx配置文件中的location详解

原文:http://outofmemory.cn/code-snippet/3037/nginx-configuration-file-de-location-explain-in-detail location 语法:location [=|~|~*|^~] /uri/ { … } 默认:否 上下文:server 这个指令随URL不同而接受不同的结构.你可以配置使用常规字符串和正则表达式.如果使用正则表达式,你必须使用 ~* 前缀选择不区分大小写的匹配或者 ~ 选择区分大小写的匹配. 确定 哪

Nginx配置请求转发location及rewrite规则

一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 # 但是正则和最长字符串会优先匹配 [ configuration B ] } location /documents/ { # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索 # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条 [ c

nginx配置文件中的location中文详解

location 语法:location [=|~|~*|^~] /uri/ { … }默认:否 上下文:server 这个指令随URL不同而接受不同的结构.你可以配置使用常规字符串和正则表达式.如果使用正则表达式,你必须使用 ~* 前缀选择不区分大小写的匹配或者 ~ 选择区分大小写的匹配. 确定 哪个location 指令匹配一个特定指令,常规字符串第一个测试.常规字符串匹配请求的开始部分并且区分大小写,最明确的匹配将会被使用(查看下文明白 nginx 怎么确定它).然后正则表达式按照配置文件

Unable to load configuration. - [unknown location]

严重: Exception starting filter StrutsPrepareFilterUnable to load configuration. - [unknown location] 出现这种错误 jar包冲突 xerce.jar和xml-apis.jar这两个jar包冲突  或者是类似这个xercesImpl-2.9.1.jar删除xerce.jar  即可

tomcat启动异常(严重: Dispatcher initialization failed Unable to load configuration. - [unknown location] )

严重: Dispatcher initialization failed Unable to load configuration. - [unknown location] at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConf

Nginx - HTTP Configuration, Module Variables

The HTTP Core module introduces a large set of variables that you can use within the value of directives. Be careful though, as only a handful of directives accept variables in the definition of their value. If you insert a variable in the value of a

nginx中http.server.location指令语法

location是nginx配置文件中http块下server块下的一个指令,语法结构为: location  [ = | ~ | ~* | ^~ ]  uri  { ... } []内部的部分为可选项,四种标识只能选其一,用于指定请求字符串和uri的匹配方式. 如果不加可选部分,nginx先按顺序将请求路径匹配所有带标准uri的location,再匹配带正则uri的location,如果有正则uri匹配成功则使用该location,如果没有正则uri匹配成功,则使用标准uri中匹配度最高的一个

Nginx.conf 中的location 详解

一.语法规则: location[=|~|~*|^~] /uri/ { - } 1."=" 开头表示精确匹配 2."^~" 开头表示uri以某个常规字符串开头,理解为匹配url路径即可.nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格). 3."~" 开头表示区分大小写的正则匹配 4."~*" 开头表示不区分大小写的正则匹配 5."!~&q