Nginx configuration location rules first-level and comparison

Source: Internet
Author: User
Tags regular expression

The Location command in Nginx is an important command in NginxHttpCoreModule. The Location command is used to configure the matched URI. URI is the "/uri/" in the syntax, which can be a string or regular expression. However, to use a regular expression, you must specify a prefix.


Location expression type
~ Indicates executing a regular expression matching, Case Sensitive
~ * Indicates executing a regular expression matching, which is case insensitive.
^ ~ Matches common characters. Use prefix match. If the match is successful, no other locations are matched.
= Exact match of common characters. That is, exact match.
@ "@" Defines a named location, which is used for internal targeting, such as error_page and try_files.
Location priority description

The location order in nginx does not have much to do with the configuration. The type of the positive location expression is related. For expressions of the same type, long strings are matched first.
The following table lists the priorities:
First priority: the equal sign type (=) has the highest priority. Once the matching is successful, no other matching items are found.
Second priority: ^ ~ Type expression. Once the matching is successful, no other matching items are found.
Third priority: Regular expression type (~ ~ . If there are multiple location regular expressions that can match, use the one with the longest regular expression.
Fourth priority: regular string matching type. Match by prefix.

Location priority example
The configuration items are as follows:

Location = /{
# Match requests only/
[Configuration A]
}
Location /{
# Match all requests starting. However, if a longer expression of the same type exists, select a longer expression. If a regular expression can be matched
# Match the regular expression first.
[Configuration B]
}
Location/documents /{
# Match all requests starting with/documents. However, if a longer expression of the same type exists, select a longer expression.
# If a regular expression can be matched, the regular expression is matched first.
[Configuration C]
}
Location ^ ~ /Images /{
# Match all expressions starting with/images/. If the match is successful, the search is stopped. Therefore, even if there is a regular expression location
# Not used
[Configuration D]
}
Location ~ * \. (Gif | jpg | jpeg) $ {
# Match all requests ending with gif jpg jpeg. However, for requests starting with/images/, Configuration D is used.
[Configuration E]
}


Request matching example

/-> Configuration
/Index.html-> configuration B
/Documents/document.html-> configuration C
/Images/1.gif-> configuration D
/Documents/1.jpg-> configuration E

Note that the above matching is irrelevant to the sequence defined in the configuration file.



Nginx location rule priority comparison

The matching rules that may be involved in nginx location include:
= Exact match
^ ~ Common character matching, Case Sensitive
~ Regular Expression Matching, Case Sensitive
/Xxx/yyy. zzz longest match
/
The nginx version used in this article is
[Root @ node1 nginx] # nginx-v
Nginx version: nginx/1.4.3
The ip address of the lab machine is 192.168.151.70, the browser is IE8, and cookies are not saved. Compare the five matching rules in sequence.
1. = comparison ^ ~
Location ^ ~ /Images {
Rewrite ^/images http://www.baidu.com permanent;
        }

Location =/images {
Rewrite/images http://www.sina.com.cn permanent;
        }
Result
[Root @ agent3 tmp] # wget http: // 192.168.151.70/images
-- 12:05:53 -- http: // 192.168.151.70/images
Connecting to 192.168.151.70: 80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
-- 12:05:53 -- http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn | 61.172.201.194 |: 80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690283 (674 K) [text/html]
Saving to: 'index.html'

100% [============================================== =====================================>] 690,283 --. -K/s in 0.08 s

12:05:54 (8.45 MB/s)-'index.html 'saved [690283/690283]
= Priority over ^ ~

2. = comparison ~
Location ~ /Images {
Rewrite ^/images http://www.baidu.com permanent;
        }

Location =/images {
Rewrite/images http://www.sina.com.cn permanent;
        }
Result
[Root @ agent3 tmp] # wget http: // 192.168.151.70/images
-- 12:05:53 -- http: // 192.168.151.70/images
Connecting to 192.168.151.70: 80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
-- 12:05:53 -- http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn | 61.172.201.194 |: 80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690283 (674 K) [text/html]
Saving to: 'index.html'

100% [============================================== =====================================>] 690,283 --. -K/s in 0.08 s

12:05:54 (8.45 MB/s)-'index.html 'saved [690283/690283]
= Priority greater ~

3. = comparison/xxx/yyy. zzz
Location/images. jsp {
Rewrite ^/images http://www.baidu.com permanent;
        }

Location =/images {
Rewrite/images http://www.sina.com.cn permanent;
        }
Result:
[Root @ agent3 tmp] # wget http: // 192.168.151.70/images. jsp
-- 12:10:58 -- http: /192.168.151.70/images. jsp
Connecting to 192.168.151.70: 80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
-- 12:10:58 -- http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn | 61.172.201.194 |: 80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690375 (674 K) [text/html]
Saving to: 'index.html. 1'

100% [============================================== =====================================>] 690,375 --. -K/s in 0.08 s

12:10:58 (8.55 MB/s)-'index.html. 1 'saved [690375/690375]
= Priority over/xxx/yyy. zzz

4. = comparison/
Location /{
Rewrite ^/(. *) $ http://www.baidu.com permanent;
        }

Location = /{
Rewrite ^/(. *) $ http://www.sina.com.cn permanent;
        }
Result
[Root @ agent3 tmp] # wget http: // 192.168.151.70
-- 12:31:09 -- http: // 192.168.151.70/
Connecting to 192.168.151.70: 80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
-- 12:31:09 -- http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.195, 61.172.201.194
Connecting to www.sina.com.cn | 61.172.201.195 |: 80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690538 (674 K) [text/html]
Saving to: 'index.html. 6'

100% [============================================== =====================================>] 690,538 1.93 M/s in 0.3 s

12:31:09 (1.93 MB/s)-'index.html. 6 'saved [690538/690538]
= Priority greater/

5 ^ ~ Comparison ~
Location ~ /Images {
Rewrite/images http://www.baidu.com permanent;
        }

Location ^ ~ /Images {
Rewrite/images http://www.sina.com.cn permanent;
        }
Result
[Root @ agent3 tmp] # wget http: // 192.168.151.70/images
-- 12:05:53 -- http: // 192.168.151.70/images
Connecting to 192.168.151.70: 80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
-- 12:05:53 -- http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn | 61.172.201.194 |: 80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690283 (674 K) [text/html]
Saving to: 'index.html'

100% [============================================== =====================================>] 690,283 --. -K/s in 0.08 s

12:05:54 (8.45 MB/s)-'index.html 'saved [690283/690283]
^ ~ Priority greater ~
6. ^ ~ Comparison:/xxx/yyy. zz
Location/images. jsp {
Rewrite ^/(. *) $ http://www.baidu.com permanent;
        }

Location ^ ~ /Images {
Rewrite ^/(. *) $ http://www.sina.com.cn permanent;
        }
Result
[Root @ agent3 tmp] # wget http: // 192.168.151.70/images. jsp
-- 12:10:58 -- http: /192.168.151.70/images. jsp
Connecting to 192.168.151.70: 80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
-- 12:10:58 -- http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn | 61.172.201.194 |: 80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690375 (674 K) [text/html]
Saving to: 'index.html. 1'

100% [============================================== =====================================>] 690,375 --. -K/s in 0.08 s
^ ~ Priority greater than/xxx/yyy. zzz
12:10:58 (8.55 MB/s)-'index.html. 1 'saved [690375/690375]

7 .~ Comparison:/xxx/yyy. zzz
Location/images. jsp {
Rewrite ^/(. *) $ http://www.baidu.com permanent;
        }

Location ~ /Images {
Rewrite ^/(. *) $ http://www.sina.com.cn permanent;
        }
Result:
[Root @ agent3 tmp] # wget http: // 192.168.151.70/images. jsp
-- 12:19:17 -- http: /192.168.151.70/images. jsp
Connecting to 192.168.151.70: 80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
-- 12:19:17 -- http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.195, 61.172.201.194
Connecting to www.sina.com.cn | 61.172.201.195 |: 80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690441 (674 K) [text/html]
Saving to: 'index.html. 2'

100% [============================================== =====================================>] 690,441 --. -K/s in 0.07 s

12:19:17 (9.12 MB/s)-'index.html. 2 'saved [690441/690441]
~ Priority greater than/xxx/yyy. zzz

8 .~ Comparison/
Location /{
Rewrite ^/(. *) $ http://www.baidu.com permanent;
        }

Location ~ /{
Rewrite ^/(. *) $ http://www.sina.com.cn permanent;
        }
Result
[Root @ agent3 tmp] # wget http: // 192.168.151.70
-- 12:26:26 -- http: // 192.168.151.70/
Connecting to 192.168.151.70: 80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
-- 12:26:26 -- http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn | 61.172.201.194 |: 80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690516 (674 K) [text/html]
Saving to: 'index.html. 5'

100% [============================================== =====================================>] 690,516 2.23 M/s in 0.3 s

12:26:27 (2.23 MB/s)-'index.html. 5' saved [690516/690516]
~ Priority greater/

9./xxx/yyy. zzz comparison/
Obviously, the priority of/xxx/yyy. zzz is greater/

In general, the priority order of the location rule is
=> ^ ~ > ~ >/Xxx/yyy. zzz>/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.