其他分享
首页 > 其他分享> > location应用

location应用

作者:互联网

location应用

在这里插入图片描述
没有修饰符

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
        location /abc {
            echo "abc";
        }
[root@localhost conf]# nginx -s reload

[root@localhost ~]# curl 192.168.136.132/abc
abc
[root@localhost ~]# curl 192.168.136.132/abc\?p1\=11\&p2\=22
abc
[root@localhost ~]# curl 192.168.136.132/abc/
abc
[root@localhost ~]# curl 192.168.136.132/abcde
abc

表示必须与指定的模式精确匹配,如:
        location = /abc {
            echo "abc";
        }
        
        location /abcde {
            echo "abcde";
        }

[root@localhost ~]# curl 192.168.136.132/abc
abc
[root@localhost ~]# curl 192.168.136.132/abcd
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost ~]# curl 192.168.136.132/abcde
abcde
[root@localhost ~]# curl 192.168.136.132/abcda
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost ~]# curl 192.168.136.132/abcdedas
abcde
[root@localhost ~]# curl 192.168.136.132/abc\?p1\=11\&p2\=22
abc
表示指定的正则表达式要区分大小写
如
        location ~ ^/abcde$ {
            echo "abcde";
        }
[root@localhost ~]# curl 192.168.136.134/abcde
abcde
[root@localhost ~]# curl 192.168.136.134/abcdE
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost ~]# curl 192.168.136.134/ABCDE
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost ~]# curl 192.168.136.134/abcde\?p1\=11\&p2\=22
abcde
[root@localhost ~]# curl 192.168.136.134/abcdef
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
表示指定的正则表达式不区分大小写
如:
        location ~* ^/abcde$ {
            echo "abcde";
        }
[root@localhost ~]# curl 192.168.136.134/abcde
abcde
[root@localhost ~]# curl 192.168.136.134/abcdE
abcde
[root@localhost ~]# curl 192.168.136.134/ABCDE
abcde
[root@localhost ~]# curl 192.168.136.134/abc
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost ~]# curl 192.168.136.134/ABCDE\?p1\=11\&p2\=22
abcde
查找顺序和优先级:由高到底依次为
带有=的精确匹配优先
正则表达式按照他们在配置文件中定义的顺序
带有^~修饰符的,开头匹配
带有~或~*修饰符的,如果正则表达式与URI匹配
没有修饰符的精确匹配
    location = /abc {
        echo "abc";
    }
    
    location ~ /abcde {
        echo "abcde";
    }

    location ~* ^/abcde$ {
        echo "xxbb";
    }
[root@localhost ~]# curl 192.168.136.134/abcde
abcde
[root@localhost ~]# curl 192.168.136.134/abcd
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost ~]# curl 192.168.136.134/abcde
abcde
[root@localhost ~]# curl 192.168.136.134/abcdes
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
没有精准匹配符
        #location = /abc {
        #    echo "abc";
        #}
    location ~ ^/abcde$ {
        echo "abcde";
    }

    location ~* ^/abcde$ {
        echo "xxbb";
    }
[root@localhost ~]# curl 192.168.136.134/abcde
abcde
[root@localhost ~]# curl 192.168.136.134/abcd
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost ~]# curl 192.168.136.134/abcde
abcde
[root@localhost ~]# curl 192.168.136.134/abcdes
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

优先级顺序:
( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )

标签:abc,abcde,192.168,location,应用,curl,root,localhost
来源: https://blog.csdn.net/weixin_57691077/article/details/118279381