openresty / openresty

High Performance Web Platform Based on Nginx and LuaJIT

Home Page:https://openresty.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sometimes "location = /" can not be matched

changeflyhigh opened this issue · comments

openresty version:openresty-1.21.4.2

#####configure-A(normal):

server {
listen 80;
server_name yunwei.abc.com;

default_type    text/html;

location / {
    return  200  'Thanks for access /';
}

location = / {
    return  200  'Thanks for access =/';
}

}

test result:

curl http://yunwei.abc.com/
Thanks for access =/

curl http://yunwei.abc.com/adfa
Thanks for access /

conclusion:
access “/” and “=/” can be matched:

#####configure-B(Unnormal):

server {
listen 80;
server_name yunwei.abc.com;

default_type    text/html;

location / {
    return  200  'Thanks for access /';
}

location = / {
    # return  200  'Thanks for access =/';
    root   /opt/nginx/nginx/html;
    index  index.html index.htm;
}

}

test result:

curl http://yunwei.abc.com/
Thanks for access /
curl http://yunwei.abc.com/adfa
Thanks for access /

conclusion:
“=/” can not be matched,only “/” can be matched, why?

The = (equals sign) modifier defines an exact match of the URI and a prefix string. If the exact match is found, the search stops.

That means =/ will not expand to =/index.html.
so curl http://yunwei.abc.com/ will match location /

location = / {
    # return  200  'Thanks for access =/';
    root   /opt/nginx/nginx/html;
    index  index.html index.htm;
}

########### nginx.conf ###########
location / {
return 200 'Thanks for access /';
}

location = / {
# return 200 'Thanks for access =/';
root /opt/nginx/nginx/html;
index index.html index.htm;
}

########### shell ###########
so
"curl http://yunwei.abc.com/" will be match location =/
"curl http://yunwei.abc.com/adfa" will be match location /

No matter how the configuration content in {} changes, the matching rules will not change. Is that right?

i find "It should be noted that using an index file causes an internal redirect" in nginx doc (https://nginx.org/en/docs/http/ngx_http_index_module.html. ) so i think this maybe right, because have a internal redirect

i find "It should be noted that using an index file causes an internal redirect" in nginx doc (https://nginx.org/en/docs/http/ngx_http_index_module.html. ) so i think this maybe right, because have a internal redirect

Thanks for your answer, so I try nginx config below:
########### nginx.conf ###########
location = / {
default_type text/html;
# return 200 'Thanks for access =/';
root /opt/nginx/nginx/html;
# index index.html index.htm;
try_files $uri $uri/ /index.html;
}

########### operation ###########
"curl http://yunwei.abc.com/" can't match location =/ yet.

我在 nginx 文档( https://nginx.org/en/docs/http/ngx_http_index_module.html)中发现“应该注意的是,使用索引文件会导致内部重定向”,所以我认为这可能是对的,因为有一个内部重定向

感谢您的回答,所以我尝试下面的 nginx 配置: ########### nginx.conf ########### location = / { default_type text/html; # return 200 '感谢访问=/'; 根/opt/nginx/nginx/html; # 索引index.htmlindex.htm; try_files $uri $uri //index.html; }

##########操作########## "curl http://yunwei.abc.com/ " 还不能匹配location =/。

in this config, =/ location will use /index.html jump to location / {}; in my mind, you need use real url to config location instead of “=/”