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¶m2 (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¶m2 (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¶m2 (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¶m2 (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:
- location blocks with the = modifier: If the specified string exactly matches the requested URI, Nginx retains the location block.
- location blocks with no modifier: If the specified string exactly matches the requested URI, Nginx retains the location block.
- location blocks with the ^~ modifier: If the specified string matches the beginning of the requested URI, Nginx retains the location block.
- location blocks with ~ or ~* modifier: If the regular expression matches the requested URI, Nginx retains the location block.
- 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.
Nginx - HTTP Configuration, the Location Block的更多相关文章
- Nginx:The Location Block Selection Algorithm
Nginx:The Location Block Selection Algorithm,摘自NGINX:A PRACTICAL GUIDE TO HIGH PERFORMANCE Nginx配置文件 ...
- Nginx - HTTP Configuration, Module Directives
Socket and Host Configuration This set of directives will allow you to configure your virtual hosts. ...
- nginx配置文件中的location中文详解
location 语法:location [=|~|~*|^~] /uri/ { … }默认:否 上下文:server 这个指令随URL不同而接受不同的结构.你可以配置使用常规字符串和正则表达式.如果 ...
- nginx配置文件中的location详解
location 语法:location [=|~|~*|^~] /uri/ { … } 默认:否 上下文:server 这个指令随URL不同而接受不同的结构.你可以配置使用常规字符串和正则表达式.如 ...
- nginx配置文件中的location理解
关于一些对location认识的误区 1. location 的匹配顺序是"先匹配正则,再匹配普通". 矫正: location 的匹配顺序其实是"先匹配普通,再匹配正则 ...
- Nginx配置请求转发location及rewrite规则
一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这 ...
- [转] Nginx配置中的location、root、alias
Nginx配置中的location.root.alias location & root 初始配置 [root@adailinux vhost]# cat rio.conf server { ...
- tomcat启动异常(严重: Dispatcher initialization failed Unable to load configuration. - [unknown location] )
严重: Dispatcher initialization failed Unable to load configuration. - [unknown location] at com.opens ...
- Nginx之旅系列 - Nginx的configuration
题记:Nginx之旅系列是用来记录Nginx从使用到源码学习的点点滴滴,分享学习Nginx的快乐 Nginx 首页: http://nginx.org/ Nginx的configuration 今天对 ...
随机推荐
- Java中的Filter过滤器
Filter简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,Web开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件 ...
- 使用apt-get autoremove造成的系统无法开机
由于误操作(apt-get autoremove xxx)删除了一些lib文件貌似,之后,系统直接重启,然后就无法进入系统,后使用引导盘对系统进行修复,思路如下: 1.挂载已经有的分区,挂载为可读可写 ...
- ags模版与vs
esri为每个版本的sdk指定了特定的vs开发版本,比如ags10.0,ags10.1指定的是vs2008和vs2010,大概是因为发布时间的关系. 无论如何,我们可以将模版移植到新的vs下.(注意红 ...
- 在自定义的dwt文件中调用page_header.lbi和page_footer.lbi
昨天下午接到需求说要增加一个新的页面,作为优惠活动规则的介绍之用,之前对ecshop各种修改,但是这次自己做页面还是第一次,文件太多,函数也太多,一个一个的读过来时间很头疼的事情,于是就参照goods ...
- android 对一个合并后的联系人选择编辑,手机屏幕会缓慢变暗后再进入编辑界面的问题
1. 手机上有一个合并过的联系人 2. 编辑合并后的联系人 3. 手机屏幕会缓慢变暗之后再进入编辑界面. 首先找到contacts源码包下的EditContactA ...
- cocos2d-x CCListView
转自:http://blog.csdn.net/onerain88/article/details/7641126 cocos2d-x 2.0 版更新了,把opengl 1.1 替换为opengl 2 ...
- 一种基于Welch's t检验的二元关系挖掘
现实中常常需要挖掘两种因素之间的关联,Welch's t检验很适合其中的nomial-numerical的关系挖掘.比如天气状况对销量的影响,或者天气情况对交通流量的影响等等.我们可以按照下雨/不下雨 ...
- Spring-AOP和AspectJ的区别和联系
AOP是Spring框架的重要组成部分.目前我所接触的AOP实现框架有Spring AOP还有就是AspectJ(还有另外几种我没有接触过).我们先来说说他们的区别: AspectJ是一个比较牛逼的A ...
- 445port入侵具体解释
445port入侵具体解释 关于"445port入侵"的内容445port入侵具体解释本站搜索很多其它关于"445port入侵"的内容 445port入侵, ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...