Nginx detailed configuration module in location (with examples)

Inscription

Prior to playing in the allocation of Nginx location block out some bug, tossing some time. Later, the Internet has access to the relevant information, looking a bit confusing. Free this weekend thinking of a good tidy configuration location module, experiment with their own hands, summed up the configuration location module.

location module configuration

The matching characteristics can be roughly divided into the following sections (in priority order)

The highest priority (=) second priority (^ ~) the third priority (according to the matching sequence ~, ~ *) the fourth priority (/)

1. The match is stopped

=: Indicates exact match, or a match on the last, or will not be matched. If a match is on, it blocks access to the location, others do not see.
^ ~: Priority match by match if the order from the top down to the URL ^ ~ later, it proceeds to the block location, the other is not watching.

2. order matching

~: Indicates a case-sensitive match regular, if the matching in accordance with the order from top to bottom on the URL, and it will not continue to search, i.e., the use of this block location.
~ *: Indicates case-insensitive regular matches, if the match in accordance with the order from top to bottom of the URL, and it will not continue to look for that block the use of this location.

3. General match

/: Expressed any requests are matched.

Examples using location

# 输入http://ip+port/images/1.p
# 此时显示的是'= /images/1.p',因为=匹配优先级最高
location = /images/1.p {
    default_type 'text/plain';
    echo '= /images/1.p';
}
location ^~ /images/1.p {
    default_type 'text/plain';
    echo ' /images/1.p';
}
# 输入http://ip+port/images/1.p
# 此时显示到的是'^~ /images/1.p',因为^~只要匹配到了就会停止匹配,哪怕后续的长度更长
location ^~ /images/ {
    default_type 'text/plain';
    echo '^~ /images/1.p';
}
location ~ /images/1.p {
    default_type 'text/plain';
    echo '~ /images/1.p';
}
# 输入http://ip+port/images/1.pxyzxyz
# 此时显示到的是'~ /images/',因为这是按照顺序匹配的,匹配到了后面的就不再匹配了
location ~ /images/ {
    default_type 'text/plain';
    echo '~ /images/';
}
location ~ /images/1 {
    default_type 'text/plain';
    echo '~ /images/1';
}
# 输入http://ip+port/images/  显示'/',因为没有匹配到后面的URL,使用默认的/规则
# 输入http://ip+port/images/1xyzxyz  显示'~ /images/1',因为匹配到了后面的正则
location / {
    default_type 'text/plain';
    echo '/';
}
location ~ /images/1 {
    default_type 'text/plain';
    echo '~ /images/1';
}
# 输入http://ip+port/images/ 显示'/images/'
# 输入http://ip+port/images/1/ab 显示'/images/'
# 输入http://ip+port/images/1/abc 显示'/images/1/abc'  匹配上第一个location后,会继续向下匹配寻找,如果有更加完整的匹配,则会有下面的。如果没有,则使用当前的。
location /images/ {
    default_type 'text/plain';
    echo '/images/';
}
location /images/1/abc {
    default_type 'text/plain';
    echo '/images/1/abc';
}

Precautions

# 在使用“=”的时候会有意外情况,比方说下面这个例子。当输入'http://ip+port/'时,发现返回的状态码是304&404
# 原因在于Nginx发现请求的是个目录时,会重定向去请求'http://ip+port/index.html',此时返回码是304
# 而后Nginx收到了'http://ip+port/index.html'这个请求,发现没有location能够匹配,就返回404了
location = / {
    default_type 'text/plain';
    index index.html index.htm;
    root /web/;
}

Guess you like

Origin www.cnblogs.com/zimskyzeng/p/11442239.html