简体   繁体   English

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

[英]Nginx regex to match single path

Suppose I have these paths:假设我有这些路径:

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

What is the best nginx config to capture this requirement?捕获此要求的最佳 nginx 配置是什么? The following is not working for m:以下不适用于 m:

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

Your regular expression matches everything which means it always wins!您的正则表达式匹配所有内容,这意味着它总是获胜! See the documentation to understand how Nginx chooses which location block to process a request.请参阅文档以了解 Nginx 如何选择哪个location块来处理请求。

You do not need to use a regular expression location .您不需要使用正则表达式location

First define location blocks for the single URIs using the = operator:首先使用=运算符为单个 URI 定义location块:

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

Then use the default location to catch anything else:然后使用默认location捕获其他任何内容:

location / { ... }

The above location blocks can appear in any order.上述location块可以以任何顺序出现。 Only regular expressions are sensitive to evaluation order.只有正则表达式对求值顺序敏感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM