Nginx static resource address

9 Views Asked by At

Translation The project is placed in the "nginx/html" folder. I have the following configuration in the nginx.conf file:

location /project1 {
    root html;
    index  index.html;
}

location /project2 {
    root html;
    index  index.html index.htm;
}

I can access each project correctly. However, when accessing static resources, all project's static resource lookup paths are "html". For example:

Request: "GET /js/app.c930e0fe.js HTTP/2.0", referrer: "website/project1"

The expected path should be "html/project1/js/app.c930e0fe.js", but the actual path is "html/js/app.c930e0fe.js".

Can you help me with this?

Attempt 1: I tried the following configuration, but it still looks for static resources in the "html" folder:

location /project1 {
    alias html/project1;
    index index.html;
}

location /project2 {
    alias html/project2;
    index index.html index.htm;
}

Attempt 2: I attempted to add the project path in front of static resources using the referrer variable, but it didn't work:

map $http_referer $referrer_path {
    "~^my website/(.*)/$" /$1;
}

Attempt 3: I also tried the following configuration to customize the lookup path for static resources, but it still looks for them in the "html" folder:

location ~* \.(js|css|woff|jpg|png|woff2)$ {
    if ($request_uri ~ "^/([^?]*)") {
        set $request_path $1;
    }

    if ($http_referer ~* "^my website/([^?]*)") {
        set $referrer_path $1;
    }
    set $resource_path "html/$referrer_path/$request_path";
    try_files =404 /$resource_path;
}
0

There are 0 best solutions below