Nginx正则表达式匹配单个路径

时间:2020-09-29 15:12:52

标签: regex nginx nginx-location nginx-config

假设我有以下路径:

/
/something # this path is variable (any characters except /)
/api/v1/something

捕获此要求的最佳nginx配置是什么?以下不适用于m:

server {
    listen 8080;
    location ~^/(?:.*)$ {
        ...
    }
    location / {
        ...
    }
    location /api/v1/something {
        ...
    }
}

1 个答案:

答案 0 :(得分:0)

您的正则表达式匹配所有内容,这意味着它总是赢!请参阅the documentation以了解Nginx如何选择哪个location块来处理请求。

您不需要使用正则表达式location

首先使用location运算符为单个URI定义=块:

location = / { ... }
location = /api/v1/foo { ... }

然后使用默认的location捕获其他内容:

location / { ... }

上面的location块可以按任何顺序出现。只有正则表达式对求值顺序敏感。

相关问题