Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write nginx rules/regexes to match empty non empty path

Tags:

nginx

I want to have an nginx rule that will proxy requests with empty path / to a back end server, and another rule that match non empty paths, ex. http://mysite/x/y/z

The following two rules do not do this, the second one is catching all:

# empty path
location ^/?$ {
    proxy_pass   http://127.0.0.1:8000;
}

location / {
    expires -1;
    alias /var/static-site/;
}

I have tried /.*/ for the second rule, without success...

like image 818
Max L. Avatar asked Sep 10 '25 10:09

Max L.


1 Answers

Use the "=" modifier to process an exact match on "/":

location = / {
    proxy_pass   http://127.0.0.1:8000;
}

location / {
    expires -1;
    alias /var/static-site/;
}
like image 70
Dayo Avatar answered Sep 13 '25 07:09

Dayo