注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

永福的技术博客

Linux运维

 
 
 

日志

 
 

【永福】nginx.conf中location的语法【根据官方文档翻译】  

2011-07-28 17:38:39|  分类: 软件编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

location

syntax: location [=|~|~*|^~|@] /uri/ { ... }

default: no

context: server

This directive allows different configurations depending on the URI. It can be configured using both literal strings and regular expressions. To use regular expressions, you must use a prefix:
  1. "~" for case sensitive matching
  2. "~*" for case insensitive matching
这个指令允许根据URI使用不同的配置。可以使用字符串和正则。使用正则表达式,必须使用前缀:
  1. “~” 大小写敏感的匹配
  2. “~*” 大小写不敏感的匹配

To determine which location directive matches a particular query, the literal strings are checked first. Literal strings match the beginning portion of the query - the most specific match will be used. Afterwards, regular expressions are checked in the order defined in the configuration file. The first regular expression to match the query will stop the search. If no regular expression matches are found, the result from the literal string search is used.
为了决定哪个location指令匹配了特定的查询,字符串将会被首先检查。匹配查询最多的字符串将被使用。然后才是正则表达式,检查的顺序以配置文件为准。查询碰到第一个匹配的正则就将停止搜索。如果没有正则匹配被找到,将会使用字符串查找。

For case insensitive operating systems, like Mac OS X or Windows with Cygwin, literal string matching is done in a case insensitive way (0.7.7). However, comparison is limited to single-byte locale's only.
在大小写敏感的OS,像Mac OS X 或 Windows with Cygwin,字符串匹配方式为大小写敏感。但是,比较被限制在单字节环境(不匹配双字节等字符)。

Regular expression may contain captures (0.7.40), which can then be used in other directives.
正则表达式中可以有捕获的变量,这些变量可以在其他指令中使用。

It is possible to disable regular expression checks after literal string matching by using "^~" prefix. If the most specific match literal location has this prefix: regular expressions aren't checked.
可以使用 “^~” 前缀来停止正则匹配。如果指定的匹配字符location指令有这个前缀,将不检查正则。

By using the "=" prefix we define the exact match between request URI and location. When matched search stops immediately. E.g., if the request "/" occurs frequently, using "location = /" will speed up processing of this request a bit as search will stop after first comparison.
使用“=”前缀,请求的URI和location将精确匹配。当匹配到时,立即停止搜索。例如,如果请求“/” 经常出现,使用"location = /" 会得到加速,因为只要匹配一个字符就可将停止。

On exact match with literal location without "=" or "^~" prefixes search is also immediately terminated.
一个location字符串在没有 "=" 或者 "^~" 前缀的情况下的精确匹配到时也将立即终止。

To summarize, the order in which directives are checked is as follows:
  1. Directives with the "=" prefix that match the query exactly. If found, searching stops.
  2. All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
  3. Regular expressions, in the order they are defined in the configuration file.
  4. If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.

It is important to know that nginx does the comparison against decoded URIs. For example, if you wish to match "/images/%20/test", then you must use "/images/ /test" to determine the location.
指令检查顺序总结如下:
  1. 查询精确匹配到使用"="前缀的指令,如果找到,搜索停止。
  2. 剩下的所有所有常规的字符串。如果匹配到的使用了"^~"的前缀,搜索停止。
  3. 正则表达式,以在配置文件中的顺序为准。
  4. 如果满足第三条匹配,则使用该结果。否则,使用第二条的匹配。
必须记住,nginx进行比较的是解码后的URIs。例如,若你要匹配"/images/%20/test",你需要在location指令出输入"/images/ /test"。

Example:

location  = / {  
# matches the query / only.
[ configuration A ]
}

location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
[ configuration B ]
}


location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
[ configuration C ]
}

location ~* \.(gif|jpg|jpeg)$ {
 # matches any request ending in gif, jpg, or jpeg. However, all
 # requests to the /images/ directory will be handled by
 # Configuration C.
 
[ configuration D ]
}

Example requests:

  • / -> configuration A
  • /documents/document.html -> configuration B
  • /images/1.gif -> configuration C
  • /documents/1.jpg -> configuration D

Note that you could define these 4 configurations in any order and the results would remain the same. While nested locations are allowed by the configuration file parser, their use is discouraged and may produce unexpected results.

The prefix "@" specifies a named location. Such locations are not used during normal processing of requests, they are intended only to process internally redirected requests (seeerror_page, try_files).

  评论这张
 
阅读(2650)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018