nginx:位置波浪号正则表达式与路径



这两个nginx指令有什么区别?

location ^~ /sub-directory

location /sub-directory

在下面的代码块中,使用proxy_pass进行重定向(如果这有所不同(。

考虑下面的nginx配置

worker_processes  1;
events {
worker_connections  1024;
}
server {
listen 80;
server_name _;
location ^~ /sub-directory {
echo "^~ /sub-directory";
}
location /sub-director
{
echo "/sub-director";
}
location ~* /sub-* {
echo "~* /sub-*";
}
}

我使用码头工人容器在上面运行

sudo docker run -p 80:80 -v $PWD/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf openresty/openresty

现在考虑以下 curl 语句

$ curl http://192.168.33.100/sub-director/abc
~* /sub.*
$ curl http://192.168.33.100/sub-director/
~* /sub.*
$ curl http://192.168.33.100/sub-director
~* /sub.*
$ curl http://192.168.33.100/sub-directory
^~ /sub-directory
$ curl http://192.168.33.100/sub-directory/
^~ /sub-directory
$ curl http://192.168.33.100/sub-directory/abc
^~ /sub-directory

如您所见,我无法以任何方式到达以下位置块

location /sub-director
{
echo "/sub-director";
}

因为正则表达式会覆盖此块。但我仍然可以到达

location ^~ /sub-directory {
echo "^~ /sub-directory";
}

这就是区别。当您使用^~并且位置匹配时,根本不会评估正则表达式基位置。

最新更新