Amlan Kar
Amlan Kar

Reputation: 34

Using wildcards/regex with variables in NGINX location

I have a nginx configuration that looks like this, where I serve different build versions at different paths. How can I do this using regex/wildcards? I need to be able to capture the path (PATH1, PATH2 etc.) as a variable and use it inside the alias directive as well.

    location /sitename/PATH1 {
      alias /some/fldr/PATH1;
      try_files $uri /sitename/PATH1/index.html;
    }

    location /sitename/PATH2 {
      alias /some/fldr/PATH2;
      try_files $uri /sitename/PATH2/index.html;
    }

    location /sitename/PATH3 {
      alias /some/fldr/PATH3;
      try_files $uri /sitename/PATH3/index.html;
    }

Upvotes: 0

Views: 1213

Answers (1)

Ivan Shatsky
Ivan Shatsky

Reputation: 15478

Check this:

location ~ ^/sitename/(?<path>PATH1|PATH2|PATH3)(?<subpath>/.*)? {
    alias /some/fldr/$path/;
    try_files $subpath /sitename/$path/index.html;
}

Upvotes: 2

Related Questions