Nginx Location Rule Precedence Comparison

Source: Internet
Author: User
The matching rules that may be involved in Nginx location are
= Exact match
^~ common character matching, case sensitive
~ Regular match, case sensitive
/xxx/yyy.zzz Maximum Match
/
The Nginx version used in this article is
[Root@node1 nginx]# Nginx-v
Nginx version:nginx/1.4.3
Experimental machine IP 192.168.151.70, browser for IE8, do not save cookies. The above five matching rule 22 were tested in sequence.
1. = Contrast ^~
Location ^~/images {
Rewrite ^/images http://www.baidu.com permanent;
}

Location =/images {
Rewrite/images http://www.sina.com.cn Permanent;
}
Results
[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 ... Permanently moved
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 ... OK
length:690283 (674K) [text/html]
Saving to: ' index.html '

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

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

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

Location =/images {
Rewrite/images http://www.sina.com.cn Permanent;
}
Results
[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 ... Permanently moved
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 ... OK
length:690283 (674K) [text/html]
Saving to: ' index.html '

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

12:05:54 (8.45 mb/s)-' index.html ' saved [690283/690283]
= priority is greater than ~

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

Location =/images {
Rewrite/images http://www.sina.com.cn Permanent;
}
Results:
[Root@agent3 tmp]# wget http://192.168.151.70/images/images.jsp
--12:10:58--http://192.168.151.70/images/images.jsp
Connecting to 192.168.151.70:80 ... Connected.
HTTP request sent, awaiting response ... Permanently moved
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 ... OK
length:690375 (674K) [text/html]
Saving to: ' INDEX.HTML.1 '

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

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

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

Location =/{
Rewrite ^/(. *) $ http://www.sina.com.cn permanent;
}
Results
[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 ... Permanently moved
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 ... OK
length:690538 (674K) [text/html]
Saving to: ' INDEX.HTML.6 '

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

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

5 ^~ Contrast ~
Location ~/images {
Rewrite/images http://www.baidu.com Permanent;
}

Location ^~/images {
Rewrite/images http://www.sina.com.cn Permanent;
}
Results
[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 ... Permanently moved
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 ... OK
length:690283 (674K) [text/html]
Saving to: ' index.html '

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

12:05:54 (8.45 mb/s)-' index.html ' saved [690283/690283]
^~ priority is greater than ~
6. ^~ Contrast/xxx/yyy.zz
location/images/images.jsp {
Rewrite ^/(. *) $ http://www.baidu.com permanent;
}

Location ^~/images {
Rewrite ^/(. *) $ http://www.sina.com.cn permanent;
}
Results
[Root@agent3 tmp]# wget http://192.168.151.70/images/images.jsp
--12:10:58--http://192.168.151.70/images/images.jsp
Connecting to 192.168.151.70:80 ... Connected.
HTTP request sent, awaiting response ... Permanently moved
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 ... OK
length:690375 (674K) [text/html]
Saving to: ' INDEX.HTML.1 '

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

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

Location ~/images {
Rewrite ^/(. *) $ http://www.sina.com.cn permanent;
}
Results:
[Root@agent3 tmp]# wget http://192.168.151.70/images/images.jsp
--12:19:17--http://192.168.151.70/images/images.jsp
Connecting to 192.168.151.70:80 ... Connected.
HTTP request sent, awaiting response ... Permanently moved
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 ... OK
length:690441 (674K) [text/html]
Saving to: ' INDEX.HTML.2 '

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

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

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

Location ~/{
Rewrite ^/(. *) $ http://www.sina.com.cn permanent;
}
Results
[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 ... Permanently moved
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 ... OK
length:690516 (674K) [text/html]
Saving to: ' INDEX.HTML.5 '

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

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

9./xxx/yyy.zzz Contrast/
Obviously, the/xxx/yyy.zzz priority is greater than/

In general, the location rule priority order 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.