初识nginx服务器配置之location配置规则
2016-05-03 17:15
429 查看
Nginx服务器配置的官方文档在:http://nginx.org/en/docs/http/ngx_http_core_module.html
然后点击【location】就可以进入location的配置规则中。
文档中写到:
他有两种格式: ①
location [
... }
② location
... }
在【初识Nginx服务器配置之文件目录】中写到:【location】是在http下的server下的。
一、我们先看location的第一种配置格式:location [
... },其中[]中的内容是可选的。可以是不写的。
原文:A location can either be defined by a prefix string, or by a regular
expression.
在第一种配置格式中又可以细分成两种格式:【前缀字符串】(文档中使用【prefix
string】表示)和【正则表达式】(文档中使用【regular expression】表示)。
原文:Regular expressions are specified with the preceding “
modifier (for case-insensitive matching), or the “
如果使用的是【正则表达式】的话,需要使用【~*】或者【~】作为前缀的。
而【~*】是不区分大小写的【for
case-insensitive matching】
【~】是区分大小写的【for case-sensitive matching】
剩下的【=】【^~】【不写】都是前缀字符串【prefix string】。
二、既然Nginx的请求地址有两种【前缀字符串】和【正则表达式】,那么这两种肯定有先后顺序的:
To find location matching a given request, nginx
first checks locations defined using the prefix strings (prefix locations).
注意:★★★★【nginx的查找规则】是:先查找【前缀字符串prefix strings】(因为【nginx
first checks】),然后【再】查看【正则表法式】。即查找完【前缀字符串】之后还要查找【正则表达式】。
在查找【前缀字符串】时也是有规则的:
Among them, the location with the longest matching prefix is selected and remembered.
在【前缀字符串】中,location使用最长匹配前缀模式【the longest matching prefix】。所以就和配置文件中写的位置没有关系。即【前缀字符串】是匹配的【最长的匹配模式】,所以【前缀字符串】在配置文件中写的位置没有影响。
Then regular expressions are checked, in the order of their appearance in the configuration file.
这句话有两层意思:①、当【前缀字符串】匹配完成之后,【正则表达式】还会继续匹配,这时匹配的顺序就是【配置文件所写的循序。】,在配置文件出现的循序。
The search of regular expressions terminates on the first match, and the corresponding configuration is
used.
如果【正则表达式】有匹配成功的,就会停止。并使用这个【正则表达式】。就是:【正则表达式】在上面说了,他是有循序的。就是在【配置文件的所写的循序】有关。当【前缀字符串】匹配完成之后,就会继续检查【正则表达式】,但正则表达式不一定都匹配,按照配置文件中的所写的循序进行匹配。当有第一个匹配成功,就会停止匹配。并使用这个location的配置属性。而不是用前面匹配的【前缀字符串】,即【覆盖】了。
If no match with a regular expression is found then the configuration of the prefix location remembered
earlier is used.
如果没有【正则表达式】匹配成功,就会使用【前缀字符串】匹配成功的location配置属性。
三、在默认的情况下:循序是先找【前缀字符串】,然后【正则表达式】,无论【前缀字符串】是不是找到,他都会去找【正则表达式】,在Nginx中提供了【二种】改变这种规则的方式。就是找到【前缀字符串】就不会向下查找了。
①、If
the longest matching prefix location has the “
使用【^~】前缀字符串,^表示非,而【~】表示正则表达式,就是不使用正则表达式,如果匹配的【前缀字符串】前面有【^~】就会停止查找【正则表达式】。
★★★★注意:【^~】匹配的模式还是【最长匹配的模式】
②、Also,
using the “
match is found, the search terminates.
第二种就是使用【=】作为前缀。如果匹配的【前缀字符串】前面【=】,就会停止查找【正则表达式】。
★★★★注意:【=】匹配的模式是【精确的匹配的模式】(【exact
match of URI and location】),就是请求的uri必须是完全匹配。
例如:在配置文件中写到:
配置3.1
srver{
listen 80;
server_name localhost;
location = /xx/xx.html{
deny all;
}
location ~ \.html{
allow all;
}
}
-------------------------------------------------------------------------------------------------------------
配置3.2
srver{
listen 80;
server_name localhost;
location /xx/xx.html{
deny all;
}
location ~ \.html{
allow all;
}
}
当有【=】前缀时,当匹配是【精确匹配】就会停止检查【正则表达式】。如果没有【=】前缀表达式,局算是【精确匹配】他也会继续查找【正则表达式】。
四、location的另一种表达式方式【@】
The “
prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection.
【@】前缀用于定义一个名称位置,他不用于普通的【请求】处理,而是用于【请求的重定向】中。
总结:
①先【前缀字符串】然后是【正则表达式】
②如果【前缀字符串】有【^~】或者【=】就会停止检查【正则表达式】,按照这个【前缀字符串】配置
③在查找【正则表达式】,按照配置文件先后循序配置,检查到第一个,就会按照这个正则表达式的配置
④如果【正则表达式】中没有匹配的模式,就会使用【前缀字符串】的匹配模式。
然后点击【location】就可以进入location的配置规则中。
文档中写到:
Syntax: | location [|=| [code]~ ~*| ^~] uri{ ... }[/code] location{ ... }[/code]@[code]name |
---|---|
Default: | — |
Context: | server, location |
location [
=|
~|
~*|
^~]
uri{
... }
② location
@
name{
... }
在【初识Nginx服务器配置之文件目录】中写到:【location】是在http下的server下的。
一、我们先看location的第一种配置格式:location [
=|
~|
~*|
^~]
uri{
... },其中[]中的内容是可选的。可以是不写的。
原文:A location can either be defined by a prefix string, or by a regular
expression.
在第一种配置格式中又可以细分成两种格式:【前缀字符串】(文档中使用【prefix
string】表示)和【正则表达式】(文档中使用【regular expression】表示)。
原文:Regular expressions are specified with the preceding “
~*”
modifier (for case-insensitive matching), or the “
~” modifier (for case-sensitive matching).
如果使用的是【正则表达式】的话,需要使用【~*】或者【~】作为前缀的。
而【~*】是不区分大小写的【for
case-insensitive matching】
【~】是区分大小写的【for case-sensitive matching】
剩下的【=】【^~】【不写】都是前缀字符串【prefix string】。
二、既然Nginx的请求地址有两种【前缀字符串】和【正则表达式】,那么这两种肯定有先后顺序的:
To find location matching a given request, nginx
first checks locations defined using the prefix strings (prefix locations).
注意:★★★★【nginx的查找规则】是:先查找【前缀字符串prefix strings】(因为【nginx
first checks】),然后【再】查看【正则表法式】。即查找完【前缀字符串】之后还要查找【正则表达式】。
在查找【前缀字符串】时也是有规则的:
Among them, the location with the longest matching prefix is selected and remembered.
在【前缀字符串】中,location使用最长匹配前缀模式【the longest matching prefix】。所以就和配置文件中写的位置没有关系。即【前缀字符串】是匹配的【最长的匹配模式】,所以【前缀字符串】在配置文件中写的位置没有影响。
Then regular expressions are checked, in the order of their appearance in the configuration file.
这句话有两层意思:①、当【前缀字符串】匹配完成之后,【正则表达式】还会继续匹配,这时匹配的顺序就是【配置文件所写的循序。】,在配置文件出现的循序。
The search of regular expressions terminates on the first match, and the corresponding configuration is
used.
如果【正则表达式】有匹配成功的,就会停止。并使用这个【正则表达式】。就是:【正则表达式】在上面说了,他是有循序的。就是在【配置文件的所写的循序】有关。当【前缀字符串】匹配完成之后,就会继续检查【正则表达式】,但正则表达式不一定都匹配,按照配置文件中的所写的循序进行匹配。当有第一个匹配成功,就会停止匹配。并使用这个location的配置属性。而不是用前面匹配的【前缀字符串】,即【覆盖】了。
If no match with a regular expression is found then the configuration of the prefix location remembered
earlier is used.
如果没有【正则表达式】匹配成功,就会使用【前缀字符串】匹配成功的location配置属性。
三、在默认的情况下:循序是先找【前缀字符串】,然后【正则表达式】,无论【前缀字符串】是不是找到,他都会去找【正则表达式】,在Nginx中提供了【二种】改变这种规则的方式。就是找到【前缀字符串】就不会向下查找了。
①、If
the longest matching prefix location has the “
^~” modifier then regular expressions are not checked.
使用【^~】前缀字符串,^表示非,而【~】表示正则表达式,就是不使用正则表达式,如果匹配的【前缀字符串】前面有【^~】就会停止查找【正则表达式】。
★★★★注意:【^~】匹配的模式还是【最长匹配的模式】
②、Also,
using the “
=” modifier it is possible to define an exact match of URI and location. If an exact
match is found, the search terminates.
第二种就是使用【=】作为前缀。如果匹配的【前缀字符串】前面【=】,就会停止查找【正则表达式】。
★★★★注意:【=】匹配的模式是【精确的匹配的模式】(【exact
match of URI and location】),就是请求的uri必须是完全匹配。
例如:在配置文件中写到:
配置3.1
srver{
listen 80;
server_name localhost;
location = /xx/xx.html{
deny all;
}
location ~ \.html{
allow all;
}
}
-------------------------------------------------------------------------------------------------------------
配置3.2
srver{
listen 80;
server_name localhost;
location /xx/xx.html{
deny all;
}
location ~ \.html{
allow all;
}
}
URI 请求 | 配置 3.1 | 配置 3.2 |
curl http://localhost/xx/xx.html | 403 Forbidden | 404 Not Found |
curl http://localhost/xx/1.html | 404 Not Found | 404 Not Found |
四、location的另一种表达式方式【@】
The “
@”
prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection.
【@】前缀用于定义一个名称位置,他不用于普通的【请求】处理,而是用于【请求的重定向】中。
总结:
①先【前缀字符串】然后是【正则表达式】
②如果【前缀字符串】有【^~】或者【=】就会停止检查【正则表达式】,按照这个【前缀字符串】配置
③在查找【正则表达式】,按照配置文件先后循序配置,检查到第一个,就会按照这个正则表达式的配置
④如果【正则表达式】中没有匹配的模式,就会使用【前缀字符串】的匹配模式。
相关文章推荐
- nginx代理指定目录
- 访问Nginx发生SSL connection error的一种情况
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- Nginx 学习笔记(一)
- 网站502与504错误分析
- 用zabbix监控nginx_status状态
- 艰难完成 nginx + puma 部署 rails 4的详细记录
- 把Lua编译进nginx步骤方法
- C#生成Word文档代码示例
- 如何使用C#从word文档中提取图片
- jQuery窗口、文档、网页各种高度的精确理解
- web 应用中常用的各种 cache详解
- Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略
- window+nginx+php环境配置 附配置搭配说明