NGINX switch root by path param and run for angular project

I want to change the root directory by first URL path param.

The example URL is: https://example-domain.com/uuid_1/product/list?mode=card

Expected behaviour is that the root should be switched to path param of uuid_1. And rest path (/product/list?mode=card) should be run for the angular project.

I need the nginx config for this situation. I tried some config but failed.


Success Scenerios

Example 1:

URL: https://example-domain.com/uuid_1

THEN:
root => /main_folder/uuid_1/angular/dist
run for angular => /

Example 2:

URL: https://example-domain.com/uuid_2/product/list?mode=card

THEN:
root => /main_folder/uuid_2/angular/dist
run for angular => /product/list?mode=card

Example 3:

URL: https://example-domain.com/uuid_3/product/edit?id=abc

THEN:
root => /main_folder/uuid_3/angular/dist
run for angular => /product/edit?id=abc


Server Folder Hierarchy

- main_folder
  - uuid_1
    - angular_project_dist
      - index.html
      - ...(rest_of_files_of_angular_project) 
  - uuid_2
    - angular_project_dist
      - index.html
      - ...(rest_of_files_of_angular_project) 
  - uuid_3
    - angular_project_dist
      - index.html
      - ...(rest_of_files_of_angular_project) 
  - uuid_4
    - angular_project_dist
      - index.html
      - ...(rest_of_files_of_angular_project) 

Failed NGINX config:

server {

    server_name example-domain.com;

    index index.html index.htm;

    # deny access to .htaccess files, if Apache's document root
    location ~ /\.ht {
        deny all;
    }

    location / {

        if ($uri ~* /([^/]+)/(.*)){
            set $project_id $1;
            set $rest_of_uri /$2;
        }
        root /var/www/example-domain.com/$project_id/angular/dist/;
        try_files $rest_of_uri $rest_of_uri/ /index.html;
    }
}

Read more here: Source link