Nginx regex to match single path

870 views Asked by At

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? The following is not working for m:

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

There are 1 answers

0
Richard Smith On BEST ANSWER

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.

You do not need to use a regular expression location.

First define location blocks for the single URIs using the = operator:

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

Then use the default location to catch anything else:

location / { ... }

The above location blocks can appear in any order. Only regular expressions are sensitive to evaluation order.