【问题标题】:nginx: location tilde regex vs pathnginx:位置波浪号正则表达式与路径
【发布时间】:2018-01-26 03:05:24
【问题描述】:

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

location ^~ /sub-directory

location /sub-directory

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

【问题讨论】:

标签: nginx reverse-proxy nginx-location proxypass


【解决方案1】:

考虑下面的 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-*";
    }
}

我使用 docker 容器在上面运行

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";
    }

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

【讨论】:

    猜你喜欢
    • 2016-12-06
    • 1970-01-01
    • 2012-06-22
    • 2010-10-30
    • 1970-01-01
    • 2020-09-11
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多