Chinaunix首页 | 论坛 | 博客
  • 博客访问: 205601
  • 博文数量: 26
  • 博客积分: 572
  • 博客等级: 中士
  • 技术积分: 262
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-16 20:34
文章分类

全部博文(26)

文章存档

2011年(26)

分类: LINUX

2011-09-23 11:40:48

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 the a prefix:

  1. ~* for case insensitive matching
  2. ~ for case sensitive matching

To determine which location directive matches a particular query, the literal strings are checked first. Literal strings match the beginning portion of the query and are case-sensitive - the most specific match will be used (see below on how nginx determines this). 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.

There are two ways to modify this behavior. The first is to use the prefix "=", which matches an exact query only. If the query matches, then searching stops and the request is handled immediately. For example, if the request "/" occurs frequently, then using "location = /" will expedite the processing of this request.

The second is to use the prefix ^~. This prefix is used with a literal string and tells nginx to not check regular expressions if the path provided is a match. For instance, "location ^~ /images/" would halt searching if the query begins with /images/ - all regular expression directives would not be checked.

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.

Example:

= / { # matches the query / only. [ configuration A ] } / { # matches any query, since all queries begin with /, but regular # expressions and any longer conventional blocks will be # matched first. [ configuration B ] } ^~ /images/ { # matches any query beginning with /images/ and halts searching, # so regular expressions will not be checked. [ configuration C ] } ~* \.(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.

阅读(2593) | 评论(0) | 转发(0) |
0

上一篇:nginx 配置expires

下一篇:nginx gzip 详解

给主人留下些什么吧!~~